views:

693

answers:

2

I am tring to intialize an array of pointers to a NODE struct that I made

struct Node{
    int data;
    Node* next;
};

the private member of my other class is declared as

Node** buckets;

It is currently initialised as buckets = new Node*[SIZE]

Is there anyway to initialize the array so that its members point to NULL or some other predefined Node pointer?

EDIT: Im looking for a means to initilize it without trying to generate a for loop to traverse through the full lenght of the array. The size of the array is determined at runtime.

EDIT 2: I tried std::fill_n(buckets, SIZE_OF_BUCKET, NULL); but the compiler gives the error "cannot convert from 'const int' to 'Node *'" I am using visual studio 2008. Is there something wrong that I am doing?

+1  A: 

If you mean by array your array-of-pointers buckets, then yes. Just set buckets = NULL.

Edit: based on your question edit, just use memset.

memset(buckets, 0, NUM_ELEMENTS_IN_BUCKETS*sizeof(Node*));
Shaggy Frog
sizeof(anypointer) does not always equal 4, these days
1800 INFORMATION
technically, using memset to set a pointer to 0 is not the same as setting it to NULL. The standard says that the 0 constant used in a pointer context is the null constant. However, this is setting the actual bit-pattern of the pointer to 0, which is not neccessarily the null constant.
Evan Teran
I removed the incorrect side not that sizeof(any pointer) would be always 4.
peterchen
@Evan I believe in C++ that the null pointer is always 0... no?@others yes oops... I still live in a 32-bit world ;)
Shaggy Frog
@Shaggy, the constant 0 is the null constant, but that is not the same as a 0 bit-pattern. There are several architectures which have a non 0 bit-pattern for NULL. (However, the null constant is still 0 in C++ regardless of this fact).
Evan Teran
@Shaggy: see http://stackoverflow.com/questions/398883/how-to-set-pointer-to-a-memory-to-null-using-memset
Evan Teran
The null pointer constant (`0`, `NULL`) is not the same as the null pointer. The latter is what is not necessary bit-wise 0 - not the former. And the latter is what you are trying to generate.
Johannes Schaub - litb
Yea, that's what I meant. I mis-spoke when I wrote "...non 0 bit-pattern for NULL". I meant "...non 0 bit-pattern for the null pointer"
Evan Teran
Ah, i see. Alright see my pet-peeve maybe you like it http://stackoverflow.com/questions/423823/whats-your-favorite-programmer-ignorance-pet-peeve/1331729#1331729 xD tell me what u think about it =)
Johannes Schaub - litb
It is definitely a point of much confusion for many. I hope that the upcoming nullptr will finally bury the issue.
Evan Teran
+6  A: 

First of all, the simplest solution is to do the following:

Node** buckets = new Node*[SIZE]();

As litb previously stated, this will value initialize SIZE pointers to null pointers.

However, if you want to do something like Node **buckets and initialize all of the pointers to a particular value, then I recommend std::fill_n from <algorithm>

Node **buckets = new Node*[SIZE];
std::fill_n(buckets, SIZE, p);

this will set each Node*' to p after allocation.

In addition, if you want the Node to have sane member valuesupon construction, the proper way is to have a constructor. Something like this:

struct Node {
    Node() : data(0), next(NULL){}
    Node(int d, Node *n = NULL) : data(d), next(n) {}

    int data;
    Node* next;
};

That way you can do this:

Node *p = new Node();

and it will be properly initialized with 0 and NULL, or

Node *p = new Node(10, other_node);

Finally, doing this:

Node *buckets = new Node[N]();

will construct N Node objects and default construct them.

Evan Teran
Alright i deleted my answer now that you have this thing integrated and because your answer is older. Why not move that line to the very top so people read it first?
Johannes Schaub - litb
Good call, done.
Evan Teran
I tried std::fill_n(buckets, SIZE_OF_BUCKET, NULL); but the compiler gives the error "cannot convert from 'const int' to 'Node *'" I am using visual studio 2008
Jaelebi
If you pass `NULL` to `fill_n`, you'll need to type-cast it since the compiler will deduce the type to be `int` rather than `pointer`: `fill_n(buckets, SIZE, static_cast<Node*>(NULL))`. You could also specify the template parameters explicitly, but that gets ugly since the parameter you want is the third, so you need to include the first two as well, even though the compiler can deduce them without your hints: `fill_n<Node**, int, Node*>(buckets, SIZE, NULL)`.
Rob Kennedy
That works. But I still get a warning "warning C4996: 'std::fill_n': Function call with parameters that may be unsafe "
Jaelebi
Try using 0 instead of `NULL`. `NULL` is a C-ism, C++ uses 0. (C++0x uses `null_ptr` or something)
kibibu
Using `NULL` is safer when using GCC. In GCC, `NULL` is defined to be `__null` instead of 0. Technically, that's non-compliant but in general you'll never know and it allows GCC catch silly errors like `int ptr = NULL;`. It's basically like c++0x's `nullptr`.
caspin