applying sizeof on function type. is it correct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sh.vipin@gmail.com

    #1

    applying sizeof on function type. is it correct

    In the following program, with
    gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56)

    there is no error. I have two queries.

    1. isn't it illegal to apply sizeof on function type.
    6.5.3.4 The sizeof operator
    The sizeof operator shall not be applied to an expression that has
    func
    tion type....

    in fact VS 2005 gives error on this.

    2. If compiler is taking f as a pointer and not as function name,
    which is legal to do though i am not sure if it is advisable to do so
    here in this case, then why the sizeof f is "1" and not 4 as for
    sizeof g

    /** fully compilable program */
    #include <stdio.h>
    void f(void ){

    }

    void (*g)(void);
    int main(int argc, char *argv[]){
    g = f;
    printf("\n%u %u",sizeof f , sizeof g);
    }


    -- vIpIn
  • Eric Sosman

    #2
    Re: applying sizeof on function type. is it correct

    sh.vipin@gmail. com wrote:
    In the following program, with
    gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56)
    >
    there is no error. I have two queries.
    >
    1. isn't it illegal to apply sizeof on function type.
    6.5.3.4 The sizeof operator
    The sizeof operator shall not be applied to an expression that has
    func
    tion type....
    >
    in fact VS 2005 gives error on this.
    Yes, you have read 6.5.3.4 correctly.
    2. If compiler is taking f as a pointer and not as function name,
    which is legal to do though i am not sure if it is advisable to do so
    here in this case, then why the sizeof f is "1" and not 4 as for
    sizeof g
    >
    /** fully compilable program */
    #include <stdio.h>
    void f(void ){
    >
    }
    >
    void (*g)(void);
    int main(int argc, char *argv[]){
    g = f;
    printf("\n%u %u",sizeof f , sizeof g);
    }
    By default, gcc accepts a language that is not exactly C,
    but "C with extras." One of those extras allows sizeof to be
    used on some kinds of operands that C forbids. You will find
    this documented in gcc's "info" as a gcc extension.

    The "info" also has a section on how to invoke gcc so it
    compiles actual C instead of "gccC." If you compile your
    program with the "-ansi" or "-std=c89" or "-std=c99" flag
    (see the "info" for their meanings), you should see a different
    outcome.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • Keith Thompson

      #3
      Re: applying sizeof on function type. is it correct

      sh.vipin@gmail. com writes:
      [...]
      /** fully compilable program */
      #include <stdio.h>
      void f(void ){
      >
      }
      >
      void (*g)(void);
      int main(int argc, char *argv[]){
      g = f;
      printf("\n%u %u",sizeof f , sizeof g);
      }
      Eric Sosman already answered your question (this is a gcc-specific
      extension), and you can get gcc to warn you about it with the proper
      options). There are also two (unrelated) problems in your printf
      call.

      "%u" is the format for printing a value of type unsigned int; the
      result of sizeof is of type size_t, which is an unsigned type that may
      or may not be unsigned int. If, for example, unsigned int is 32 bits
      and size_t is 64 bits, your program could misbehave.

      You print the new-line character at the beginning of the line. You
      should print it at the end. A new-line before the start of your
      output is unnecessary. A missing new-line at the end of your output
      can cause your program to misbehave, depending on the implementation.

      A more portable version (without fixing the sizeof problem):
      printf("%lu %lu\n",
      (unsigned long)sizeof f,
      (unsigned long)sizeof g);

      C99 specifies "%zu" for printing a size_t value directly, but
      unfortunately C99 support is not universal.

      Finally, since main returns an int, you should return an int; add
      "return 0;" before the closing "}". It's not required in C99, but in
      my opinion it's good style.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • jacob navia

        #4
        Re: applying sizeof on function type. is it correct

        sh.vipin@gmail. com wrote:
        In the following program, with
        gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56)
        >
        there is no error. I have two queries.
        >
        1. isn't it illegal to apply sizeof on function type.
        6.5.3.4 The sizeof operator
        The sizeof operator shall not be applied to an expression that has
        func
        tion type....
        >
        in fact VS 2005 gives error on this.
        >
        lcc-win gives an error too.



        --
        jacob navia
        jacob at jacob point remcomp point fr
        logiciels/informatique
        http://www.cs.virginia.edu/~lcc-win32

        Comment

        Working...