The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
can i declare a variable -> array[256][1024]?
Discuss can i declare a variable -> array[256][1024]? in the C Programming forum on Dev Shed. can i declare a variable -> array[256][1024]? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 26th, 2003, 08:07 AM
|
|
Junior Member
|
|
Join Date: Jun 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
can i declare a variable -> array[256][1024]?
I want to declare an multidimensional array as follows:
array[256][1024]....
but the program seems to crash.
when i use smaller numbers like
array[50][50]
the program runs like its supposed to..
can i use 256 and 1024?
|

June 26th, 2003, 01:48 PM
|
|
Junior Member
|
|
Join Date: Jun 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
What type is the array?
What is the size of each element in the array?
|

June 28th, 2003, 09:50 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|

June 29th, 2003, 12:54 AM
|
|
Junior Member
|
|
Join Date: Jun 2003
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
i got it up to 500...here is the code:
#include <iostream.h>
int main()
{
int theArray[500][500] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}};
for (int i = 0; i<500; i++)
for (int j=0; j<500; j++)
{
cout << "theArray[" << i << "][" << j << "]: ";
cout << theArray[i][j]<< endl;
}
return 0;
}
|

June 30th, 2003, 07:51 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Location: Bangalore, India
Posts: 13
Time spent in forums: 30 m 10 sec
Reputation Power: 0
|
|
|
The program crashes because it exceeds the size of memory heap.
When you declare a multidmensional array, the program reserves some space. When the size of array is large enough, it runs out of space and hence gives an error.
|

June 30th, 2003, 08:42 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
bes, you are delcaring it inside of a function (main() is a function, too). Declare it outside the function, as a global variable, and you will not be restricted by the stack space, which is normally 256 Kb (even though it sounds like its 1,024 Kb in your case - it can be changed with compiler settings). The global heap space is limited to 256 Mb for older 32-bit OS's, and limted only to 4 Gb for newer 32-bit OS's. Take a look at my first post above.
|

June 30th, 2003, 03:04 PM
|
|
Junior Member
|
|
Join Date: Jun 2003
Posts: 13
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
lol..sorry im new at this stuff...I just got that outta my C++ book
|

July 1st, 2003, 09:41 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
There is no need to be sorry... I just want to make it clear to everyone that there is a difference between declaring a variable inside of the main() function, and declaring it globally, outside of the main() function. This is a problem that caught me off guard, as well, so I know it will catch a lot of other people... I guess it is easy to think that you are not declaring variables on the stack when you declare them at will anywhere inside of main(), because it just does not seem like a function for some reason.
I should also note that dynamically allocating the arrays at run-time is another method of using all possible RAM with no complaints from the compiler.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|