| | |
What's wrong with this 3D array declaration?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 18
Reputation:
Solved Threads: 0
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:
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.
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)
short matrix[6][400][400];
•
•
•
•
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:
So what's wrong with this code?C++ Syntax (Toggle Plain Text)
short matrix[6][400][400];

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."
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?
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.
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)
#include<iostream> #include<vector> int main() { using std::vector; const short d1=4,d2=400,d3=400,init_val=0; //Define it!! vector<vector<vector<short> > > Matrix(d1, vector<vector<short> >(d2, vector<short>(d3, init_val))); //Use it!! Matrix[0][25][14]=11; std::cout<<Matrix[0][25][14]; }
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
(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
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):
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...
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)
typedef short Matrix400[400][400]; int main() { Matrix400* matrix = new Matrix400[6]; cout << sizeof(Matrix400[6]) << endl; // sample ... delete [] matrix; // don't forget to free ...
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...
![]() |
Similar Threads
- array declaration problem (C++)
- creating and searching multi array (C++)
- Allocating an array of objects ? (C++)
- my max function in my array wont work (C++)
- What's Wrong with my Array? (C++)
- Why am I getting this output (C++)
- array declaration explanation needed (C)
- Array declaration problem (C++)
- Return multidimension array (C)
Other Threads in the C++ Forum
- Previous Thread: TCP/IP Client Server Scallability
- Next Thread: hello guys, need help compiling
Views: 307 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






