What's wrong with this 3D array declaration?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 18
Reputation: nanchuangyeyu is an unknown quantity at this point 
Solved Threads: 0
nanchuangyeyu nanchuangyeyu is offline Offline
Newbie Poster

What's wrong with this 3D array declaration?

 
0
  #1
May 23rd, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

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

 
0
  #2
May 23rd, 2009
Originally Posted by nanchuangyeyu View 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:
  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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

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

 
0
  #3
May 23rd, 2009
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!).
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

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

 
0
  #4
May 23rd, 2009
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?
  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.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #5
May 23rd, 2009
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):
  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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 307 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC