Click here to Skip to main content
15,943,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a quick question. Sorry it is a bit of a newbie question so forgive me if it is obvious. I want to have a two dimensional dynamic array in C++ so I'll code something like this:-

VERTS** verts1; //[1]


VERTS*(*verts1); //[2]

What I'm thinking is that the expression [1] is an array of pointers to pointers. But what is [2]??? Someone said that the brackets make a difference. Can someone explain this???

What about memory allocation - I have the following:-

verts1 = new VERTS*[num_edges];
verts1[current_edge] = new VERTS[num_columns];

When I deallocate I have:-

if (verts1!=NULL)
    for (int i=0; i<num_edges; i++)
        delete [] verts1[i];
    delete [] verts1;

Can you please verify this allocation and deallocation. I'm pretty sure it's okay but what if I use expression VERTS*(*verts1); Does this change anything???

Also, sometimes I want to deallocate a specific row so have something like this:-

delete [] verts1[current_edge];

This is wrong isn't it? Don't I need to loop over all the elements like this:-

for(int i=0; i<num_columns;>{
    delete verts[current_edge][i];

Hopefully, someone can throw some light on this as I'm a bit confused. Once again, sorry if this is a silly question.

Kind regards...

C++ grammar is known to be undecidable (what a line does depends on the definition preceding it,so my guess may be not true).

verts** verts1

Is a declaration of a variable that - if dereferenced twice - will result into a verts value. So it is a "pointer to pointer to verts"


is the creation of a temporary object of type verts*, initialized with the pointer returned by *verts1.
(think to code like point(2,3);).

Allocation and deallocation are conceptually fine.
But -in fact- you're doing exacly what std::vector<std::vector<vertexs> > does by itself behind the scene.
Share this answer
Aescleal 5-Jan-11 13:41pm    
Good catch about the second question (the declaration of the temporary), everyone else ignored that!
Agree with Aescleal, you are using C++, be happy !
boost has a library designed for multidimensional array :[^]
Share this answer
Why are you doing all the memory management yourself? std::vector is your friend and just as efficient as messing about with new/delete everywhere.

So instead of having pointers to pointers have a vector of vectors:

std::vector<std::vector<vert> > vertices;


Share this answer
#1 is fine (ignoring the memory performance issues due to poor locality of reference by not allocating a contiguous block), but you should set verts1=0 after deallocation for good practice. If you deallocate a particular row only, then you also should set it to null afterwards if you are not immediately replacing it.
Share this answer
Thanks folks for your responses. I've changed my code to include vector's and everything looks good. I was a little worried because there is a slight overhead compared to standard arrays and I'm doing a 3D algorithm where speed is critical. However, there is no noticeable speed difference.

Also, I was thinking about the performance issues mentioned by Ted2102. He points out that repeated calls to new and delete is going to fragment the memory so that all my array data will no longer be in a contiguous block of memory.

What I'd like to know is whether vector suffers from any of these problems? E.g. if you are using vector's of vectors and then repeatedly resize your vectors, will it suffer from the same fragmentation problem???? I'm guessing it will unless there something I'm not aware of.

Anyway, thanks a lot everyone for your help. All the posts were very helpful.

Share this answer
Aescleal 5-Jan-11 13:41pm    
Using a vector is almost always going to be more efficient than manually fiddling around with new and delete EXCEPT for memory consumption - manual memory management may be better if you're on a tight memory budget. Fragmentation is unlikely to be an issue with a vector of vectors as std::vector overallocates memory to avoid having to reallocate and move blocks quite so much when the vector expands.

Incidentally You can control how much memory vector uses by using the reserve member function. Again only use that if you need more precise control over sizing and reallocation.



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900