GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-May-2009, 08:13
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past

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:
CPP / C++ / C Code:
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.
  #2  
Old 23-May-2009, 10:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by nanchuangyeyu
...
I am trying to declare a 3D array with the size of 6,400 and 400 in each dimension using the code as following:
CPP / C++ / C Code:
short matrix[6][400][400];

In VS2005 a "short" is 16 bits long, so this array requires 1,920,000 bytes in memory.

In just about all implementations (and certainly for VS2005) variables declared like this are allocated storage on a stack. See Footnote.

The total default stack size for Microsoft 32-bit compilers is usually something like a Megabyte, so this won't work. To tell the compiler/linker to use a larger stack see, for example, Visual C++: Set Stack Size

One "easy" solution that doesn't require a large stack size is to declare the array to be static.
CPP / C++ / C Code:
    static short matrix[6][400][400];
Static variables are allocated storage on the 'heap' and the array is stored in memory obtained from the operating system when the program is loaded. Microsoft compilers usually have a heap limit something like two Gigabytes, so it should work.

Now, "easy" is sometimes "best" and sometimes not. For example, if you had several functions that allocated the storage for local use within the function, "static" is probably not the way to go. For this compiler you can make the stack size larger. An alternative would be to use dynamic allocation. On the other hand, if you just have one fixed-size array in your program, a default stack size and a "static" array may very well be practical.

Regards,

Dave

Footnote: Things they don't always tell you in beginning programming class: If your program is going to need more than a few hundred K bytes of storage for variables, consider using dynamic array allocation (using new for C++ and malloc() or calloc() for C programs). Sometimes static storage will work, and I think it will for your problem, but the bottom line is that it is the programmer's responsibility to be aware of the program's resource requirements.
Last edited by davekw7x : 23-May-2009 at 11:26.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Giving garbage value as the result of product winner C Programming Language 10 19-Jul-2008 02:47
Error C2374 lolp1 C++ Forum 3 25-May-2008 17:40
i need help in C++ PLZ its_me C++ Forum 3 04-Dec-2006 22:51
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 07:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:04.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.