help with static memory allocation !

Discussion in 'C Programming' started by Crimzon, May 26, 2004.

  1. Crimzon

    Crimzon Guest

    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
     
    Crimzon, May 26, 2004
    #1
    1. Advertising

  2. Crimzon

    Eric Sosman Guest

    Crimzon wrote:
    > 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.

    --
     
    Eric Sosman, May 26, 2004
    #2
    1. Advertising

  3. Crimzon

    CBFalconer Guest

    Crimzon wrote:
    >
    > 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.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, May 26, 2004
    #3
  4. Crimzon

    kal Guest

    (Crimzon) wrote in message news:<>...

    <OT>

    > 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>
     
    kal, May 27, 2004
    #4
  5. Crimzon

    Crimzon Guest

    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 !!!

    (kal) wrote in message news:<>...
    > (Crimzon) wrote in message news:<>...
    >
     
    Crimzon, May 27, 2004
    #5
  6. Crimzon

    kal Guest

    (Crimzon) wrote in message news:<>...
    > 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.
     
    kal, May 28, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    1
    Views:
    421
    Jakob Bieling
    Jul 24, 2003
  2. Ken
    Replies:
    24
    Views:
    3,952
    Ben Bacarisse
    Nov 30, 2006
  3. Replies:
    53
    Views:
    3,147
    David Thompson
    Feb 26, 2007
  4. chris
    Replies:
    6
    Views:
    1,033
    chris
    Oct 28, 2005
  5. Replies:
    5
    Views:
    656
    Chris Thomasson
    Aug 23, 2007
Loading...

Share This Page