Allocating an array of pointer??

Hi,
just a checking question, I'd like to allocate an array of pointers, as you would do in C with:
Node** a = calloc(n, sizeof(Node*));

using the new operator in C++ is it correct the expression:
Node** a = new Node*[n] ?

Thanks for any consideration
- Atari
That will do it, yes.
But maybe you should look at std::vector

(¿why pointers, btw?)
kind of finding hard to adapt to references... pointers have the special value NULL that helps me to check if an object is present or unavailable. So next question would be if the new[] operator initialises all pointers to the NULL value?

regards
- Atari
Last edited on
closed account (zb0S216C)
Why not a reference to a pointer? Here's a piece of code that exemplifies such a thing:

1
2
3
4
5
6
7
8
9
10
int *pointer(nullptr);

void routine(const int *&ptr)
{
    if(!ptr)
        // ...
}

// The invocation:
routine(pointer);


Wazzak
Last edited on
My reply won't answer your question, dean.

You needn't reinvent the wheel. Use the containers that the STL provides.
http://www.cplusplus.com/reference/stl/

ne555's suggestion was roughly std::vector<Node *> if you really want to go that way.
Last edited on
declaring an array of pointers is simple.

int* ptrArray[10];

This is an array of 10 int pointers. They are NOT set to NULL by default.


To do it dynamically you write:

int** ptrArray = new int*[10];

In this case, the pointers are set to NULL;



It's really easy to understand this stuff by just playing around. Just declare them and cout them. If you see an address, you know it's not set to NULL.
Last edited on
To ensure that arrays are nicely zero-ed

1
2
3
4
// local stack variables

int* ptrArray[10]; // filled with random stuff / debug fill pattern
int* ptrArray[10] = {0}; // nicely zeroed (curly {}s) 


1
2
3
4
// heap variables

int** ptrArray = new int*[10]; // filled with random stuff / debug fill pattern
int** ptrZeroedArray = new int*[10](); // nicely zeroed (rounded ()s) 
Last edited on
thanks for your answers. Just a couple of points:

@framework/wazzak wow! I am pretty baffled that it is possible to have something like that in C++. I supposed that references were no more than aliases for the real variable. But thinking about, what would happen if you create an array of references? For instance I know that (at least in C) if you create an array of 10 integers, you are allocating a contiguous memory block that has enough space to hold 10 integers. Now, if you make an array of references of integers, I presume that the only manner to keep a contiguous memory block is through the use of pointers, and therefore the way you access items in your code is merely syntactic sugar hidden by the compiler. In the end an array of pointers is exactly the same as an array of references. But I am confused what makes an array of pointers+references?

@IceThatJaw and andywestken I think your answers contrast, but if I understood well invoking the default constructor of a basic type like an integer or a pointer will always assign the value 0 ? and new[] invokes the empty constructor?

regards
- Atari

Dean, I am pretty green but I actually tested what I said.

Now, I am educated enough to know that when I got all zero's from int** ptrArray = new int*[10];, it could have been some garbage picked up from the heap.

Shit like that happens alot and makes debugging even more frustrating. I would probably take andywestken's word over mine.



And string is a C++ STL class so it is always set to logical null upon creation. Integers however are not unless they are defined outside of a class or struct, I believe.

Also, if you are declaring an array of objects such as classes, the default constructor must be available because it is the only constructor that can and will be called. ( you will get a compiler error if it isn't)
Last edited on
Topic archived. No new replies allowed.