473,898 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof() calculated at compile time or run time

gotta question on sizeof keyword

does the sizeof keyword calcuates the size at compile time or run time ??

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.n et --
Sep 14 '08 #1
27 5623
CodeMonk3y wrote:
gotta question on sizeof keyword

does the sizeof keyword calcuates the size at compile time or run time ??
At compile time.

Sep 14 '08 #2
Rolf Magnus wrote:
CodeMonk3y wrote:
gotta question on sizeof keyword
For the OP, sizeof is an operator.
does the sizeof keyword calcuates the size at compile time or run
time ??

At compile time.
I haven't been keeping up with the changes coming to the standard. Are
variable-length arrays (currently in C99) being added? That's one case
where sizeof is a run-time construct, as the size of such an array
can't be determined at compilation. It's not something really needed in
C++, with std::vector and all.


Brian

--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Sep 14 '08 #3
Alf P. Steinbach wrote:
* Default User:
I haven't been keeping up with the changes coming to the standard.
Are variable-length arrays (currently in C99) being added?

No.
I thought it unlikely.
That's one case
where sizeof is a run-time construct, as the size of such an array
can't be determined at compilation.

It might be so in C99, but presumably if variable length array
support was added in C++ it would not be so in C++.
Er, what? How would you have C99-style VLAs that could be sized at
compilation?
>
It's not something really needed in
C++, with std::vector and all.

Current practice says otherwise. Several compilers support e.g.
_alloca, to do in an unsafe way what variable length array support
could do simply and safely.
Do they support that specifically for C++, or is it a holdover from C
extensions?

Brian

--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Sep 14 '08 #4
Alf P. Steinbach wrote:
* Default User:
Er, what? How would you have C99-style VLAs that could be sized at
compilation?

Why should C++ variable length array be C99-style?
Unless you bend the definition to where it's meaningless, then I don't
see how any array that can be dynamically created can yield its size at
compile time.
But anyway, given that, it would still be problematic in C++ to let
sizeof yield the dynamic size of such an array.

C++ has other means. In C++ you don't expect sizeof to e.g. yield the
dynamic size of a string. Similarly for dynamic arrays.
Not similarly at all. "Array" means something in both C and C++. You
expect sizeof an array to give you the size of the array. std::string
is a totally new thing, part of the selection of container classes.
Those include methods for determining the size.

Either the syntax and semantics of this "VLA" would have to be so
different as to call into question calling it an array, or you're going
to need some sort of dynamic sizeof operator. I'm not sure why you're
so resistant to the very idea.

I suspect that if VLAs ever do make it into C++, they will look a lot
like C99. Your opinion may vary.
[alloca]
Do they support that specifically for C++, or is it a holdover from
C extensions?

Neither. It's for both languages. See e.g.
http://docs.sun.com/app/docs/doc/819-2243/alloca-3c.
That tells you what it is, not why it's available in C++. It suspect
that it was created for C, and used in C++ pre-STL/std. I think it's
more a holdover than something they feel is specifically needed for C++
(if they even think about it much).


Brian

Sep 14 '08 #5
Alf P. Steinbach wrote:
* Default User:
Alf P. Steinbach wrote:
* Default User:
Er, what? How would you have C99-style VLAs that could be sized
at compilation?
Why should C++ variable length array be C99-style?
Unless you bend the definition to where it's meaningless, then I
don't see how any array that can be dynamically created can yield
its size at compile time.

In other words, you now see that your earlier notion was ludicruous.
No, I don't.
Could it be that you misunderstood something?
I'm not following.
Not similarly at all. "Array" means something in both C and C++. You
expect sizeof an array to give you the size of the array.

I do not expect what you write in the context of a more abstract
array.
I'm not even sure what this is trying to say.
And I think you do not expect sizeof(v) where v is std::vector to
yield dynamic size.
No, I don't. Why would I? A vector is not an array. It has a member
function for yielding the size. A array does not.
Yet std::vector is an array,
No, it is not. It is a standard container. Arrays are defined in the
standard already. Vectors are not arrays.

I now see that you are the one confused.


Brian
Sep 14 '08 #6
Alf P. Steinbach wrote:

You wrote "You expect sizeof an array to give you the size of the
array". That is false in general. It is only true for a very specific
narrow meaning of "array" (see below), which means that here you're
into terminology discussion.
Which is the definition of array used in C++.

There's a reason they didn't call it std::array.
>
And I think you do not expect sizeof(v) where v is std::vector to
yield dynamic size.
No, I don't. Why would I? A vector is not an array. It has a member
function for yielding the size. A array does not.

Consider what distinguishes "raw array" from "array".
Nothing, as there is no such thing yet.

At any rate, you have nothing to back up your statements. Knowing your
history, you aren't willing reasonably discuss these things, so I'll be
killing this thread. You can post your arguments to whomever else might
be interested.

