Hey fellow programmers,
Vandaag ben ik tegen een stuk code aangelopen, wat in eerste instantie crashte, uiteindelijk heb ik het verholpen door een reference te veranderen( zie code ). Nu is mijn vraag wat er is er mis met het gebruiken van een reference ( of pointer ) in de volgende code:? Ikzelf zie namelijk niet zo 123 waar het nu precies misgaat.
Vandaag ben ik tegen een stuk code aangelopen, wat in eerste instantie crashte, uiteindelijk heb ik het verholpen door een reference te veranderen( zie code ). Nu is mijn vraag wat er is er mis met het gebruiken van een reference ( of pointer ) in de volgende code:? Ikzelf zie namelijk niet zo 123 waar het nu precies misgaat.
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| nodeList->push_back( parentNode ); //push back parentnode nodeStack.push( 0 ); bool leafReached = false; while( !nodeStack.empty() ) { bool continueTraversal = true; //fetch parent! int parentIndex = nodeStack.top(); nodeStack.pop(); /* Replace with the following to crash the program: alDepthNode& curNode = (*nodeList)[ parentIndex ]; */ alDepthNode curNode = (*nodeList)[ parentIndex ]; //stop when max depth reached if( maxDepth > 0 ) { if( curNode.m_depth == maxDepth ) continueTraversal = false; } // stop when leafnode reached else if(curNode.m_node.m_bitSet & OCTREE_IS_LEAF ) { continueTraversal = false; } if( continueTraversal ) { int childCount = 0; int sliceId = curNode.m_node.getSliceId(); int sliceOffset = curNode.m_node.getOffsetInSlice(); alRunTimeSlice *slicePtr = m_treePtr->loadSlice( sliceId ); for( int i = 0; i < 8; i++ ) { if( curNode.m_node.hasChild( i ) ) { alDepthNode childNode; childNode.m_parentIndex = parentIndex; childNode.m_bounds = alOctree::boundsForId( curNode.m_bounds, i ); childNode.m_bounds.print(); childNode.m_depth = curNode.m_depth + 1; childNode.m_node = slicePtr->getNodeAt( sliceOffset + i ); //add to nodelist nodeList->push_back( childNode ); int childIdx = nodeList->size() - 1; //store child index curNode.m_childIndices[i] = childIdx; //push on stack nodeStack.push( childIdx ); } } //This is necessary because using references crashes??? the program (*nodeList)[parentIndex] = curNode; } } |