By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

when is sizeof evaluated

P: n/a
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

Oct 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

--

Frederick Gotham
Oct 28 '06 #2

P: n/a
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

Oct 29 '06 #3

P: n/a

Divick wrote:
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?
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

Oct 29 '06 #4

P: n/a
Greg wrote:
>
Divick wrote:
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?

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
Oct 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.