Ergg, Bad pointer!

Dec 8, 2010 at 10:32pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<typename TVertex>
void Primitive<TVertex>::Begin(ushort pVertices, ushort pIndices)
{
	vertexCount = 0;
	indexCount = 0;
	TVertex *vertex = new TVertex[pVertices];
	ushort *index = new ushort[pIndices];
	myMesh = 0;
}

template<typename TVertex>
void Primitive<TVertex>::AddVertex(const TVertex &pVertex)
{
	vertex[vertexCount] = pVertex;
	++vertexCount;
}


This comes out as a fatal error. Thankfully, I am able to debug it, but I still haven't figured anything out.
Dec 8, 2010 at 11:20pm
I'm just guessing here as you haven't provided much in the way of how those functions are used, but it looks like if you add a vertex, the AddVertex() function will be accessing beyond the end of the vertex array. You never extend it's size.
Dec 8, 2010 at 11:41pm
Wait, but I want it to stay contained within the vertex array. What am I doing wrong about that?

Edit:
oh joy, I've looked all over the internet and nothing tells me what's so bad about what I'm doing.
Last edited on Dec 9, 2010 at 12:27am
Dec 9, 2010 at 12:29am
Would it be easier to use a std::vector<TVertex> and std::vector<ushort>? You can then query the size of the vector and ensure that vertexCount remains below that. Or you can just let them grow as needed.
Dec 9, 2010 at 12:38am
Hmm... okay.
Topic archived. No new replies allowed.