Brian
Sep 15 '08 #7
Alf P. Steinbach wrote:
>
Hm, ending on a personal attack with insinuations & the works, plus
snipping what contradicts your earlier arguments.
I thought dummy spitting is what Usenet is all about :)

--
Ian Collins.
Sep 15 '08 #8
On Sep 14, 11:08 pm, "Alf P. Steinbach" <al...@start.no wrote:
* Default User:
Alf P. Steinbach wrote:
>That's one case
where sizeof is a run-time construct, as the size of such an array
can't be determined at compilation.
It might be so in C99, but presumably if variable length array
support was added in C++ it would not be so in C++.
Er, what? How would you have C99-style VLAs that could be
sized at compilation?
Why should C++ variable length array be C99-style?
But anyway, given that, it would still be problematic in C++
to let sizeof yield the dynamic size of such an array.
*IF* C++ were to add VLA, there are only two possible choices
with regards to sizeof: make it illegal on a VLA, or say that it
is not a constant if used on a VLA. Since the major argument in
favor of VLA in C++ is C compatibility, I suspect that the
latter would be chosen. But it's really just another question
which would have to be addressed. There are a certain number of
complexities with the way VLA's interact with classes as well,
and apparently, no one felt that they were important enough to
do the work necessary to integrate them into C++.
C++ has other means. In C++ you don't expect sizeof to e.g.
yield the dynamic size of a string. Similarly for dynamic
arrays.
>It's not something really needed in
C++, with std::vector and all.
Current practice says otherwise. Several compilers support e.g.
_alloca, to do in an unsafe way what variable length array support
could do simply and safely.
Do they support that specifically for C++, or is it a
holdover from C extensions?
Neither. It's for both languages. See
e.g.http://docs.sun.com/app/docs/doc/819-2243/alloca-3c.
Which doesn't mean that it isn't a hold-over from the stone
age:-). We have a lot of things which are the same for both
languages in that category.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 15 '08 #9
Alf P. Steinbach schrieb:
* James Kanze:
>On Sep 14, 11:08 pm, "Alf P. Steinbach" <al...@start.no wrote:
>>* Default User:
Alf P. Steinbach wrote:
>That's one case
>where sizeof is a run-time construct, as the size of such an array
>can't be determined at compilation.
It might be so in C99, but presumably if variable length array
support was added in C++ it would not be so in C++.
>>>Er, what? How would you have C99-style VLAs that could be
sized at compilation?
>>Why should C++ variable length array be C99-style?
>>But anyway, given that, it would still be problematic in C++
to let sizeof yield the dynamic size of such an array.

*IF* C++ were to add VLA, there are only two possible choices
with regards to sizeof: make it illegal on a VLA, or say that it
is not a constant if used on a VLA.

No, that's incorrect.

Instead of those silly, problematic choices, there is always the
rational choice, of letting sizeof do what it already does in all other
cases, providing the known compile time size (as for e.g. string and
vector).

It beats me how you could forget that.
He forget that because a C99 VLA doesn't have a "known compile time size".
> Since the major argument in
favor of VLA in C++ is C compatibility, I suspect that the
latter would be chosen.

Since that argument has been defeated (as well it should be, IMHO! :-)
), if VLAs were adopted it would presumably be due to some better
argument such as practical need demonstrated by existing practice using
unsafe alternatives.
But there already is a safe alternative, so there is little need for
another semi-safe alternative.

--
Thomas
Sep 15 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
4969
by: signuts | last post by:
I'm aware of what sizeof(...) does, what I would like to know is if sizeof(...) is compiled in or a function that's executed at run-time. Like for example { int a; printf("a is %d bytes\n", sizeof(a)); }
7
1943
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
18
21855
by: Kenneth Brody | last post by:
Is sizeof() legal within a #if preprocessor line? Specifically, I would like to use the following "sanity check" at compile time, but I'm getting an error on the compile about the #if line. struct foo { int a; int b; };
11
393
by: chandanlinster | last post by:
There is a sentence in the book "The C programming language" that says - "C provides a compile-time unary operator called sizeof that can be used to compute the size of any object." What does the phrase "compile-time unary operator mean?
20
2734
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to work on all implementation. size_t size_obj = (char*)(&obj + 1) - (char*)(&obj); I wanted to find out on which implementation this would fail. One of
3
1842
by: venkat | last post by:
Hi, I am new to c programming language. I heard that , sizeof can be made as run time operator. Even though it is a compile time operator. I am not sure also whether sizeof can be made as run time operator. If it can be made as run time operator, Please clarify me how to make it as run time operator?. what configurations have to do , to get this behavior. Please clarify my doubt. Thanks,
34
2078
by: Raman | last post by:
Hi All, Is ut possible to calculate size of any standard data types at run time i.e without using sizeof() operator Thanks and Regards, Raman
0
9993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10948
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10484
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8036
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4706
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.