0

I have searched on google but only found answers for single-dimension arrays.

I have a 3 dimension array storing data to be later processed by a function. My array looks like this : levelcode[400][20][100]. It stores all the info that the decode level function needs. I get an stack overflow error immediately.

But how can i point to the entire array to get the values of it ( or how do i pass down the entire array? ) ?

I know i can technically call the function for each existing parameter but i think it would be faster and it would look better if the entire array was passed down or being used using a pointer of some sort. How can i accomplish this?

vcp
  • 962
  • 7
  • 15
MoustacheSpy
  • 743
  • 1
  • 6
  • 27
  • Have you read the answers to similar questions, such as this one: http://stackoverflow.com/questions/18283089/pointers-to-3d-arrays-c – W.K.S Mar 29 '16 at 12:24
  • Is it possible to use an `std::array` instead? –  Mar 29 '16 at 12:35
  • Can you post some code to show how your arrays are declared? – Chiel Mar 29 '16 at 12:37
  • Do you have an `some_type name[][][]` or `some_type name***`? – NathanOliver Mar 29 '16 at 12:37
  • You should consider using a flatten single dimension array. You could simply use `std::vector` or `std::array` – coincoin Mar 29 '16 at 12:39
  • I have this : levelCode1[][][]; – MoustacheSpy Mar 29 '16 at 12:41
  • would it be possible to define it as a global variable? Then id have more memory since heap doesnt overflow as easily as stack and the function would have the array too? Or is there something im missing – MoustacheSpy Mar 29 '16 at 12:43
  • @MoustacheSpy Typically global variables is considered code smell. – NathanOliver Mar 29 '16 at 12:45
  • I know but it seems like the right thing to do? My array looks like this : levelcode[400][20][100].It stores all the info that the decode level function needs. I get an stack overflow error immediately. I could split it into multiple arrays. But heap could probably hold it – MoustacheSpy Mar 29 '16 at 12:52
  • @MoustacheSpy If your array is that big I suggest you use a `std::vector` – NathanOliver Mar 29 '16 at 12:59
  • @NathanOliver Can you explain to me what that is exactly? – MoustacheSpy Mar 29 '16 at 13:47
  • @MoustacheSpy a [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) is basically a self managed grow-able array. It stores the data dynamically so you will be using main memory instead of the small program memory. I would suggest you use it with just one dimension and then you can use math to fake the 3 dimensions. – NathanOliver Mar 29 '16 at 13:49
  • @NathanOliver Thank you. Please post it as an answer so i can check it as solved! – MoustacheSpy Mar 29 '16 at 14:04

1 Answers1

1

I suggest you use a std::vector. It is basically a self managed grow-able array. It stores the data dynamically(heap) so you will be using the full system memory instead of the small bit of memory the program is given for automatic objects(stack). With your levelcode[400][20][100] you have 800,000 elements. if the array is of type int then you would more than likely need 3.2MB of space for the array. typically this is larger than than the space provided to the program and will cause a stack overflow

I would suggest you use single dimension vector and then you can use math to fake the 3 dimensions. This will make the data more cache friendly as multi-dimensional vectors do not have to have each dimension located right next to each other like a multi-dimensional array does.

So instead of having a

std::vector<std::vector<std::vector<some_type>>> name{DIM1, vector<vector<some_type>>{DIM2, vector<some_type>{DIM3}}};

and using it like

name[x][y][z]

We could have a

std::vector<some_type> name{DIM1 * DIM2 * DIM3};

and then you can access the elements with

name[x*DIM2*DIM3 + y*DIM3 + z]
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • how should i "fake" it? Cant i just use 3 of those? – MoustacheSpy Mar 29 '16 at 14:11
  • I don't find this a good answer to the question without any examples and without the explanation how to map three dimensions to a single one. – Chiel Mar 29 '16 at 14:15
  • Furthermore, the `std::vector` is of course the best container, but it does not really explain why the stack allocation overflows. – Chiel Mar 29 '16 at 14:16
  • 1
    @Chiel I was getting to that when you posted your comments. Hopefully this addresses those concerns. – NathanOliver Mar 29 '16 at 14:26
  • -1 What is required for a SO question should also apply to answers. An answer should be minimal and adressing the question without redundant information. It also should use correct terms. 'main memory'? 'small program based memory'? segfault vs. stack overflow? why confuse with 'faking'? Where is the proof that cache optimisation is needed anyway? – stefan Mar 29 '16 at 15:39
  • @stefan I fixed a couple of your points. There is no proof that the cache optimization is needed but I tried to make the code performance as close to what the OP had and a single dimension vector does that where a multi dimension vector does not. – NathanOliver Mar 29 '16 at 15:48