Note that there are some explanatory texts on larger screens.

plurals
  1. PODesign a Multidimensional Array in C++
    primarykey
    data
    text
    <p>I want to design a C++ class for multidimensional arrays. By multidimensional I mean 1d, 2d, 3d, etc. The class supports element by element addition and scalar multiplication operators. Assume A and B are instances of the class (of the same size &amp; dimension). I'd like to use the objects in expressions like this:</p> <p>C = A * 2 + B</p> <p>The thing I'm wondered about is how to manage the memory. In the above expression, A * 2 will create a temporary object of the class which is later added to B. Anyways, the temporary object is garbage after the addition is done. I wrote the following class and it works nicely but I am pretty sure it leaks the memory. Now my questions are,</p> <ol> <li>How can I fix the memory problem? What is the best way to design the class?</li> <li><p>Is that possible to allocate the required memory on the stack instead of the heap?</p> <pre><code>class XArray { int num_dim; int *dims; int *index_helper; int table_size; double *table; public: XArray(const int n, const int *d):num_dim(n), dims(d) { int size = 1; for (int i = 0; i &lt; n; i++) { size *= d[i]; } table_size = size; table = new double[size]; index_helper = new int[n]; }; ~XArray() { delete[] table; delete[] index_helper; }; int dim(int d) { return dims[d]; } double&amp; operator()(int i) { index_helper[0] = i; return get_helper(1, index_helper); } double&amp; operator()(int i, int j) { index_helper[0] = i; index_helper[1] = j; return get_helper(2, index_helper); } double&amp; operator()(int i, int j, int k) { index_helper[0] = i; index_helper[1] = j; index_helper[2] = k; return get_helper(3, index_helper); } XArray operator*(double m) { XArray *xa = new XArray(num_dim, dims); for (int i = 0; i &lt; table_size; i++) { xa-&gt;table[i] = this-&gt;table[i] * m; } return *xa; } XArray operator+(const XArray &amp;that) { if (num_dim != that.num_dim) { char *msg = new char[100]; sprintf(msg, "XArray::dimensions do not match in + operation, expected %d, found %d", num_dim, that.num_dim); throw msg; } for (int i = 0; i &lt; num_dim; i++) { if (this-&gt;dims[i] != that.dims[i]) { char *msg = new char[100]; sprintf(msg, "XArray::dimension %d not mached, %d != %d", i, dims[i], that.dims[i]); throw msg; } } XArray *xa = new XArray(num_dim, dims); for (int i = 0; i &lt; table_size; i++) { xa-&gt;table[i] = this-&gt;table[i] + that.table[i]; } return *xa; } private: double&amp; get_helper(int n, int *indices) { if (n != num_dim) { char *msg = new char[100]; sprintf(msg, "XArray::dimensions do not match, expected %d, found %d", num_dim, n); throw msg; } int multiplier = 1; int index = 0; for (int i = 0; i &lt; n; i++) { if (indices[i] &lt; 0 || indices[i] &gt;= dims[i]) { char *msg = new char[100]; sprintf(msg, "XArray::index %d out of range, %d not in (0, %d)", i, indices[i], dims[i]); throw msg; } index += indices[i] * multiplier; multiplier *= dims[i]; } return table[index]; } </code></pre> <p>};</p></li> </ol>
    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.
 

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