Stack overflow when declaring array
Hello,
I have a class, MyClass, which contains a 2-dimensional array, my_array, of a structure of type MyStruct. Below is the code I have used to implement this :
Code:
struct MyStruct
{
int u;
int v;
int w;
int x;
int y;
int z;
};
class MyClass
{
private:
MyStruct my_array[1000][500];
};
void main()
{
MyClass my_class;
}
However, when I compile this, I get a compiler error indicating "stack overflow". I'm a bit of a rookie, but from what I gather this is because when I create the my_struct array, I am creating it directly onto the stack, rather than the heap. And, seeing as my_struct contains a large number of MyStructs, each of which contains 6 ints, then that is a lot of data to be stored on the stack, causing an overflow.
But I don't know how to do this any other way, and I don't really understand which elements of the program are created in the stack, and which are created on the heap.
I have tried defining MyClass without telling it to explicitely create all of the elements of the array:
Code:
class MyClass
{
private:
MyStruct my_array[][];
};
But I get the error: "error C2087: 'my_array' : missing subscript"
All I want to do is let my program know that this array will exist, but I don't need to create all the elements of the array right at the start of the program do I? And they certainly shouldn't be put on the stack, as I will need to refer to them throughout the program...
This whole stack / heap thing is getting me down, please could somebody enlighten me? Thanks!
Re: Stack overflow when declaring array
Code:
struct MyStruct
{
int u;
int v;
int w;
int x;
int y;
int z;
};
class MyClass
{
private:
MyStruct my_array[1000][500];
};
MyClass my_class;
void main()
{
}
Re: Stack overflow when declaring array
This question has already been answered elsewhere, and frankly, a global variable is typically a non-solution.
Re: Stack overflow when declaring array
Great that seems to work, thanks.
One more thing - what's the difference between these two in declaring my_class:
Code:
MyClass my_class;
// or
MyClass my_class = new MyClass();
Are they the same? And is the only difference that if I wanted to have a constructor with parameters, I could use the latter to initialize some variables??
Thanks.
Re: Stack overflow when declaring array
Quote:
Originally Posted by ejohns85
Are they the same?
Effectively, since the second version is likely to be optimised to the first, but if I am not wrong, in theory the second version involves default construction of a temporary and then copy construction of my_class.
Quote:
Originally Posted by ejohns85
And is the only difference that if I wanted to have a constructor with parameters, I could use the latter to initialize some variables?
No, you can directly initialise with arguments, e.g.,
Code:
MyClass my_class(x, y, z);
Re: Stack overflow when declaring array
Quote:
Originally Posted by
laserlight
This question has already been answered
elsewhere,
I was just about to post one like that! :thumb:
Re: Stack overflow when declaring array
Code:
MyClass my_class = new MyClass(); // Looks like java!
That won't compile.
Did you mean
Code:
MyClass my_class = MyClass(); // A bit pointless
or
Code:
MyClass* my_class = new MyClass(); // Manual memory allocation
Re: Stack overflow when declaring array
You could do this:
Code:
class MyClass
{
public:
MyClass()
: my_array( 1000, std::vector<MyStruct>(500) ) // 1000 vectors with 500 MyStruct-s each.
{
}
private:
std::vector< std::vector<MyStruct> > my_array;
};
EDIT:
Quote:
Originally Posted by
laserlight
This question has already been answered
elsewhere, and frankly, a global variable is typically a non-solution.
Oh - well nicely confirmed then. :)