14

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.

asked Apr 18, 2013 at 17:04
6
  • 4
    answered in details at SO: Why does sizeof(x++) not increment x? Commented Apr 18, 2013 at 17:50
  • 3
    How do you expect the size of a type to change at runtime? Commented Apr 18, 2013 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. Commented Apr 18, 2013 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. Commented Apr 18, 2013 at 20:27
  • 4
    Came to upvote the answer "because it operates at compile time", left disappointed. Commented Apr 18, 2013 at 20:44

4 Answers 4

24

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.

answered Apr 18, 2013 at 17:06
3
  • 3
    Because C(++) doesn't have run-time object metadata you can't really get those things at run time either. Commented Apr 18, 2013 at 17:28
  • 6
    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. Commented Apr 18, 2013 at 18:26
  • 1
    @Jens The question is tagged [C++] and VLA did not make it into the C++ standard. Commented Apr 18, 2013 at 20:54
13

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

answered Apr 18, 2013 at 17:40
7

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.

answered Apr 19, 2013 at 9:41
2
  • 1
    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. Commented Apr 19, 2013 at 10:18
  • 1
    Yeah, the code is portable, in the sense you can compile a (different) correct binary for each platform. Commented Apr 19, 2013 at 10:34
5

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.

answered Apr 18, 2013 at 17:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.