943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 480
  • C++ RSS
May 23rd, 2009
0

What's wrong with this 3D array declaration?

Expand Post »
Hi,
I am trying to declare a 3D array with the size of 6,400 and 400 in each dimension using the code as following:
C++ Syntax (Toggle Plain Text)
  1. short matrix[6][400][400];
but actually it introduced a run time error "stack overflow" after surviving the compilation.So what's wrong with this code and how to fulfil my mission rightly?Thank u in advance.I use VS 2005 if it makes any difference.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nanchuangyeyu is offline Offline
18 posts
since Nov 2008
May 23rd, 2009
0

Re: What's wrong with this 3D array declaration?

Hi,
I am trying to declare a 3D array with the size of 6,400 and 400 in each dimension using the code as following:
C++ Syntax (Toggle Plain Text)
  1. short matrix[6][400][400];
So what's wrong with this code?
Nothing, with me it compiles and runs fine
Try compiling it as a release executable, you're probably compiling it in debugging mode ...
Last edited by tux4life; May 23rd, 2009 at 10:30 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 23rd, 2009
0

Re: What's wrong with this 3D array declaration?

With that kind of error message I would be expecting that you have over or under run the array indexes.

If you post some more code, we can have a look.

But check that you don't let your indexes get to 400 or something.
(or negative!).
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
May 23rd, 2009
0

Re: What's wrong with this 3D array declaration?

short usually 2 bytes, So in your case the 3d array Matrix has size of
2 X 6 X 400 X 400 = 1920000 bytes = 1.83 Mega Bytes.
Don't you think it is much?
If your program is using this much of memory, ask yourself " Do I really need this much"?
And besides, why aren't you using vectors instead of arrays?
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. int main()
  5. {
  6. using std::vector;
  7. const short d1=4,d2=400,d3=400,init_val=0;
  8. //Define it!!
  9. vector<vector<vector<short> > > Matrix(d1,
  10. vector<vector<short> >(d2,
  11. vector<short>(d3,
  12. init_val)));
  13.  
  14. //Use it!!
  15. Matrix[0][25][14]=11;
  16. std::cout<<Matrix[0][25][14];
  17. }
I know the syntax is a bit discouraging. Thats why most people go with some wrapper classes like that of Boost's multi_array
For time being cosider the following article from Dr.Dobb's

At the worst case, follow Tux's suggestion and stick with arrays.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
May 23rd, 2009
0

Re: What's wrong with this 3D array declaration?

Look at Project Properties|Linker|System and set the proper Stack Reserve Size parameter (in bytes - for example, set 3000000, see seedhant3s calculations). May be your IDE (VS 2005) has slightly different names for these parameters (I have VS 2008 now).

Better try to avoid large local (automatic) array allocations, especially in recursive functions.

Yet another straightforward approach (allocate a large array in the heap storage):
C++ Syntax (Toggle Plain Text)
  1. typedef short Matrix400[400][400];
  2. int main()
  3. {
  4. Matrix400* matrix = new Matrix400[6];
  5. cout << sizeof(Matrix400[6]) << endl; // sample
  6. ...
  7. delete [] matrix; // don't forget to free
  8. ...

I don't like multi-dimensional vectors (without wrappers). In addition to cumbersome declaration syntax, they have not contiguous data storage. Of course, tastes differ

Do simple things as simple as possible...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: TCP/IP Client Server Scallability
Next Thread in C++ Forum Timeline: hello guys, need help compiling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC