There are some bewildering sizeof() questions I have in my mind. Could anyone shed some light on this?
int main() {
printf("%d\n", sizeof(main)); // Ans: 1
}
That is, the sizeof() a function identifier though it is treated internally as a pointer gives 1 byte always, why?
Also, sizeof(void) gives output 1 where as sizeof(void*) is 4 bytes on my Ubuntu 8.04 (32-bit) machine. I am using gnu gcc compiler. Whether this is compiler dependent?
Also, the following program compiles perfectly for me without any error
int main() {
void *vptr = (void *) malloc(sizeof(void)); // (or) malloc (sizeof(void*));
vptr++; // what it is incremented to?
}
And, why the output of the following program is still 10?
int main() {
int i = 10;
printf("size is %d bytes\n", sizeof(i++)); // Ans: size is 4 bytes
printf("%d\n", i); // Ans: 10
}
main(), and functions in general, aren't sensible things to take a sizeof of. The compiler can't possibly know the code size yet. It's forbidden entirely in C++.
It doesn't make much sense to sizeof(void) either. void is a special variable type that can only be declared as a pointer and can't be dereferenced without typecasting. That it has a sizeof() at all is probably just to make pointer arithmetic simpler.
As for what a 'void *' type is incremented to, you can test that easily by printf("%p", pointer); It is incrememented as one byte, as you'd expect for a type with a sizeof(1). Makes sense. You don't know the size of its elements, if any, so assume the lowest common denominator.
As for why sizeof(i++) doesn't increment it, an expression isn't a sensible thing to take a sizeof either. sizeof() isn't a function call or any sort of runtime operation, the compiler substitutes one literal value at compile time and is done with it. The compiler doesn't have to calculate the value of i++ to understand that i+1 is still an integer, and substitute the size of an integer. It's more akin to a typedef than anything, and you wouldn't expect an expression to work in a typedef. But since sizeof() gets interspersed inside procedual code it has to tolerate more garbage.
In summary don't take the sizeof of an expression, function, or void thing. Only variables, pointers, and types. You'll always get sane results on those.
Be careful with this - a function identifier is not a pointer. You get a pointer when you take the address of a function (e.g. "&main"), and the compiler treats use of the raw identifier as a pointer when it expects a pointer to be there, for example when you're passing a function pointer as an argument to a function. The following code might clarify: