when is sizeof evaluated

D

Divick

Hi,
does sizeof operator evaluate the size at compile time or run time
? In other words is sizeof(SomeType) evaluated at compile time or at
runtime? Please provide the reasoning involved as well.

TIA,
Divick
 
F

Frederick Gotham

Divick:
Hi,
does sizeof operator evaluate the size at compile time or run time
? In other words is sizeof(SomeType) evaluated at compile time or at
runtime? Please provide the reasoning involved as well.


The C++ Standard places no restriction on when, where, how, what and why a
particular usage of the "sizeof" operator is evaluated; it only necessitates
that the resultant value be available as a compile-time constant.
 
D

Divick

The C++ Standard places no restriction on when, where, how, what and why a
particular usage of the "sizeof" operator is evaluated; it only necessitates
that the resultant value be available as a compile-time constant.
Does that mean that sizeof is a compile time construct (I guess that is
what you mean in above but still you say that C++ standard places no
restrictions "when" it is evaluated)? Could you please provide some
insight into how it is evaluated no matter whatever the
implementation?

Thanks,
Divick
 
G

Greg

Divick said:
Does that mean that sizeof is a compile time construct (I guess that is
what you mean in above but still you say that C++ standard places no
restrictions "when" it is evaluated)? Could you please provide some
insight into how it is evaluated no matter whatever the
implementation?

The sizeof() operator is a compile-time operator and the value it
returns is an integral constant. Note moreover that the operand passed
to the sizeof() operator is not "evaluated" - meaning that sizeof()
calls no functions, allocates no objects or otherwise has any type of
runtime behavior at all.

Greg
 
D

Default User

Greg said:
The sizeof() operator is a compile-time operator and the value it
returns is an integral constant. Note moreover that the operand passed
to the sizeof() operator is not "evaluated" - meaning that sizeof()
calls no functions, allocates no objects or otherwise has any type of
runtime behavior at all.

This is particularly important when dealing with pointers. The sizeof
operator doesn't evaluate the pointer at all, so it's safe to have a
null pointer or a pointer that hasn't been set at all. Even
"dereferences" are fine:

int* p = 0;

size_t size = sizeof *p; // renders the size of the underlying object



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top