Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an approach using C and structs to provide some degree of object oriented along with a set of helper functions.</p> <p>The idea here was to use Kerrick's suggestion of a contiguous array.</p> <p>I am not sure if I got the offset calculation correct and it has not been tested so it is worth what you are paying for it. However it may be helpful as a starting place.</p> <p>The idea is to have a single contiguous area of memory to make memory management easier. And to use a function to access a particular element using a zero based offset in the x, y, and z directions. And since I was not sure as to the element size/type, I made that a variable as well.</p> <pre><code>#include &lt;malloc.h&gt; typedef struct _Array3d { int elSize; // size of each element of the array in bytes int side; // length of each side of the 3d cube in elements char * (*Access) (struct _Array3d *pObj, int x, int y, int z); char buffer[1]; } Array3d; static char * Array3d_Access (Array3d *pObj, int x, int y, int z) { char *pBuf = NULL; if (pObj &amp;&amp; x &lt; pObj-&gt;side &amp;&amp; y &lt; pObj-&gt;side &amp;&amp; z &lt; pObj-&gt;side) { pBuf = &amp;(pObj-&gt;buffer[x * pObj-&gt;side * pObj-&gt;elSize * pObj-&gt;side * pObj-&gt;elSize + y * pObj-&gt;side * pObj-&gt;elSize + z * pObj-&gt;elSize]); } return pBuf; } // Create an Array3d cube by specifying the length of each side along with the size of each element. Array3d *Array3d_Factory (int side, int elSize) { Array3d *pBuffer = malloc (sizeof(Array3d) + side * elSize * side * elSize * side * elSize); if (pBuffer) { pBuffer-&gt;elSize = elSize; pBuffer-&gt;side = side; pBuffer-&gt;Access = Array3d_Access; } return pBuffer; } // Create an Array3d cube that is the same size as an existing Array3d cube. Array3d *Array3d_FactoryObj (Array3d *pObj) { Array3d *pBuffer = NULL; if (pObj) { int iBufferSize = pObj-&gt;side * pObj-&gt;elSize * pObj-&gt;side * pObj-&gt;elSize * pObj-&gt;side * pObj-&gt;elSize; pBuffer = malloc (sizeof(Array3d) + iBufferSize); if (pBuffer) { pBuffer-&gt;elSize = pObj-&gt;elSize; pBuffer-&gt;side = pObj-&gt;side; pBuffer-&gt;Access = pObj-&gt;Access; } } return pBuffer; } // Duplicate or clone an existing Array3d cube into new one. // Returns NULL if cloning did not happen. Array3d *Array3d_Dup (Array3d *pObjDest, Array3d *pObjSrc) { if (pObjSrc &amp;&amp; pObjDest &amp;&amp; pObjSrc-&gt;elSize == pObjDest-&gt;elSize &amp;&amp; pObjSrc-&gt;side == pObjDest-&gt;side) { int iBufferSize = pObjSrc-&gt;side * pObjSrc-&gt;elSize * pObjSrc-&gt;side * pObjSrc-&gt;elSize * pObjSrc-&gt;side * pObjSrc-&gt;elSize; memcpy (pObjDest-&gt;buffer, pObjSrc-&gt;buffer, iBufferSize); } else { pObjDest = NULL; } return pObjDest; } int main(int argc, _TCHAR* argv[]) { Array3d *pObj = Array3d_Factory (10, 20 * sizeof(char)); char *pChar = pObj-&gt;Access (pObj, 1, 2, 3); return 0; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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