sizeof operator
Is the sizeof operator in c a compile time or run time operator..Because the following code works fine with a gcc compiler
int a;
scanf("%d",&a);
printf("%d",sizeof(a));
But I found in one of the tutorials thatsizeof is compile time??Can someone please clear the confusion?
Related Article: sizeof operator
is a C discussion thread by nitin1 that has 22 replies and was last updated 7 months ago.
saurabh.mehta.33234
Junior Poster in Training
52 posts since Nov 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
@Tumlee thankyou foryour answer I agree to your point..but then what about the following code
int foo(){return 5;}
int main()
{
printf("%d",sizeof(foo()));
return 0;
}
here the call to foo() is made at runtime so the sizeof is evaluated at runtime??
saurabh.mehta.33234
Junior Poster in Training
52 posts since Nov 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36
@tux4life thank you for your links but I have a different doubt...I want to know that since the sizeof is compile time operator then why does thee above code outputs 4 even though the call to foo() will be made at runtime..So how does the compiler know that the value passed as parameter to sizeof will be int if the call itself is made at runtime!!!
saurabh.mehta.33234
Junior Poster in Training
52 posts since Nov 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
I guess it is because the size of the return type of foo() is known at compile time, but I'm probably NOT correct on this.
I hope that someone else can clarify on this, I might learn something too then ;)
[EDIT]
Okay, apparently I was wrong
Standard C (ISO/IEC 9899:1999) section 6.5.3.4:
"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 expression that designates a bit-field member."
(source: http://stackoverflow.com/questions/2666392/what-does-sizeof-function-name-return)
[/EDIT]
mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36
foo() on line 4 of that code is a pointer, not a function. All pointers regardless of type are the same size, and in 32-bit compilers they are 4 bytes. szieof() does not work on functions as it treats function names as pointers to a function.
Ancient Dragon
Achieved Level 70
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,576
Skill Endorsements: 69
I want to know that since the sizeof is compile time operator then why does thee above code outputs 4 even though the call to foo() will be made at runtime.
You'll notice that foo() isn't called when used as an operand to sizeof:
#include <stdio.h>
int foo()
{
puts("foo!");
return 5;
}
int main(void)
{
printf("%d\n", sizeof(foo()));
return 0;
}
That's because sizeof only evaluates the result of an expression, even if the expression contains runtime-only components or would be completely invalid if actually run. Another example is with malloc():
int *p = malloc(sizeof *p);
Dereferencing an uninitialized pointer is a horrible idea, but because sizeof evaluates the result as if the expression were run instead of actually running it, the above line is completely safe and even recommended.
deceptikon
Challenge Accepted
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
@ancient dragon thankyou for your answer>> i tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..
saurabh.mehta.33234
Junior Poster in Training
52 posts since Nov 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
i tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..
That proves nothing as the most common size of float is 4 bytes and there's no guarantee that a function pointer have the same size as object pointers. Try using double and you'll probably get output of 8, or char and you'll get output of 1. sizeof in this case is evaluating to the type of the return value.
deceptikon
Challenge Accepted
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
I tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..please contribute if you feel my conclusion is wrong!!
saurabh.mehta.33234
Junior Poster in Training
52 posts since Nov 2012
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
please contribute if you feel my conclusion is wrong!!
I'll assume that you simply missed my post before saying that. If so, please read my post. If not, please read my post again until you understand it.
deceptikon
Challenge Accepted
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57