# Dynamic new..

This is a discussion on Dynamic new.. within the C++ Programming forums, part of the General Programming Boards category; Hi, can someone please clarify this...pointers always...grrr anyhow int *a= new int[9] ; creates an array of pointers, each pointer ...

1. ## Dynamic new..

Hi, can someone please clarify this...pointers always...grrr
anyhow

int *a= new int[9] ;

creates an array of pointers, each pointer pointing to an integer in memory

int *a = new int*[9] ;

should create an array of pointers, each pointer pointing to a pointer in memory..it wouldn't let me do the declaration above?

I have to do int **a for it to work..Is this because it is a pointer to a pointer?
I always treat DMA as a normal array because it is easier..but I guess there are always exceptions... :S

2. If it's a dynamic 2D array, then obviously you need the later. Because a (the first) behave like an array, the first subscript leads directly to the integer. But in a 2D array, each of the subscripts lead directly to another array.

So anyway, why do you need int** instead of a vector?

3. Yeah you could do vector<int*> instead... or vector<int> instead of new int[] and then do the x * DIM_X + y calculation yourself. Either way, if you want to flatten a 2-d array like that, you have to calculate a one dimensional subscript.

4. Hi Elysia..
I am confused lol..
if i do this
int **a = new int*[10]

the first set of array is the array of pointers that new returns..the second set would be the int * pointers ..am I right in thinking that?

i need it to implement graph representation..

5. int **a = new int*[10];

That would be the first dimension, but you still have to allocate all of the second dimension which is what the first dimension points to. Or you can make it easier by using the flattened layout I talked about.

6. xD i can like comments...haha
yo whiteflag...
em, yeah our lecturer was saying thats inefficient to do.
what I am trying to do is create an array of Node pointers that will point to connected nodes..

so I have
Node **a = new Node*[10]
and that will point to a connected node..and that in turn will point to another node a[] is connected to..
since Node **a = new Node*[10] is originally a 1dimensional arrays of pointers..
i wasn't fully sure (and really still not) why i needed the double *

7. Your lecturer is an idiot if he/she thinks it's inefficient.
std::vector<std::vector<int>> myvec(std::vector<int>(second_dimension), first_dimension);
There is your 2D array. Even better with boost.

8. Oh, well that's different. Your Node* type is the type of array element, so the regular base pointer requires another star. As long as your Node* elements are treated like single nodes, the array is still flat, but if a[x] is actually a list of nodes it's more like a matrix. Saying that the elements are still connected doesn't really help me tell you what's what. It depends on your usage of the Node* element.

And there is a problem with vector< vector<int> >... it's not going to keep things rectangular, if you push to much or too little. So if a rectangular grid is what's important and you still want to avoid new, use something like vector< tr1::array<foo> >, I guess. Anything that forces reallocation of the whole matrix when the second dimension gets extended is good.

9. Figure 4 is what I am trying to implement
Part 5: From Trees to Graphs
does it really matter what type the array is?
The int*[], Node*[] dynamic array is still flat..its almost like saying int[4] or Node[9] except it is pointers... and as Elysia said..the subscripts directly access the values...so the declaration should work.

OK..i will try this

int **a = {0x40, 0x50,0xdF} ;

a[0] accesses 0x40..this is the first star...a[0][0] the second subscript, the second * refers to the memory location that 0x40 points to.... :S

10. Code:
`int ** a= new int * [10];`
This means that a is a double pointer that can only points to the single pointer. So, in the above written line, you've created a dynamic array of pointers (single) through a double pointer.
Now what you've to do is to;
create an array that will be pointed by your single pointers pointed by your double pointer (a).

Code:
```int ** a=new int * [10];

for(int i=0;i<9;i++){
a[i]=new int[size_of_your_array];
}```
That will allow you to create arrays.

11. em..right
sorry..I have to get it right in my own words..

new int *[10]

creates a 1 dimensional array of pointers having 10 elements.
each of this elements can have subsequent dynamic arrays allocated to it...as in, it can point to a new allocated 1d array or a predefined one.

each of the elements could also point to normal variables like
int d;
a[0] = &d

..so the first* refers to the pointer that accesses the return addresses from the new operator..and the second * refers to the address of &d, the address pointed to by the return pointers from d

by the way, to clarify maybe I am having the whole thing upside down

does int* a = new int[10]

return only one pointer to an array of 10 integers

or an array of 10 pointers to 10 integers?

12. Originally Posted by Eman
by the way, to clarify maybe I am having the whole thing upside down

does int* a = new int[10]

return only one pointer to an array of 10 integers

or an array of 10 pointers to 10 integers?
It returns a pointer to the first element of an array of 10 int.

13. yo Llight cool..i knew I was thinking about it the wrong way around..

so the first start refers to the dereference of the address that a contains
*a or a[0] which is the address of the first element of some array

and the second star refers to the dereference of the address that the first element of that array contains...

...warmer?

14. Here is how it looks like (where p is your pointer):

15. sweet Elysia..that should pretty much clear it up..
I am not sure about the graph representation algorithm..i will create a different thread for that to make it neater..
first I will try and solve it.
danke much