memory allocation problem with 2 dimensional array

This is a discussion on memory allocation problem with 2 dimensional array within the C++ Programming forums, part of the General Programming Boards category; I need to define two very large 2 dimensional arrays. The following statement compiles fine, but it seems to kill ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    memory allocation problem with 2 dimensional array

    I need to define two very large 2 dimensional arrays. The following statement compiles fine, but it seems to kill my system when it is run.

    short retr[256][512],appr[256][512];

    I've compiled and run successfully on both nt and osx when the statement is as follows:

    short retr[100][512],appr[100][512];

    My best guess is that I have overloaded my memory. How do I get around this problem and/or allocate more memory at runtime? Thanks,

    nn3a




    Note: Compiler is codewarrior for osx and windows.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    try allocating the memory for the arrays on heap instead of stack. Use malloc().

    Code:
    /* instead of: short retr[100][512],appr[100][512];  */
    
    short *retr = NULL;
    short *appr = NULL;
    
    retr = (short *) malloc( sizeof(short) * 100 * 512 );
    if ( retr == NULL )
    {
       // handle error appropriately...
    }
    
    appr = (short *) malloc( sizeof(short) * 100 * 512 );
    if ( appr == NULL )
    {
       // handle error appropriately...
    }
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > short retr[256][512],appr[256][512];
    sizeof(short) * 2 * 256 * 512 = 1/2Mbyte

    Are you declaring these variables inside a function (like main)?
    If so, then you're probably exceeding the stack limit

    Try this
    static short retr[256][512],appr[256][512];
    It keeps the scope local to the function you're in, but the memory isn't allocated from the stack.

    Ruchikar's solution might work, but you lose the [x][y] subscripting ability.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    > > How do I get around this problem and/or allocate more memory at runtime?

    If you are trying to dynamically allocate memory, malloc() and realloc() are the options for you.

    One of the main difference between:
    Code:
    short arr[100][100];
    and
    Code:
    short *arr = NULL;
    arr = (short *) malloc( sizeof(short) * 100 * 100 );
    is that in the latter case you can change the size of the allocated memory using realloc().

    However, both are equivalent in terms of accessing the allocated memory. You can access arr[x][y] in both the cases.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    Thanks for the suggestions everyone. I had tried declaring the variables as static before and was still running out of memory. Once I found the target settings in the compiler I was able to change the amount of memory allocated to the stack and heap. For now I'm going to stick with the declarations
    Code:
        short retr[256][512],appr[256][512];
    as I seem to be unable to use the [x][y] scripting when using the malloc() on my platform. My compiler doesn't like me calling to the 2D array when it was declared
    Code:
        short *retr = NULL;
        arr = (short *) malloc( sizeof(short) * 256 * 512 );
    Again, thanks for your help,

    nn3a

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    mea culpa

    I'm sorry, I was wrong. On none of the platforms and compilers that I use, can a double subscript be applied to a malloc()ed pointer.

    However, a single subscript can. That's why I wrote so cocksuredly - "both are equivalent in terms of accessing the allocated memory"

    Thanks for the enlightenment.

    Coming to your problem, I think filling up stack with large variables is not all that good an idea.

    A messy solution is: use malloc() to allocate memory in heap, define accessor/mutator functions to manipulate this memory. Something like:
    Code:
    modify_arr( x, y, value );
    val = fetch_arr( x, y );
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    Re: mea culpa

    Originally posted by Ruchikar
    Coming to your problem, I think filling up stack with large variables is not all that good an idea.
    I am worried about the same thing. Eventually I'm going to need 16 of these 2D arrays and my stack would be way too overloaded. Setting these declarations into the global moves everything to the heap, correct? I just need to make sure I carve out enough memory to run everything. Thanks you've all been a lot of help.

    nn3a

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    If you really want to allocate a 2D array, using malloc, and keep the ability to use [x][y] indexing, then do this

    short retr[256][512];

    Would be
    short (*retr)[512];
    retr = malloc ( 256 * sizeof(*retr) );

    Yes, all the () are necessary

    This only works if the minor dimension (512) is a compile time constant. If you want something which varies at runtime - ask

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  2. Memory allocation problem
    By spank in forum C Programming
    Replies: 3
    Last Post: 04-19-2006, 01:37 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. memory allocation problem....help..
    By CyC|OpS in forum C Programming
    Replies: 8
    Last Post: 10-18-2002, 09:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21