help with static memory allocation !

C

Crimzon

I am using MSVC++ 6.0 compiler. I am declaring an array char
ch[5800][20]. Program works fine for these arbitrary sizes. But if I
make the size of the array bigger like ch[10000][20], the program
gives me an error message pertaining to the array size and doesnt
work.
Does it mean that the total size of the array allocated (10000 * 20)
should be less than 65535 or something like that ? The compiler help
file tells that the size of data type character is 1 Byte. Why is
there an issue with the total number of character data types allocated
? System cannot allocate so much bytes contiguously ? Is there a
simple way to overcome this problem using standard data type
declaration, without using malloc or new ? Is it an issue with near /
far pointers ?
System configs are as follows, 512 MB RAM on a INTEL 2405Mhz machine,
OS is WIN XP.
Thanks in advance
 
E

Eric Sosman

Crimzon said:
I am using MSVC++ 6.0 compiler. I am declaring an array char
ch[5800][20]. Program works fine for these arbitrary sizes. But if I
make the size of the array bigger like ch[10000][20], the program
gives me an error message pertaining to the array size and doesnt
work.
Does it mean that the total size of the array allocated (10000 * 20)
should be less than 65535 or something like that ? The compiler help
file tells that the size of data type character is 1 Byte. Why is
there an issue with the total number of character data types allocated
? System cannot allocate so much bytes contiguously ? Is there a
simple way to overcome this problem using standard data type
declaration, without using malloc or new ? Is it an issue with near /
far pointers ?
System configs are as follows, 512 MB RAM on a INTEL 2405Mhz machine,
OS is WIN XP.
Thanks in advance

This sounds like Question 16.3 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

If not, see Question 19.23. If that's still not the
problem, you may have encountered some limitation in
the compiler you're using -- and if that's the case,
seek your answer in an MSVC forum; comp.lang.c won't
have what you need.
 
C

CBFalconer

Crimzon said:
I am using MSVC++ 6.0 compiler. I am declaring an array char
ch[5800][20]. Program works fine for these arbitrary sizes. But if I
make the size of the array bigger like ch[10000][20], the program
gives me an error message pertaining to the array size and doesnt
work.
Does it mean that the total size of the array allocated (10000 * 20)
should be less than 65535 or something like that ? The compiler help
file tells that the size of data type character is 1 Byte. Why is
there an issue with the total number of character data types allocated
? System cannot allocate so much bytes contiguously ? Is there a
simple way to overcome this problem using standard data type
declaration, without using malloc or new ? Is it an issue with near /
far pointers ?
System configs are as follows, 512 MB RAM on a INTEL 2405Mhz machine,
OS is WIN XP.

System specific questions are off-topic here. The C standard
guarantees at most 65536 bytes of storage to be available. The
actual answer probably has something to do with default stack
sizes. You should see a newsgroup dealing with your compiler
and/or OS.
 
K

kal

(e-mail address removed) (Crimzon) wrote in message
I am using MSVC++ 6.0 compiler. I am declaring an array char
ch[5800][20]. Program works fine for these arbitrary sizes. But if I
make the size of the array bigger like ch[10000][20], the program
gives me an error message pertaining to the array size and doesnt
work.

Is it the program that is giving the error message? If so then
looking into the program code should tell you under what conditions
the message is given. One likely cause is erroneous code.
Does it mean that the total size of the array allocated (10000 * 20)
should be less than 65535 or something like that ?

No.

However, you cannot allocate large arrays as local variables (that
is in the stack.) The stack size is usually about 1MB. Such large
aggregates, if necessary, should be allocated with static storage.

On the other hand, it would be better to use dynamic memory
allocation for large storage requirements.
The compiler help
file tells that the size of data type character is 1 Byte. Why is
there an issue with the total number of character data types allocated
? System cannot allocate so much bytes contiguously ? Is there a
simple way to overcome this problem using standard data type
declaration, without using malloc or new ? Is it an issue with near /
far pointers ?
System configs are as follows, 512 MB RAM on a INTEL 2405Mhz machine,
OS is WIN XP.
Thanks in advance

The following works fine in MSVC++ 6.0.

<code>
#include <stdio.h>

int main(int argc, char* argv[])
{
static char ch[1024*1024][64];

ch[0][0] = '1';

ch[1024*1024-1][64-1] = ch[0][0] + 1;

printf("%c, %c\n",ch[0][0],ch[1024*1024-1][64-1]);

return 0;
}
</code>

</OT>
 
C

Crimzon

declaration similar to "static char ch[1024*1024][64];" solved the
problem. Though I don't understand at this instance what happens when
the char array is declared static.
Thank you very much !!!
 
K

kal

declaration similar to "static char ch[1024*1024][64];" solved the
problem. Though I don't understand at this instance what happens when
the char array is declared static.

I don't understand it well either but it works!

The static keyword here indicates that the variable has static
storage duration. That is to say, storage for the variable is
allocated when the program begins and deallocated when the
program ends.

If you will permit me to make a suggestion, if you are going to
do any programming at all using C, then you really ought to get
hold of a copy of that K&R book and read it in its entirety.
Thank you very much !!!

You are most welcome.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top