Dynamically Allocated Arrays

I’m working on a .ase loader for loading my models… I’m not all that great with C++, but im trying… Anyhow, i have read in an “int index”, which is the number of verticies that are going to be read in next, I now need an array of array[index][3], for the x,y,z of each vertex… Anyway, i dont know how to make it use “index” as a declarer for the size of the array…
Can anyone help me out on this? If you could, please post an example…
Thanks in Advance.
ImpactDNI

There are two ways of managing dynamic memory, only one of which I can do effectively, its actually very easy. Look at the example below:

int x;
cin >> x;
char *data;
data = new char [x];
for(int i = 0; i<x; i++) {
data[i] = “A”;
}
cout << data << endl;
delete data[];

The only line I’m not sure of being correct is the last one, being delete data[] but I’m 50% sure thats correct. Look up new and delete, they rule! Isn’t that easy though?

Not to be picky, but it would actually be

delete [] data;

how sure are you about that? don’t the [] always come after the variable name? What’s the sense in that?

Please explain, thanks a lot

I’m 100% sure of that.

delete and delete [] are different “operators.”

You’re right that the brackets always come after the variable name when defining the array, but the delete [] operator is a special case where they come first.

Part of the reason they did it that way I think, was because if you have something like so…

char** ppChArray = new char*[10];
for (int c=0;c<10;c++) ppChArray[c] = new char[10];

// now delete
for (int c=0;c<10;c++)
{
delete [] ppChArray[c]; // seems cleaner somehow than delete ppChArray[c][]
}

delete [] ppChArray;

[This message has been edited by Deiussum (edited 11-22-2002).]

Yah I guess your right. So what do you do when you wanto delete one of the dimensions of the array x[y]? Is it delete [] x[]?

WOW!!! lol, thats so helpful, i love this board, you guys rock! I’ve seen the new and delete commands, but never used them… again, this helps a ton, and you guys rule, (im out of school for the weekend) but if i have any more trouble getting this to work, ill be sure to post back!
Thanks again!
ImpactDNI

Originally posted by 31337:
Yah I guess your right. So what do you do when you wanto delete one of the dimensions of the array x[y]? Is it delete [] x[]?

You’d have to know which element you want to delete. It would be

delete x[nIndexToDelete];

and remember, if you have an array:

buf[x]

and you use this:

delete buf;

without the [], this will only delete the first entry in the array.

jebus

Using delete on an array allocated with new[] is undefined behaviour. Most compilers I know of will release the whole memory block though, but don’t count on it working properly.

I usually don’t use delete on arrays, so I really should have verified my statement above before posting. I use MSVC 6 and my program craches when I use delete on an array allocated with new[]. So I suppose I know at least one compiler that can’t handle new/delete mismatches. But nothing wrong with that, since it’s undefined behaviour.

Alright, well, im back in school, and im working on gettin this to work
But the problem is, im in a class…
its “p” is the class… and p->numVertex is the index i want (number of vertexes), and i want p->verts[index][3] to be the array… so i have “index” number of verticies, and each have an x,y,z… how do i dynamically allocate within a class?

class foo
{
public:
int numVerts;
float (*verts)[3];
};

foo *p = new foo;
p->numVerts = 10;
p->verts = new float[p->numVerts][3];

delete p->verts;

The parentheses around *verts in the class is important. Without them, you get an array of three float pointers. You want a pointer to a float array with three elements.

p->verts[i] will return the address of the i:th vertex. To pass the vertex to OpenGL, use either of these two calls.

glVertex3fv(p->verts[i]);
glVertex3f(p->verts[i][0], p->verts[i][1], p->verts[i][2]);

Or vertex arrays of course.

On a related note I know that if I forget to deallocate memory it will stay in the memory and this is bad. Can I check that I have deleted all my allocaed stuff. The way I have done it in Pascal is to print the amount of memory free before and after my program has run, can I do this or something similar in C++?

fringe

C++ does not provide any functions to check the memory. You will have to use platforms specific functions. Check out your platform API.

You may find Paul Nettle’s memory manager interesting. It overrides default new / malloc / delete / free methods and generates a report when the apps terminates to show if there are memory leaks and where they are.

Official page: http://www.fluidstudios.com/publications.html (look for MMGR (the other links seem really interesting indeed ))

As you are doing C++,I would do this with STL :

#include <vector>

using std::vector;

typedef struct {
float x,y,z;
} Vec3;

main() {
vector<Vec3*> array;

int index = readAseIndex();

for( int i=0; i<index; i++ ) {
Vec3* point = new Vec3;
point->x = readXfromAse();
point->y = readYfromAse();
point->z = readZfromAse();
array.push_back(point);
}

vector<Vec3*>::iterator it = array.begin();

for( ; it != array.end; it++ ) {
Vec3* point = *it;
cout << " x : " << point->x
<< " y : " << point->y
<< " z : " << point->z
<< endl;
}
}

see http://www.sgi.com/tech/stl/ for stl reference.

have fun!
cb

dynamic variable = delete variable
dynamic array[1000]= delete [] array … Nothing more nothing less

I remember reading somewhere that the difference between “delete variable” and “delete [] variable” is that the [] form implies multiple elements and the destructor for each element will be called. With the first form “delete variable”, only the first destructor is called.

Now, I don’t know if or what the standard way is to handle the mixing of the new/delete forms - but it is not recommended.

That is partly correct. When calling delete [], you also call the destructor of all elements. But calling delete (assuming the memory was allocated with new []) results in undefined behaviour. It may call the first destructor, or it may not. It may free the memory correctly, or it may not.

Not only is it “not recommended” to mix operators with and without [], it’s undefined behaviour.