I have something that I need a 2D array, but for better cache performance, I really should be normal Array Here are my thoughts but I do not know that this is a terrific idea:
const int xwidth = 10, ywidith = 10; Int main () {int * tempInts = new int [XWIDTH * YWIDTH]; Int ** ints = new int * [XWIDTH]; (Ints [i] = & amp; tempInts [i * YWIDTH];} // do ints works for [int i = 0; i & lt; xwidth; i ++] [] ints [0] ; Delete [] ints; Return 0;}
So it is the idea that instead of new
a bunch of arrays (and in different places in memory of them
Reasons to be removed [] * (int *) ints;
Because I actually do this in a class And this is [the amount of memory to save the trivial pointer.
Just wondering Yes, if there is a reason then this is a terrible idea or if there is an easy / better way to target the target in <[x] * x [y] ints [x] [y] instead of Edit: A simple benchmark indicates that my path is fast without optimization, but GCC is optimally optimized for some reason If you want GCC- best pile, then mine, then normal If you compile at a larger value than the X_MAX set and set Y_MAX to a small value, and use GCC-O3, then my stack should be really fast, but not normal. If you make X_MAX smaller and Y_MAX larger, then you should win (also for some reason on the stack method). The problem with this kind of approach is that this error is prone to error. I tell you to wrap allocations and access to individual elements for your 2D array in the classroom. In that case, if you ever feel the need to change the allocation, then you can only implement ways to maintain the interface. Your remaining code will actually use only the operator () and the constructor, it will help you to stop the memory leak too. ints [x * YWIDTH + y]
class Array2D {Private: / * required indicator for selected implementation * / public: Array2D (unsigned int dim1, unsigned int dim2); ~ Array2D () / * is required, because you will have allocation memory for this class * / double operator () (unsigned int x, unsigned int y); }
Comments
Post a Comment