Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Originally, this is a part of another question.

Why is sizeof called a compile-time operator? Isn't it actually a run-time operator? And if it is indeed a compile-time operator, how does it help in producing portable code which runs the same in different computers ? Please explain in detail.

share|improve this question
4  
answered in details at SO: Why does sizeof(x++) not increment x? –  gnat Apr 18 '13 at 17:50
3  
How do you expect the size of a type to change at runtime? –  MichaelT Apr 18 '13 at 18:12
    
@MichaelT: The size of an instance of a type can certainly change -- there's class polymorphism, after all. I'd argue that sizeof(polymorphic_ptr*) being constant is quite counter-intuitive and just silly. Yeah, it's the C++ way, but silly nevertheless. –  Kuba Ober Apr 18 '13 at 20:24
    
@KubaOber Indeed. I'm just curious how the OP thought it should behave in different cases and hopefully get some code that would demonstrate his confusion over this to help expand the question. –  MichaelT Apr 18 '13 at 20:27
4  
Came to upvote the answer "because it operates at compile time", left disappointed. –  Ben Jackson Apr 18 '13 at 20:44

4 Answers 4

up vote 23 down vote accepted

sizeof() gives you the size of the data type, not the size of a particular instance of that type in memory.

For example, if you had a string data object that allocated a variable size character array at runtime, sizeof() could not be used to determine the size of that character array. It would only give you the size of the pointer.

The size of a data type is always known at compile time.

share|improve this answer
3  
Because C(++) doesn't have run-time object metadata you can't really get those things at run time either. –  C. Ross Apr 18 '13 at 17:28
5  
Actually, if you use sizeof on an array, you will get the size of the array (that is the element size times the number of elements). But if you use it on a pointer, you only get the size of the pointer. So, since in most cases where you'd like to know the size of an array, you only have a pointer, it's not all that useful. –  sepp2k Apr 18 '13 at 18:26
1  
@Jens The question is tagged [C++] and VLA did not make it into the C++ standard. –  authchir Apr 18 '13 at 20:54
    
@sepp2k, yeah, corrected. Thanks. –  dan1111 Apr 19 '13 at 4:35

because the entire sizeof "call" is calculated at compile time and whatever is between the parenthesis is discarded and not run at runtime,

the result is purely based on static type info available to the compiler

share|improve this answer

Why is sizeof called a compile-time operator?

Because, at compile time, the compiler calculates the sizeof the expression and substitutes that compile-time constant value.

Isn't it actually a run-time operator?

No. You can even use sizeof to evaluate the size of expressions you can't legally execute (ie, that would incur Undefined Behaviour), so long as the compiler can figure out what the type of the expression is.

Also, even before C++11 constexpr, you can use sizeof expressions in ways you can't use run-time expressions.

And if it is indeed a compile-time operator, how does it help in producing portable code ...

Types may vary in size on different platforms. Using sizeof expressions instead of hard-coded assumptions means your code won't break when you compile on a different platform and your types change size.

share|improve this answer
    
Well, I find this the most useful answer ;). Bro(assuming you to be male), I have a doubt. Do you mean to say that when people say that sizeof makes programs portable, they mean that the source code can be compiled without any errors in all machines and they don't mean that the the executable program can be run in any machine. Right ? If this is right, my doubt gets really cleared. –  The Peaceful Coder Apr 19 '13 at 10:18
    
Yeah, the code is portable, in the sense you can compile a (different) correct binary for each platform. –  Useless Apr 19 '13 at 10:34

C++ doesn't actually store the metadata for objects at runtime so size checking must be compile time. For an example of how C++ doesn't validate size, declare an array of int of some arbitrary size and read past the end of it. If you're lucky you'll get a segfault but more likely you'll simply read gibberish, because C++ doesn't track the size of your array.

See Can a C/C++ program seg-fault from reading past the end of an array (UNIX)? for an example from SO.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.