Allocating Memory in C++

Started by
24 comments, last by jedi2832 20 years, 11 months ago
hey guys, help me out and finish the discussion.
let me run down a noob problem im having right now.

I have a struct LandVert



      struct LandVert{    float x, y, z ; // The transformed position for the vertex.	float nx, ny, nz; //the normal for the square	DWORD color; //the diffuse color of the vertex, also stores alpha.    float tu,tv;	// The vertex texture coordinates.//	};//here i initialize the pointerLandVert* vertices = 0; //pointer to vertices array    In a call to the object that contains the struct i need to newnumofVerts vertices, so i need an array of my struct with numofVerts elements.      HRESULT TListAuthor::MakeTListAuthor(int numofVerts){   vertices = new LandVert[(numofVerts - 1)];}  Later on i attempt to manipulate the components of the struct                      vertices[0].x = -1.0f;		vertices[0].y = 2.0f;		vertices[0].tu = 0.0f;		vertices[0].tv = 0.0f;		vertices[1].x = 1.0f;		vertices[1].y = 2.0f;		vertices[1].tu = 1.0f;		vertices[1].tv = 0.0f;		vertices[2].x = -1.0f;		vertices[2].y = 0.0f;		vertices[2].tu = 0.0f;		vertices[2].tv = 1.0f;		vertices[3].x = -1.0f;		vertices[3].y = 0.0f;		vertices[3].tu = 0.0f;		vertices[3].tv = 1.0f;		vertices[4].x = 1.0f;		vertices[4].y = 2.0f;		vertices[4].tu = 1.0f;		vertices[4].tv = 0.0f;		vertices[5].x = 1.0f;		vertices[5].y = 0.0f;		vertices[5].tu = 1.0f;		vertices[5].tv = 1.0f;	for (int i = 0; i < numofVerts; i++)	{		vertices[i].color = 0xFFFFFFFF;		vertices[i].nx = 0.0f;		vertices[i].ny = 1.0f;		vertices[i].nz = 0.0f;		vertices[i].z = -5.0f;	}  later on i create the V Bufferand then finally draw      	g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);		g_pd3dDevice->SetFVF(UNTRANS_CUSTOMVERTEX);      g_pd3dDevice->SetStreamSource(0,g_pVBuff,0 , sizeof(LandVert));      g_pd3dDevice->SetTexture(0,g_texture);      g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);	g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);    


when i create my array of structs statically it renders fine,
but when i try to create the array dynamically nothing renders.
the reason i included the drawing code is because the sizeof operators and any possible conflicts between
sizeof(Struct) and sizeof (pointertoStruct)


anyone mind helping out,

*edit* just wanted to pint out that the code compiles,
i have debug code that lets me know the VertexBuffer and Texture are being created, thx for any help, just cant figure it out.

Dreddnafious Maelstrom

"If I have seen further, it was by standing on the shoulders of Giants"

Sir Isaac Newton

[edited by - Dreddnafious Maelstrom on May 1, 2003 5:40:02 PM]

[edited by - Dreddnafious Maelstrom on May 1, 2003 5:49:24 PM]
"Let Us Now Try Liberty"-- Frederick Bastiat
Advertisement
Sorry you weren''t very clear what you are trying to do.

Are you trying to dynamically allocate a number of LandVert objects?
yes, i have a struct defined and im trying to dynamically create an array of them.

Im also uncertain how to refer to them later in code and am trying to do it as:

vertices[num].attribute = value.

it compiles and doesnt throw any exceptions, i just cant find my error. when i create the array statically it renders no problem, which is why i think im handling the memory allocation wrong.

[edited by - Dreddnafious Maelstrom on May 1, 2003 5:53:28 PM]
"Let Us Now Try Liberty"-- Frederick Bastiat
It looks fine, the way you access the array later. I think you're allocating the memory fine.

Where do you send your vertices into the buffer? Could your problem be this line?
g_pd3dDevice->SetStreamSource(0,g_pVBuff,0 , sizeof(LandVert));   

Don't you want to replace that with numVertices*sizeof(LandVert).

Also, where do you delete your objects? Remember to use delete[]...

Regards,
Jeff

[edited by - rypyr on May 1, 2003 6:41:36 PM]

[edited by - rypyr on May 1, 2003 6:45:22 PM]

[edited by - rypyr on May 1, 2003 6:47:21 PM]
hehe, that was it, i was sizeof''ing a pointer.
i had begun to doubt my sanity, lol.
thx for the help man.
"Let Us Now Try Liberty"-- Frederick Bastiat
vertices = new LandVert[(numofVerts - 1)];                                   ^^^^
Why do you do that? You will allocate one less LandVert than specified in numofVerts. Is that intentional? In order to allocate 10 elements (element [0-9]), specify 10.

This topic is closed to new replies.

Advertisement