sizeof main;

  • Thread starter Vijay Kumar R Zanvar
  • Start date
V

Vijay Kumar R Zanvar

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.
 
G

Grumble

Vijay said:
#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.
 
H

Hallvard B Furuseth

Vijay said:
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.
 
J

Jonas Mellin

Vijay said:
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.
 
M

Martin Ambuhl

Vijay said:
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;
}
 
R

Richard Bos

Vijay Kumar R Zanvar said:
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
 
V

Vijay Kumar R Zanvar

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.
 
D

Dan Pop

In said:
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
 
D

Dan Pop

In said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top