sizeof main;

Discussion in 'C Programming' started by Vijay Kumar R Zanvar, Dec 2, 2003.

  1. Hi,

    Why the following program gives two different
    answers? -

    #include <stdio.h>

    int
    main ( void )
    {
    printf ( "%u\n", sizeof ( main ) );
    printf ( "%u\n", sizeof ( main () ) );
    return 0;
    }

    GCC 3.3.2 (Windows XP) gives the following o/p:

    1
    4

    Thanks.

    --
    Vijay Kumar R Zanvar
    My Home Page - http://www.geocities.com/vijoeyz/
    Vijay Kumar R Zanvar, Dec 2, 2003
    #1
    1. Advertising

  2. Vijay Kumar R Zanvar

    Grumble Guest

    Vijay Kumar R Zanvar wrote:
    > #include <stdio.h>
    >
    > int
    > main ( void )
    > {
    > printf ( "%u\n", sizeof ( main ) );
    > printf ( "%u\n", sizeof ( main () ) );
    > return 0;
    > }
    >
    > GCC 3.3.2 (Windows XP) gives the following o/p:
    >
    > 1
    > 4


    Probably some weird gcc extension since:

    $ gcc -pedantic toto.c
    toto.c: In function `main':
    toto.c:5: warning: sizeof applied to a function type

    Therefore, you might want to ask on the gcc mailing list.
    Grumble, Dec 2, 2003
    #2
    1. Advertising

  3. Vijay Kumar R Zanvar wrote:

    > Why the following program gives two different
    > answers? -
    >
    > printf ( "%u\n", sizeof ( main ) );


    I don't know what this is supposed to mean. The size of the code of the
    main function? Apparently not, given the returned value. Allowing this
    seems to be a GCC extension, and not a very useful one. Maybe the gcc
    manual explains it.

    > printf ( "%u\n", sizeof ( main () ) );


    This is the size of the expression main(), i.e. the size of main's
    return type, int.

    --
    Hallvard
    Hallvard B Furuseth, Dec 2, 2003
    #3
  4. Vijay Kumar R Zanvar

    Jonas Mellin Guest

    Vijay Kumar R Zanvar wrote:
    > Hi,
    >
    > Why the following program gives two different
    > answers? -




    >
    > #include <stdio.h>
    >
    > int
    > main ( void )
    > {
    > printf ( "%u\n", sizeof ( main ) );



    This is not really allowed. In ISO-C section 6.5.4.3 it is stated:
    Constraints
    1 The sizeof operator shall not be applied to an expression that has
    function type or an incomplete type, to the parenthesized name of such a
    type, or to an lvalue that designates a bit-field object.

    If you turn on all options in gcc to obtain good warnings etc., you will
    capture the error:

    gcc -ansi -pedantic -Wall sizeof.c
    sizeof.c: In function `main':
    sizeof.c:6: warning: sizeof applied to a function type

    The function main is of 'int (*f)(X)' type, where I am not entirely sure
    how to specify X (variable argument list that does not follow ISO-C, may
    be '...'). That is, main is of a function type.

    I am uncertain that the return value of sizeof has an actual meaning in gcc.

    > printf ( "%u\n", sizeof ( main () ) );


    This is the size of 'int' type.

    > return 0;
    > }
    >
    > GCC 3.3.2 (Windows XP) gives the following o/p:
    >
    > 1
    > 4
    >
    > Thanks.
    >
    > --
    > Vijay Kumar R Zanvar
    > My Home Page - http://www.geocities.com/vijoeyz/
    >
    >
    Jonas Mellin, Dec 2, 2003
    #4
  5. Thank You. It solves my problem.
    Vijay Kumar R Zanvar, Dec 2, 2003
    #5
  6. Vijay Kumar R Zanvar wrote:

    > Hi,
    >
    > Why the following program gives two different
    > answers? -


    The *real* question is why you ignored or suppressed the gcc diagnostics
    which I have embedded as comments into your code:

    #include <stdio.h>

    int main(void)
    {
    printf("%u\n", sizeof(main));

    /* gcc diagnostics:
    warning: invalid application of `sizeof' to a function type
    warning: unsigned int format, long unsigned int arg (arg 2)
    */
    printf("%u\n", sizeof(main()));

    /* gcc diagnostic:
    warning: unsigned int format, long unsigned int arg (arg 2)
    */
    return 0;
    }





    --
    Martin Ambuhl
    Martin Ambuhl, Dec 2, 2003
    #6
  7. Vijay Kumar R Zanvar

    Richard Bos Guest

    "Vijay Kumar R Zanvar" <> wrote:

    > Why the following program gives two different
    > answers? -


    Short answer: because C is not Pascal.

    Long answer:

    > printf ( "%u\n", sizeof ( main ) );


    This asks for the size of the main function, which is actually illegal:

    # 6.5.3.4 The sizeof operator
    # Constraints
    # 1 The sizeof operator shall not be applied to an expression that has
    # function type

    Since your compiler apparently decided to compile thr program anyway,
    this constraint violation invokes undefined behaviour, and may give any
    answer whatsoever. In principle, anything which happens after this point
    could be completely random.

    > printf ( "%u\n", sizeof ( main () ) );


    This asks for the size of the value returned by evaluating main(). Since
    main() returns int (and you've correctly defined it as doing so), that
    is equal to sizeof (int). Note, btw, that main() isn't evaluated, since
    its size can be determined without doing so (unlike its value).

    Note also that you are invoking undefined behaviour in both printf()
    calls, because you don't know that size_t is equivalent to unsigned int.
    Either cast both sizeofs to (unsigned int), so it corresponds to the
    printf() specifier %u, or, if you use C99, change the %u to %zu.

    > GCC 3.3.2 (Windows XP) gives the following o/p:
    >
    > 1
    > 4


    Apparently, your compiler has decided to give all functions a size of 1,
    and int is four bytes large on your implementation.

    Richard
    Richard Bos, Dec 2, 2003
    #7

  8. > The *real* question is why you ignored or suppressed the gcc diagnostics
    > which I have embedded as comments into your code:


    May be there is no meaning to the program what I have written.
    But, look! What a response I am getting and how it is helping
    me to learn more! Do you not agree?

    I did notice the warnings, but that was not my answer.

    Thanks.

    --
    Vijay Kumar R Zanvar
    Vijay Kumar R Zanvar, Dec 2, 2003
    #8
  9. Vijay Kumar R Zanvar

    Dan Pop Guest

    In <bqhofn$229ca3$-berlin.de> "Vijay Kumar R Zanvar" <> writes:

    >I did notice the warnings, but that was not my answer.


    If you did notice them, yet you couldn't figure out the answer, you have
    a problem. A big one.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Dec 2, 2003
    #9
  10. Vijay Kumar R Zanvar

    Dan Pop Guest

    In <bqhlto$8cf$> Grumble <> writes:

    >Vijay Kumar R Zanvar wrote:
    >> #include <stdio.h>
    >>
    >> int
    >> main ( void )
    >> {
    >> printf ( "%u\n", sizeof ( main ) );
    >> printf ( "%u\n", sizeof ( main () ) );
    >> return 0;
    >> }
    >>
    >> GCC 3.3.2 (Windows XP) gives the following o/p:
    >>
    >> 1
    >> 4

    >
    >Probably some weird gcc extension since:


    Indeed: in GNU C, sizeof(something) yields 1 when something is not a
    complete object type. The purpose of this extension is to allow
    performing pointer arithmetic on pointers to functions or to objects
    of incomplete types, like void pointers by treating them as pointers to
    char. It saves some casts or temporary pointer variables in certain
    cases, which is a good thing (if you can afford it).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Dec 2, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,329
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    628
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,415
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    555
    Joe Smith
    May 22, 2006
  5. Alex Vinokur
    Replies:
    7
    Views:
    497
    Clark S. Cox III
    Aug 14, 2006
Loading...

Share This Page