 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
dragoncoder Guest
|
Posted: Thu Dec 01, 2005 4:35 pm Post subject: Is sizeof() evauated at compile time ? |
|
|
Okay, here is the code.
#include <cstdlib>
#include <iostream>
int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
2. Is sizeof () evaluated at compile time ? I want to know if the code
produces undefined behaviour.
Thanks
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Chris Uzdavinis Guest
|
Posted: Fri Dec 02, 2005 12:33 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
| Quote: | Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
|
No. Macros are not in namespaces.
| Quote: | 2. Is sizeof () evaluated at compile time ? I want to know if the code
produces undefined behaviour.
|
sizeof() is evaluated at compile time, and its evaluation does not
consider values, only types. Your code is fine. One common "trick"
people do is take the sizeof() a function invocation expression, which
gives the size of the return type of the function without actually
calling it.
--
Chris
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
peter steiner Guest
|
Posted: Fri Dec 02, 2005 12:33 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
dragoncoder wrote:
| Quote: | Okay, here is the code.
#include
#include
int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
|
NULL is defined to be a macro, thus namespaces do not apply. (C.2.2.3)
| Quote: |
2. Is sizeof () evaluated at compile time ? I want to know if the code
produces undefined behaviour.
|
yes, sizeof is (kind of) evaluated at compile time.
if sizeof is given an expression it just computes the resulting object
size. the expression is not evaluated in the sense of pointer
dereference that you are worrying about.
your sizeof operator call is legal and directly translates to
sizeof(int). (5.3.3)
-- peter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Razzer Guest
|
Posted: Fri Dec 02, 2005 12:37 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
dragoncoder wrote:
| Quote: | Okay, here is the code.
#include
#include
int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
|
No. NULL is a marco. It *cannot* have a std:: prefix.
| Quote: |
2. Is sizeof () evaluated at compile time ? I want to know if the code
produces undefined behaviour.
|
Yes. Your code is well-formed.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
berad Guest
|
Posted: Fri Dec 02, 2005 12:38 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
no you don't have to put std:null, and no its evaluated at runtime, at
least that would make sense to me
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Fri Dec 02, 2005 12:52 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
On 1 Dec 2005 11:35:55 -0500, "dragoncoder" <pktiwary (AT) gmail (DOT) com>
wrote:
| Quote: | Okay, here is the code.
#include
#include
int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
|
NULL is a macro defined by some compilers. It is not part of the ANSI
standard C++ library, and therefore it is not in namespace std. You
should actually write:
int *ptr = 0;
instead of NULL. Bjarne Stroustrup, among others, recommends this
(see: "The C++ Programming Language", chapter 5, section 5.1.1).
| Quote: | 2. Is sizeof () evaluated at compile time ? I want to know if the code
produces undefined behaviour.
|
sizeof() is indeed evaluated at compile time. Because the compiler
knows that ptr points to int, it knows the size of *ptr even if it
hasn't yet been set to point to a valid object. Somewhere in the
standard it says that no expressions in the sizeof() statement are
actually evaluated, hence there are no side effects by writing *ptr
here. I might be wrong, but I believe the behavior of your code is
well-defined.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Clark S. Cox III Guest
|
Posted: Fri Dec 02, 2005 12:53 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
On 2005-12-01 11:35:55 -0500, "dragoncoder" <pktiwary (AT) gmail (DOT) com> said:
| Quote: | Okay, here is the code.
#include <cstdlib
#include
int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
|
No. NULL is a macro. Macros are simple text substitution, and don't
obey scoping rules.
| Quote: | 2. Is sizeof () evaluated at compile time ?
|
Yes it is.
| Quote: | I want to know if the code produces undefined behaviour.
|
sizeof only relies on the *type* of its operand, never its *value*.
sizeof doesn't evaluate its operand.
your program will produce the same output as:
#include
int main() {
std::cout << sizeof (int) << std::endl;
return 0;
}
--
Clark S. Cox, III
[email]clarkcox3 (AT) gmail (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Fri Dec 02, 2005 12:54 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
"dragoncoder" <pktiwary (AT) gmail (DOT) com> writes:
| Quote: | #include <cstdlib
#include
|
std::endl is declared in
as well.
| Quote: | int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
|
No. NULL is a macro and therefore doesn't belong to a namespace.
| Quote: | 2. Is sizeof () evaluated at compile time?
|
Yes.
| Quote: | I want to know if the code produces undefined behaviour.
|
I don't think it does. The only problem is the lacking #inclusion of
to work fine (if <iostream> happens to #include <ostream>).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
gottlobfrege@gmail.com Guest
|
Posted: Fri Dec 02, 2005 12:54 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
| Quote: | Is sizeof() evauated at compile time ?
|
Yes.
dragoncoder wrote:
| Quote: | Okay, here is the code.
#include
#include
int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
|
IMO, just use 0 instead of NULL.
| Quote: | 2. Is sizeof () evaluated at compile time ? I want to know if the code
produces undefined behaviour.
|
Interesting question. Technically, by the wording of the standard, it
might be arguable, but I will bet that you are fine for all
implementations.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Fri Dec 02, 2005 12:55 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
dragoncoder wrote:
| Quote: | #include
#include
int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
|
No. NULL is a macro.
| Quote: | 2. Is sizeof () evaluated at compile time ?
|
Yes.
| Quote: | I want to know if the code produces undefined behaviour.
|
No, I believe it's well defined - it should output whatever the size of
an int is on your system.
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Clark S. Cox III Guest
|
Posted: Fri Dec 02, 2005 11:43 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
On 2005-12-01 19:33:52 -0500, "peter steiner" <pnsteiner (AT) gmail (DOT) com> said:
| Quote: | dragoncoder wrote:
2. Is sizeof () evaluated at compile time ? I want to know if the code
produces undefined behaviour.
yes, sizeof is (kind of) evaluated at compile time.
|
What do you mean by "kind of"? I thought that, in C++ (and in C, if you
discount C99's VLA's) sizeof always results in a compile-time constant.
--
Clark S. Cox, III
[email]clarkcox3 (AT) gmail (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Clark S. Cox III Guest
|
Posted: Fri Dec 02, 2005 11:44 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
On 2005-12-01 19:54:02 -0500, Thomas Maeder <maeder (AT) glue (DOT) ch> said:
| Quote: | "dragoncoder" <pktiwary (AT) gmail (DOT) com> writes:
#include <cstdlib
#include
std::endl is declared in
as well.
|
Every single code example using endl in the standard includes
<iostream>, and not <ostream>
--
Clark S. Cox, III
[email]clarkcox3 (AT) gmail (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Fri Dec 02, 2005 11:46 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
On 1 Dec 2005 19:52:40 -0500, Bob Hairgrove <invalid (AT) bigfoot (DOT) com>
wrote in comp.lang.c++.moderated:
[snip]
| Quote: | Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
NULL is a macro defined by some compilers. It is not part of the ANSI
standard C++ library, and therefore it is not in namespace std. You
|
NULL is a macro defined by specified headers in every single
conforming C and C++ compiler. Macros defined in headers are
considered part of the library.
[snip]
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Fri Dec 02, 2005 11:49 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
Razzer wrote:
| Quote: | dragoncoder wrote:
Okay, here is the code.
#include <cstdlib
#include
int main() {
int* ptr = NULL; // Do I have to put std::NULL ?
std::cout << sizeof (*ptr) << std::endl;
return 0;
}
Questions:
1. Do I have to write std::NULL ? As I know all standard library names
are defined in std namespace.
No. NULL is a marco. It *cannot* have a std:: prefix.
|
That is not true in general. Assert also is a macro, but depending
on your compiler compliance and whether you included
versus <assert.h> std::assert can be wellformed. The same reasoning
applies to std::errno and others. The reason is clear, of course:
Whether a namespace-specifier is needed, depends on the way of macrofying..
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hrayr BABAJANYAN Guest
|
Posted: Fri Dec 02, 2005 11:50 am Post subject: Re: Is sizeof() evauated at compile time ? |
|
|
Hi!
1. No!
NULL is a macro defined like
#define NULL 0 // or
#define NULL ((void*)0) // macros are evaluated by
preprocessor
// and the name
resolutions at copile time
// so there is no
std::NULL (in std namespace)
2. Yes!
operator sizeof is being evaluated at compile time, it computes
something like 'typeof'
for the expression inside ()s (the type of the expression), and
then computes the
size of that type.
well the code listed looks ok, no undefined behaviour, the size
of expression *ptr is
defined at compile time (depends on the platform you use).
Cheers!
Hrayr
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|