|
-
April 3rd, 2009, 05:51 AM
#1
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!
-
April 3rd, 2009, 06:03 AM
#2
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()
{
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
April 3rd, 2009, 06:06 AM
#3
Re: Stack overflow when declaring array
This question has already been answered elsewhere, and frankly, a global variable is typically a non-solution.
-
April 3rd, 2009, 06:09 AM
#4
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.
-
April 3rd, 2009, 06:14 AM
#5
Re: Stack overflow when declaring array
 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.
 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);
-
April 3rd, 2009, 06:18 AM
#6
Re: Stack overflow when declaring array
 Originally Posted by laserlight
This question has already been answered elsewhere,
I was just about to post one like that!
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
April 3rd, 2009, 06:22 AM
#7
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
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
April 3rd, 2009, 06:43 AM
#8
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:
 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.
Last edited by Zaccheus@Work; April 3rd, 2009 at 06:45 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|