Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Can't Write 1's To Struct Member
    primarykey
    data
    text
    <p>I'm having a little problem writing 1's to a struct's member.</p> <p>I have a struct which represents a Voxel which can have 8 children (IDs 0-7). In order to state which children a Voxel has I'm using an unsigned 8-bit integer. If the Voxel has a certain child, then the bit at the position in the children member that equals the child's ID is set to 1.</p> <p>For example, if a Voxels children member stored the value: 10000101 Then the Voxel would have children 0, 2 and 7 (from right to left).</p> <p>The code for the Voxel struct is as below:</p> <pre><code>typedef struct Voxel { uint8_t children; //list of children Voxel* firstChildPointer; }; </code></pre> <p>First child pointer points to the start of a list of voxels which are this voxels children.</p> <p>The problem occurs when wanting to add a child. I need to change the children member to reflect the added child. When doing this however, I get a runtime exception saying "Access violation writing location" followed by a memory address.</p> <p>The code I use to change the children is below:</p> <pre><code>void addVoxel(Voxel* parent, char childID) { parent-&gt;children |= (1 &lt;&lt; childID); } </code></pre> <p>When all the Voxels are first loaded into memory the Voxel's children is set without error. But after that I cannot write 1's to the member. 0's write perfectly fine. I have tried using many methods to write things, even a plain old assignment to a number. But if the number has more 1's then it won't work.</p> <p>It so happens that a pointer to the parent in this case it stored as a global variable, and each voxel also stores a pointer to it's children, could this be the problem? If so would passing the parent as a reference help? I know how to do this in C# but not in C++.</p> <p>Any help would be great.</p> <p>S.</p> <p>EDIT:</p> <p>The add voxel method is called using this line:</p> <pre><code>addVoxel(octree, 4); </code></pre> <p>octree is a voxel pointer as defined by:</p> <pre><code>Voxel* octree = octreeLoader.loadVoxelData(); </code></pre> <p>The load voxel data method loads octree information from a file. We know that this section and the return pointer work as we use an exact duplicate of them to ray trace. However in short the pointer is created using this line:</p> <pre><code>Voxel* octree; </code></pre> <p>And then assigned memory using this line:</p> <pre><code>octree = (Voxel *) calloc(1, sizeof(Voxel)); </code></pre> <p>Children is then assigned using this line:</p> <pre><code>octree[0].children = children; </code></pre> <p>Each further child is then created and given memory in the same way. I have checked the children member is initialised correctly and the childID is correct.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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