Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++: forward std::vector allocation

Started by maxest Jun 17, 2010 at 5:48 AM 2 replies 1.5k views
Original Post
maxest
maxest
I would like to allocate some fixed amount of memory to vector.

I have the following structure:
struct MeshNode{	mtx normalTransform;	int verticesNum;	int texCoordsNum;	vector<vec3> vertices;	vector<vec2> texCoords;	vector<NodeFace> faces;	vector<vec3> verticesNormals;	vector<vec3> facesNormals;};


Some declaration in code:
vector<MeshNode> nodes;


And then I try to alloc memory this way:
nodes.reserve(this->nodesNum);


The problem is that my progam crashes when application tries to access some field of nodes[0]. In fact, size() of the vector indicates to 0 after memory reservation. I am almost sure that yeasterday this stuff worked and today it doesn't. Moreover, in some other places I also do forward allocation this way and it works.

Does anyone know what might be the problem?

NOTE:
Such allocation:
	for (unsigned int i = 0; i < this->nodesNum; i++)	{		MeshNode meshNode;		nodes.push_back(meshNode);	}

works but this is now what I want to do every time I need forward allocation - raw C arrays are much neater in this situation!
alvaro
alvaro
Original post by maxest
Quote:
nodes.reserve(this->nodesNum);

Read the documentation for vector. That only reserves some space in memory for a certain number of objects, but it does not construct the objects and the size of the vector won't change. What you want is `node->resize(nodesNum)'.

Quote:
	for (unsigned int i = 0; i < this->nodesNum; i++)	{		MeshNode meshNode;		nodes.push_back(meshNode);	}

works but this is now what I want to do every time I need forward allocation - raw C arrays are much neater in this situation!

Before you decide which option is neater, perhaps you should make sure you understand how both work.

Jasperge
Jasperge
hi,

when you are calling: nodes.reserve(this->nodesNum), you request a change in capacity of the vector. (http://www.cplusplus.com/reference/stl/vector/reserve/)

However the actual size of the vector nor the amount elements are modified.
When you want to pre-allocate a number of elements, I would advise you to use the vector::resize() method.
Replacing
nodes.reserve(this->nodesNum);

with
nodes.resize(this->nodesNum);


will likely solve your problem. More information about the vector::resize: http://www.cplusplus.com/reference/stl/vector/resize/

maxest
maxest
Huh, now I feel really nooby. You both point to documentation which I had taken a look at and I guess I understood the difference between these two methods (resize and reserve). In all others places in code (which didn't cause crash) I actually use resize everywhere and only in this one place (which caused crash) I accidentally used reserve. Sorry for my oversight (caused by this common "re" prefix) and thanks for pointing this out to me :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.