473,473 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array initialization

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
Jul 22 '05 #1
12 4021
Jo Siffert wrote:

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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
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
Jul 22 '05 #3
JKop wrote:


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
In article <b8**************************@posting.google.com >,
jo********@gmx.net (Jo Siffert) wrote:
I have an array like
Foo **ppArr = new Foo*[ size ];


Use vector<Foo*> and all problems brought up in the discussion go away.
Jul 22 '05 #5
JKop wrote:
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'.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6
Andrey Tarasevich posted:
JKop wrote:
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
Jul 22 '05 #7
JKop wrote:
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.


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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8
Karl Heinz Buchegger posted:
JKop wrote:
> 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.


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
Jul 22 '05 #9
JKop wrote:

Karl Heinz Buchegger posted:
JKop wrote:

> 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.
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?


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)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10
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
Jul 22 '05 #11
JKop wrote:

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?


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #12
JKop wrote:
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?
...


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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #13

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

Similar topics

2
by: Fred Zwarts | last post by:
If I am right, members of a class that are const and not static must be initialized in the initialization part of a constructor. E.g. class C { private: const int I; public: C(); };
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
19
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
3
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value...
5
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
15
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
2
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.