How To Allocate Enough Memory For My Array

Started by
7 comments, last by Mike.Popoloski 16 years, 2 months ago
Hey there, I've recently converted my game project over from Visual Basic to C/C++. Now I'm at the stage where I'm writing the model loader, I've rewritten the MAXScript exporter I was using to work with binary files, to make the files smaller and more easily read. Now I've gotten my program to read the header for my binary file, but now comes the tricky part, loading the data in the file. Below are the structs I'm using, I just want to know who I can allocate enough space for the oVertices, oTVertics and oFaces arrays so that I can reference oVertices(0).x for the x component of the 1st vertex. Simple, huh?
[source lang=cpp]
struct VERTEX {
	float	x;
	float	y;
	float	z;
};

struct FACE {
	short	a;
	short	b;
	short	c;
};

struct CMF_FILE_HEADER {
	float	fVersion;
	char	fNumObjs;
};

struct CMF_OBJECT_HEADER {
	long	hVertices;
	short	hFaces;
	long	hTVertices;
};

struct CMF_FILE_DATA {
	VERTEX	oVertices();
	VERTEX	oTVertices();
	FACE	oFaces();
};

Advertisement
Well, since you know the size of your array, it really shouldn't be that hard.

VERTEX *vertices = new VERTEX[vertexCount];


Now you can access each vertex like any array:

vertices[0].x = 0;


Also, remember that once you are done with them, you must delete the memory you allocated like this:

delete[] vertices;
Mike Popoloski | Journal | SlimDX
std::vector is a templated dynamic array class, included in the Standard C++ Library.

Usage is much like a normal array, but with member functions such as push_back() to add an element and size() to tell the current number of elements in the vector.

#include <vector>struct CMF_FILE_DATA {	std::vector<VERTEX>	oVertices;	std::vector<VERTEX>	oTVertices;	std::vector<FACE>	oFaces;};void load( CMF_FILE_DATA &data ){    // read vertex count    while( /* more vertices */ )    {         VERTEX v = // read vertex         data.vertices.push_back(v);    }}void draw( const CMF_FILE_DATA &data ){    for( int i = 0 ; i < data.vertices.size() ; ++i )    {        plotVertex(data.vertices);    }}
Quote:Original post by Mike.Popoloski
Also, remember that once you are done with them, you must delete the memory you allocated


Just be wary that the notion of "done with them" is a very surprising one in particular. For example, the following code contains a memory leak:
int *array = new int[size];frobnicate(array);delete [] array;


The memory leak comes from the possibility of frobnicate throwing an exception, which would consequently cause the execution flow to skip the deletion of the array. The typical solution to this, in the 'C style', it a 'finally' clause:
int *array = new int[size];try { frobnicate(array); }catch(...) {  delete [] array;  throw;}delete [] array;


Because writing a try-catch(...)-throw block for every object you need to release is annoying, C++ programmers use RAII instead: this relies on the fact that destructors are automatically called when leaving a scope, even if this happens because of an exception. So, the following code does not contain leaks:
std::vector<int> array(size);frobnicate(& array.front());// The memory is always deallocated without effort here.


Cheers for the quick responses, just one more question

[source lang=cpp]CMF_FILE_DATA newData;		newData.oVertices = new VERTEX[newObject->hVertices];newData.oTVertices = new VERTEX[newObject->hTVertices];newData.oFaces = new FACE[newObject->hFaces];


Would this work?
That would depend on the type of oVertices, oTVertices and oFaces. If they are the same as in your original post, then it won't even compile. If they are dumb pointers, then they'll probably leak memory if there's an exception. If they are smart pointers then it probably won't compile.

Any chance anyone might be able to point me in the right direction?
Quote:Any chance anyone might be able to point me in the right direction?


Quote:std::vector is a templated dynamic array class, included in the Standard C++ Library.

Usage is much like a normal array, but with member functions such as push_back() to add an element and size() to tell the current number of elements in the vector.


Quote:So, the following code does not contain leaks:

std::vector<int> array(size);

frobnicate(&array.front());

// The memory is always deallocated without effort here.


Quote:That would depend on the type of oVertices, oTVertices and oFaces. If they are the same as in your original post, then it won't even compile. If they are dumb pointers, then they'll probably leak memory if there's an exception. If they are smart pointers then it probably won't compile.


(Hint: in your original post, you didn't actually make oVertices, oTVertices and oFaces "arrays"; you made them member functions.)

Is there any particular reason you're not convinced the direction you're being pointed in is the right one?
Yea, my memory solution is the one you asked for, but the one provided by rip-off is much preferred, for the reasons stated by the rest of the posters here. Use the std::vector, it will save you many headaches.
Mike Popoloski | Journal | SlimDX

This topic is closed to new replies.

Advertisement