C++ null pointers

Started by
23 comments, last by ToohrVyk 17 years, 6 months ago
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?
Advertisement
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.
Several machines use non-zero null pointers. Also, see this FAQ.
It shold be set to 0 both in c and c++.but the direct way is: S s ={0};
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.
Thanks for the link ToohrVyk thats interesting.
Have you looked at the C++ standard?
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.
NULL is either defined as (void*)0 or 0, so it IS null.
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*
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.

This topic is closed to new replies.

Advertisement