Stack Overflow? (Operator Overloading)

Started by
19 comments, last by iMalc 12 years, 10 months ago
I keep getting a stack overflow error when running the below code.
Can anyone tell me why this is?

Thanks

//Create the array
char testWorld[25*16][25*16][25*16];
//Populate the array
for (int z=0; z<25*16; z++)
{
for (int y=0; y<25*16; y++)
{
for (int x=0; x<25*16; x++)
{
testWorld[x][y][z] = testWorld[x+16][y][z];
}
}
}
//Move the array
fps->start();
for (int z=0; z<25*16; z++)
{
for (int y=0; y<25*16; y++)
{
for (int x=0; x<24*16; x++)
{
testWorld[x][y][z] = testWorld[x+16][y][z];
}
}
}
cout << fps->get_ticks();
If this post was helpful please +1 or like it !

Webstrand
Advertisement
Someone correct me if I'm wrong but I think you need to use operator new if you want to allocate on the heap IE your stack is too small.

I keep getting a stack overflow error when running the below code.
Can anyone tell me why this is?

Thanks

//Create the array
char testWorld[25*16][25*16][25*16];
//Populate the array
for (int z=0; z<25*16; z++)
{
for (int y=0; y<25*16; y++)
{
for (int x=0; x<25*16; x++)
{
testWorld[x][y][z] = testWorld[x+16][y][z];
}
}
}
//Move the array
fps->start();
for (int z=0; z<25*16; z++)
{
for (int y=0; y<25*16; y++)
{
for (int x=0; x<24*16; x++)
{
testWorld[x][y][z] = testWorld[x+16][y][z];
}
}
}
cout << fps->get_ticks();



Your array is just to big for the Stack. You'll need to allocate it dynamically.
Because you're trying to allocate 64000000 bytes on the stack, and the stack has a maximum size.

You can increase the maximum stack size, but you'd probably be better off just allocating this big array on the heap instead - i.e. use new char[blah]

(ninja'd)

wink.gif
[size="1"]
Consider using a dynamic container like std::vector<char> instead of manually managing dynamic memory.
It seems I can only allocate a 1deminsional array dynamically.
char* testWorld;
testWorld = new char [25*16*25*16*25*16];


Would I have to access the elements like this?
testWorld[(x*400*400)+(y*400)+(z)]
Is that math correct?

Is it possible to be able to access the elements like the following?
testWorld[x][y][z]


Consider using a dynamic container like std::vector<char> instead of manually managing dynamic memory.
[/quote]

All I need is the 64mb char's in memory, the size wont change, only the data. Wouldn't a vector be overkill?
If this post was helpful please +1 or like it !

Webstrand
You can write a wrapper class:

#include <vector>

template<typename T>
class Array3D
{
public:
Array3D(int width, int height, int depth)
:
width(width),
height(height),
depth(depth),
storage(width * height * depth)
{
}

T &operator()(int x, int y, int z)
{
return storage[index(x,y,z)];
}

const T &operator()(int x, int y, int z) const
{
return storage[index(x, y, z)];
}

private:
int index(int x, int y, int z) const
{
return (x * width * height) + (y * height) + z
}

int width;
int height;
int depth;
std::vector<T> storage;
};

You index using array(1,7,42) rather than using square brackets, but it isn't too bad. You could use boost::multi_array, but I find it very ugly.


Wouldn't a vector be overkill?
[/quote]
Vector is pretty lean. I prefer not to mix the raw memory management with other logic. You could use a fixed array like boost::array as the underlying storage.
Man that array3d class is pretty slick I think I may implement that.

The only thing is would that be slower than a standard 3d array because we have to calculate the index?
return (x * width * height) + (y * height) + z
If this post was helpful please +1 or like it !

Webstrand
You change the width, height and depth to be template parameters, which would cause it to be evaluated (as much as possible) at compile time. This could cause code bloat if you create many instances of this class with varying sizes and types.

You could write an iterator type. This would allow you to use direct pointer increments for accessing each element when you don't need random access.

I don't know if it would cause as much trouble as you are worrying about. The modern CPU tends to be waiting for memory, it will likely have plenty of time to do that math =]

Man that array3d class is pretty slick I think I may implement that.

The only thing is would that be slower than a standard 3d array because we have to calculate the index?
return (x * width * height) + (y * height) + z


The index has to be calculated like that anyway - the only difference with a standard array is that the compiler will generate the code automatically when compiling.

This topic is closed to new replies.

Advertisement