Array initialization

J

Jo Siffert

Hi,

I have an array like
Foo **ppArr = new Foo*[ size ];

I would like to check if the array holds a pointer to an object at a
certain index using something like
if ( ppArr[ idx ] == NULL ) { ... }

However, this statement always evaluates to false, as ppArr[ idx ]
always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7).
Do I have to set all fields explicitly to NULL (using something like
UNIX's bzero()) to get this to work?

Thanks,
Jo
 
K

Karl Heinz Buchegger

Jo said:
Hi,

I have an array like
Foo **ppArr = new Foo*[ size ];

I would like to check if the array holds a pointer to an object at a
certain index using something like
if ( ppArr[ idx ] == NULL ) { ... }

However, this statement always evaluates to false, as ppArr[ idx ]
always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7).
Do I have to set all fields explicitly to NULL (using something like
UNIX's bzero()) to get this to work?
Yes.


Thanks,
Jo
 
J

JKop

Jo Siffert posted:
Hi,

I have an array like
Foo **ppArr = new Foo*[ size ];

I would like to check if the array holds a pointer to an object at a
certain index using something like
if ( ppArr[ idx ] == NULL ) { ... }

However, this statement always evaluates to false, as ppArr[ idx ]
always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7).
Do I have to set all fields explicitly to NULL (using something like
UNIX's bzero()) to get this to work?

Thanks,
Jo

Foo* &pArr = *(new Foo*[size]);

memset(pArr,0,sizeof(Foo*[size]));

delete [] &pArr;


Some-one may reply to this and say that setting padding bits to 0 is
undefined behaviour.

[Insert quote from Standard here]

-JKop
 
K

Karl Heinz Buchegger

JKop said:
Foo* &pArr = *(new Foo*[size]);

memset(pArr,0,sizeof(Foo*[size]));

delete [] &pArr;

I know that you have become a newborn fan of references, but in the
above the reference doesn't buy you anthing except adding
confusion. If you do dynamic allocation, catch the pointer as
what it is: a pointer.

Foo** pArr = new Foo* [ size ];
....
delete [] pArr;

This also will correct the problem you have when doing:

Foo* &pArr = *(new Foo*[size]);
pArr[0] = NULL;
delete [] &pArr;

The assignment
pArr[0] = NULL;
does *not* do what most programmers would expect it to do:
set the 0-th pointer to NULL. Instead it tries to assign NULL
to the object pointed to by the 0-th pointer.
 
A

Andrey Tarasevich

JKop said:
I have an array like
Foo **ppArr = new Foo*[ size ];

I would like to check if the array holds a pointer to an object at a
certain index using something like
if ( ppArr[ idx ] == NULL ) { ... }

However, this statement always evaluates to false, as ppArr[ idx ]
always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7).
Do I have to set all fields explicitly to NULL (using something like
UNIX's bzero()) to get this to work?

Thanks,
Jo

Foo* &pArr = *(new Foo*[size]);

memset(pArr,0,sizeof(Foo*[size]));

delete [] &pArr;


Some-one may reply to this and say that setting padding bits to 0 is
undefined behaviour.
...

More than that. Setting non-padding (value-forming) bits of a pointer to
0 doesn't necessarily produce the null-pointer value of corresponding
type. In other words, it is not guaranteed that elements of the array
will compare equal to 'NULL' after that 'memset'.
 
J

JKop

Andrey Tarasevich posted:
JKop said:
I have an array like
Foo **ppArr = new Foo*[ size ];

I would like to check if the array holds a pointer to an object at a
certain index using something like
if ( ppArr[ idx ] == NULL ) { ... }

However, this statement always evaluates to false, as ppArr[ idx ]
always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7).
Do I have to set all fields explicitly to NULL (using something like
UNIX's bzero()) to get this to work?

Thanks,
Jo

Foo* &pArr = *(new Foo*[size]);

memset(pArr,0,sizeof(Foo*[size]));

delete [] &pArr;


Some-one may reply to this and say that setting padding bits to 0 is
undefined behaviour.
...

More than that. Setting non-padding (value-forming) bits of a pointer to
0 doesn't necessarily produce the null-pointer value of corresponding
type. In other words, it is not guaranteed that elements of the array
will compare equal to 'NULL' after that 'memset'.

This I don't understand.

-JKop
 
K

Karl Heinz Buchegger

JKop said:
This I don't understand.

It's pretty simple once you know it.

When setting a pointer to 'doesn't point anywhere' we write

int* pPtr = 0;

Even if the above looks like assigning the bit pattern for 0 to a pointer
variable, it need not be so. A specific platform might use a completely
different bit pattern for describing: pointer to nowhere. Well, even if
a specific platform does that, we still write pPtr = 0, and the compiler
has to replace 0 with the bit pattern used at that platform.

Pointer_value_0 != bit_pattern_for_0

The compiler can do this, because it knows about pointers and this
special case. But memset() doesn't.
 
J

JKop

Karl Heinz Buchegger posted:
It's pretty simple once you know it.

When setting a pointer to 'doesn't point anywhere' we write

int* pPtr = 0;

Even if the above looks like assigning the bit pattern for 0 to a
pointer variable, it need not be so. A specific platform might use a
completely different bit pattern for describing: pointer to nowhere.
Well, even if a specific platform does that, we still write pPtr = 0,
and the compiler has to replace 0 with the bit pattern used at that
platform.

Pointer_value_0 != bit_pattern_for_0

The compiler can do this, because it knows about pointers and this
special case. But memset() doesn't.

Interesting! I'm assuming this is in the Standard, yes?


Similarly, if you write:

int* jk; //Global variable

It may get 0, or it may get whatever is supposed to be a NULL pointer. That
right?


-JKop
 
K

Karl Heinz Buchegger

JKop said:
Karl Heinz Buchegger posted:


Interesting! I'm assuming this is in the Standard, yes?

Yes. of course
But it's not written down that way. It follows from some rules that
turn around null pointer values, null pointer constants and their
required behaviour.
Similarly, if you write:

int* jk; //Global variable

It may get 0, or it may get whatever is supposed to be a NULL pointer. That
right?

It may be everything. No initialization -> undefined (except in static
cases. God how I hate those exceptions everywhere:

static int i;

i has a value of 0. Even without initialization. It's a leftover from C)
 
J

JKop

Karl Heinz Buchegger posted:
It may be everything. No initialization -> undefined (except in static
cases. God how I hate those exceptions everywhere:

static int i;

i has a value of 0. Even without initialization. It's a leftover from
C)


int* jk;


Mine was a global variable. Global variables get initialized to 0, no?

If so, does a global pointer get initialized to NULL, which may or may not
be 0?


-JKop
 
K

Karl Heinz Buchegger

JKop said:
Karl Heinz Buchegger posted:


int* jk;

Mine was a global variable. Global variables get initialized to 0, no?

Aehm. ... thinking ... I think the answer is yes. (Not so sure
if I have mixed up static with global variables right now)

I write explicite initializations everywhere and don't have to remember
all those exception rules :)
If so, does a global pointer get initialized to NULL, which may or may not
be 0?

yes.

Same with double.
Noboby says that the bit pattern for 0.0 equals all bits zero.
 
A

Andrey Tarasevich

JKop said:
Karl Heinz Buchegger posted:



int* jk;


Mine was a global variable. Global variables get initialized to 0, no?

If so, does a global pointer get initialized to NULL, which may or may not
be 0?
...

Hmm... You are a bit confused. When it comes to pointer initialization,
NULL acts the same way as literal '0' (or, more precisely, an integral
constant expression that evaluates to zero). So in this context it is
safe to say that NULL _is_ 0.

A pointer of type 'int*' with static storage duration gets implicitly
initialized to null-pointer value (NPV) of type 'int*'. This is not
exactly what NULL is. NULL is universal null-pointer constant (NPC)
(just like literal '0', for example). When NPC gets converted to certain
pointer type 'T*', it turns into NPV of that type. Different pointer
types might use completely different representations for their NPVs.
NPVs are not required to be represented by "all-zeroes" bit pattern.
That's why 'memset' is not guaranteed to produce NPVs in the original
example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top