In the gcc compiler, sizeof(main)
, sizeof(printf)
and sizeof(scanf)
all are 1.
I want to know how the size of all these are 1. What is the logic behind it?
views:
143answers:
2
A:
I would have thought it is the size of a pointer to function. One actually surprises me...
Mario The Spoon
2010-10-01 11:57:26
That should be a comment, it's not really an answer.
Joachim Sauer
2010-10-01 11:58:55
instead of upvoting this comment, you people should downvote the "answer" instead.
codymanix
2010-10-01 12:09:13
What's wrong with comments? I find the one of the most useful things ever is discussing, with other developers, what they thought. Eliminating that is, IMHO, gross folly.
Blank Xavier
2010-10-01 13:40:31
So applying sizeof to a function type is undefined/unspecified behaviour?
codymanix
2010-10-01 12:01:06
@Parixit: Because you did something that makes no sense, so you get a result that makes no sense. Garbage in, garbage out. It's undefined behavior, and the compiler could do anything it likes, including formatting your harddrive or setting your hair on fire. But it chose to do something a bit less dramatic, and just returned 1
jalf
2010-10-01 12:06:49
I guess that function as is have no size (size=0), but sizeof is required to return something > 0. And yes, a compile error instead would have made more sense to me.
codymanix
2010-10-01 12:10:59
@Parixit The gcc developers decided to hardcode that `sizeof function` is 1.
nos
2010-10-01 12:11:31
As far as the standard is concerned, shouldn't this result in a diagnostic, rather than undefined behavior, since it's a breach of language constraints? Indeed, with `-pedantic`, gcc issues a warning. Obviously in non-standard modes (without `-pedantic`), gcc can do what it likes: if you're not following the standard, then *everything* is undefined behavior by definition.
Steve Jessop
2010-10-01 12:38:24