I am trying to use a lot of (vector<>)s in my code for a QuadTree type

structure. More or less, I have one main (vector<>)[m_oNodes] which holds

all the nodes. But I also have all sorts of nested (vector<>)s and

relations between the nodes in the main (vector<>). It has been mentioned

to me that pointers to elements in the (vector<>)s can go bad quite often.

Now I am paranoid and not sure how long or under what conditions a pointer

(such as p_pNode or pNode in CreateQuad()) will be valid. I am afraid to

nest too deep, or use too many pointers. I wanted to nest

CreateLeafHeightMap inside CreateQuad, but I am worried about doing so. Is

there a good reference as to when pointers to (vector<>)s go bad? What can

you guys tell me about this, am I being to paranoid? Thanks.

struct StQuadBound

{

float fX;

float fZ;

Quote:

};

struct StLeafHMVertex

{

float fX;

float fZ;

float fY;

float fNx;

float fNy;

float fNz;

float fTu;

float fTv;

Quote:

};

struct StNode

{

bool bLeaf; // This node is a leaf

bool bRoot; // This node is the root

int iIndex; // This node's index in vector

int iLevel; // This node's level in tree

StQuadBound stBounds[4]; // This node's boundaries

int iParentIndex; // This node's parent's index

int iSize; // This node's width/height

C2DVector<StLeafHMVertex, 1, 1> oHeightMap; // This node's height map

Quote:

};

//******************Part of the Class******************//

void CreateQuad(StNode *p_pNode)

/**

* Name: CreateQuad

* Desc: Take a QuadTree node and make 4 child nodes

**/

{

if (p_pNode->iLevel == m_iMaxLevel) return;

for (int iCnt = 0; iCnt<4; iCnt++)

{

m_iNodeCount++;

StNode *pNode = &m_oNodes[m_iNodeCount-1];

pNode->iIndex = m_iNodeCount - 1;

// Child is never the root

pNode->bRoot = false;

// Child is leaf if is in the last level

pNode->iLevel = p_pNode->iLevel + 1;

if (pNode->iLevel == m_iMaxLevel)

pNode->bLeaf = true;

else

pNode->bLeaf = false;

// Parent

pNode->iParentIndex = p_pNode->iIndex;

// Bounds

pNode->iSize = p_pNode->iSize / 2;

float iCenterX = p_pNode->stBounds[0].fX + pNode->iSize;

float iCenterZ = p_pNode->stBounds[3].fZ + pNode->iSize;

float fLBX = 0;

float fLBZ = 0;

switch (iCnt)

{

case 0:

fLBX = p_pNode->stBounds[0].fX;

fLBZ = iCenterZ;

break;

case 1:

fLBX = iCenterX;

fLBZ = iCenterZ;

break;

case 2:

fLBX = iCenterX;

fLBZ = p_pNode->stBounds[2].fZ;

break;

case 3:

fLBX = p_pNode->stBounds[0].fX;

fLBZ = p_pNode->stBounds[2].fZ;

break;

}

ComputeBounds(pNode, fLBX, fLBZ);

// Recurse children

if (!pNode->bLeaf) CreateQuad(pNode);

}

Quote:

}

void CreateLeafHeightMap(int p_iNode)

/**

* Name: CreateLeafHeightMap

* Desc: Create Height Map for a node

**/

{

StNode *pRoot = &m_oNodes[0];

StNode *pNode = &m_oNodes[p_iNode];

// Get first column and row index

int iFirstCol = (int)((pNode->stBounds[0].fX - pRoot->stBounds[0].fX) /

m_iHeightMapIntSize);

if (iFirstCol != 0) iFirstCol++;

int iFirstRow = (int)((pRoot->stBounds[0].fZ - pNode->stBounds[0].fZ) /

m_iHeightMapIntSize);

if (iFirstRow != 0) iFirstRow++;

int iColCnt = (int)(((pNode->stBounds[1].fX - pNode->stBounds[0].fX) /

m_iHeightMapIntSize) + 1);

int iRowCnt = (int)(((pNode->stBounds[0].fZ - pNode->stBounds[3].fZ) /

m_iHeightMapIntSize) + 1);

pNode->oHeightMap.Resize(iColCnt, iRowCnt);

for (int iCntCol = 0; iCntCol<iColCnt; iCntCol++)

{

for (int iCntRow = 0; iCntRow<iRowCnt; iCntRow++)

pNode->oHeightMap.Set(iCntCol, iCntRow) =

pRoot->oHeightMap.Get(iFirstCol+iCntCol, iFirstRow+iCntRow);

}

Quote:

}