Jump to content
  • Advertisement
Sign in to follow this  
ToohrVyk

C++ null pointers

This topic is 4307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C, the following code is not guaranteed to set the pointers a and b to NULL:
typedef struct { int *a, *b; } S;

int main() {
  S s;
  memset(&s,0,sizeof S);
}
Does this problem also exist in C++, or is the C++ standard also unspecific about NULL pointer representation?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Quote:
In C, the following code is not guaranteed to set the pointers a and b to NULL:

Didn't know that, whats the reason do you know?, or have a link to an example?
I would have thought setting the memory to 0 would work.

Share this post


Link to post
Share on other sites
Quote:
Original post by QSlash
It shold be set to 0 both in c and c++.but the direct way is: S s ={0};


1° I know that it is set to 0. My question is, is it set to null?
2° For the purposes of the problem, let us assume that memory is set to zero, as opposed to initialized with zero.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks for the link ToohrVyk thats interesting.
Have you looked at the C++ standard?

Share this post


Link to post
Share on other sites
I would have, but my dead-tree-paste version is at home right now, so I don't have access to it, and it isn't legally available online as far as I know.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
NULL is either defined as (void*)0 or 0, so it IS null.

Share this post


Link to post
Share on other sites
Quote:

I would have, but my dead-tree-paste version is at home right now, so I don't have access to it, and it isn't legally available online as far as I know.

Isn't it? I found it available online in pdf format a while back. Was that illegal?
*feels guilty*

Share this post


Link to post
Share on other sites
Quote:
NULL is either defined as (void*)0 or 0, so it IS null.

Completely false, (void*)0 is not valid and other representations is valid.
Quote:
The macro NULL is an implementation-defined C++ null pointer constant in this International Standard(4.10).180)

180) Possible definitions include 0 and 0L, but not (void*)0.


The interesting part of the C++ standard is:
Quote:
A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4).

I can't see it make any claims that a null pointer is guarenteed to be 0, it's guarenteed to evalute to 0, but its representation can be different. There may be something in the C++ standard which implicitely states that the representation is guarenteed to be 0, but I haven't heard about such a thing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!