Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ variable data being overwritten
    primarykey
    data
    text
    <p>Been a few years since I've written C/C++, and now I'm facing a problem I just cannot seem to solve on my own.</p> <p>Given the following struct:</p> <pre><code>struct InputData { float diameter; float length; int vertIndex; struct InputData *parent; vector&lt;InputData*&gt; children; bool deadEnd; InputData(float dia, float lngth) { diameter = dia; length = lngth; vertIndex = NULL; parent = NULL; deadEnd = false; } }; </code></pre> <p>I start out by defining a number of nodes, and their parent/child relationship:</p> <pre><code>InputData i0 = InputData(3.0f, 3.0f); InputData i1 = InputData(2.0f, 2.0f); InputData i2 = InputData(1.0f, 1.0f); InputData i3 = InputData(1.0f, 1.0f); InputData i4 = InputData(1.0f, 1.0f); InputData i5 = InputData(1.01f, 0.5f); i0.children.push_back(&amp;i1); i1.children.push_back(&amp;i2); i2.children.push_back(&amp;i3); i3.children.push_back(&amp;i4); i4.children.push_back(&amp;i5); i1.parent = &amp;i0; i2.parent = &amp;i1; i3.parent = &amp;i2; i4.parent = &amp;i3; i5.parent = &amp;i4; </code></pre> <p>Note that i5 as the only node doesn't have any children.</p> <p>I then move on to do some work using this data (calling BuildMeshVertices(&amp;i0, &amp;vertices) from main()), and end up adding a child to i5:</p> <pre><code>void BuildMeshVertices(InputData* current, vector&lt;SimpleVertex&gt; *vertices) { //Do work if(current-&gt;children.size() == 1) { BuildMeshVertices(current-&gt;children[0], vertices); } else if(current-&gt;children.size() == 0 &amp;&amp; current-&gt;deadEnd == false) { InputData iDeadEnd = InputData(1.01f, 0.5f); iDeadEnd.deadEnd = true; iDeadEnd.parent = current; current-&gt;children.push_back(&amp;iDeadEnd); BuildMeshVertices(&amp;iDeadEnd, vertices); } } </code></pre> <p>After which everything is fine. i0 has one child (i1), i1 has a one child (i2), so on and so forth and i5 now has a child as well.</p> <p>I call another function (BuildMeshIndices()), and suddenly a few lines into this function (line 63) the data for the newly added child to i5 is being overwritten. i5 still points to the right child, but the data for this child is suddenly garbled.</p> <p>Here's a screenshot <a href="http://i27.tinypic.com/1q1gkh.png" rel="nofollow noreferrer">before and after</a> (sorry about the link, but I weren't allowed to use IMG tags)</p> <p>I cannot figure out why this happens, but I've got a feeling it's got something to do with my poor memory management? </p> <p><strong>UPDATE</strong> Also it doesn't have to be done this way. If for example changing the children vector to a vector of values is the preferred C++ way, I would much prefer that. I've tried to comment on the answers, but I'm not sure you guys are seeing the comments (According to the FAQ you need 50 reputation to leave comments)? </p> <p>Below is the full source code (with everything unnecessary stripped out, but enough to reproduce the error):</p> <pre><code>#include "stdafx.h" #include &lt;vector&gt; using std::vector; struct InputData { float diameter; float length; int vertIndex; struct InputData *parent; vector&lt;InputData*&gt; children; bool deadEnd; InputData(float dia, float lngth) { diameter = dia; length = lngth; vertIndex = NULL; parent = NULL; deadEnd = false; } }; //-------------------------------------------------------------------------------------- // Vertex types //-------------------------------------------------------------------------------------- struct SimpleVertex { float Pos; SimpleVertex(float Position) { Pos = Position; } }; void BuildMeshVertices(InputData* current, vector&lt;SimpleVertex&gt; *vertices) { current-&gt;vertIndex = vertices-&gt;size(); //Add vertices.. if(current-&gt;children.size() == 1) { BuildMeshVertices(current-&gt;children[0], vertices); } else if(current-&gt;children.size() == 0 &amp;&amp; current-&gt;deadEnd == false) { InputData iDeadEnd = InputData(1.01f, 0.5f); iDeadEnd.deadEnd = true; iDeadEnd.parent = current; current-&gt;children.push_back(&amp;iDeadEnd); BuildMeshVertices(&amp;iDeadEnd, vertices); } } void BuildMeshIndices(InputData* current, vector&lt;unsigned long&gt; *indices) { indices-&gt;push_back(current-&gt;vertIndex+2); indices-&gt;push_back(current-&gt;vertIndex+0); indices-&gt;push_back(current-&gt;vertIndex+1); indices-&gt;push_back(current-&gt;vertIndex+3); indices-&gt;push_back(current-&gt;vertIndex+0); indices-&gt;push_back(current-&gt;vertIndex+2); InputData *parent = current-&gt;parent; unsigned long vOffset; if(parent != NULL &amp;&amp; parent-&gt;children.size() == 1) { vOffset = (unsigned long)current-&gt;vertIndex; indices-&gt;push_back(vOffset+7); indices-&gt;push_back(vOffset+5); indices-&gt;push_back(vOffset+4); indices-&gt;push_back(vOffset+6); indices-&gt;push_back(vOffset+5); indices-&gt;push_back(vOffset+7); indices-&gt;push_back(vOffset+10); indices-&gt;push_back(vOffset+8); indices-&gt;push_back(vOffset+9); indices-&gt;push_back(vOffset+11); indices-&gt;push_back(vOffset+8); indices-&gt;push_back(vOffset+10); indices-&gt;push_back(vOffset+15); indices-&gt;push_back(vOffset+13); indices-&gt;push_back(vOffset+12); indices-&gt;push_back(vOffset+14); indices-&gt;push_back(vOffset+13); indices-&gt;push_back(vOffset+15); indices-&gt;push_back(vOffset+18); indices-&gt;push_back(vOffset+16); indices-&gt;push_back(vOffset+17); indices-&gt;push_back(vOffset+19); indices-&gt;push_back(vOffset+16); indices-&gt;push_back(vOffset+18); } if(current-&gt;children.size() == 1 &amp;&amp; current-&gt;deadEnd == false) { BuildMeshIndices(current-&gt;children[0], indices); } } int _tmain(int argc, _TCHAR* argv[]) { InputData i0 = InputData(3.0f, 3.0f); InputData i1 = InputData(2.0f, 2.0f); InputData i2 = InputData(1.0f, 1.0f); InputData i3 = InputData(1.0f, 1.0f); InputData i4 = InputData(1.0f, 1.0f); InputData i5 = InputData(1.01f, 0.5f); i0.children.push_back(&amp;i1); i1.children.push_back(&amp;i2); i2.children.push_back(&amp;i3); i3.children.push_back(&amp;i4); i4.children.push_back(&amp;i5); i1.parent = &amp;i0; i2.parent = &amp;i1; i3.parent = &amp;i2; i4.parent = &amp;i3; i5.parent = &amp;i4; // Create vertex buffer vector&lt;SimpleVertex&gt; vertices; BuildMeshVertices(&amp;i0, &amp;vertices); // Create index buffer vector&lt;unsigned long&gt; indices; BuildMeshIndices(&amp;i0, &amp;indices); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload