473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

when is sizeof evaluated

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
4 6020
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
18
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
17
by: ³á³á³á³á | last post by:
int i = 0; i = sizeof( i++ ); What's the result of i¡H I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is undefined behavior. I know that i = i++ is undefined...
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
12
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
11
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
10
by: Kislay | last post by:
int main() { int i=10; printf("\n Size of i = %d ",sizeof(++i)); printf("\n i = %d ",i); system("pause"); return 0; } On executing the above code , the value of i obtained as 10 . What
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.