Memory too much?

This is a discussion on Memory too much? within the C Programming forums, part of the General Programming Boards category; Hi everyone, I am trying to compile a c program with the following float declaration: Code: int function(){ float M[256][256][9]; ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    11

    Memory too much?

    Hi everyone,

    I am trying to compile a c program with the following float declaration:

    Code:
    int function(){
      float M[256][256][9];
    }
    I compile just fine (gcc filename.c, no other arguments), but when my program gets to that line where it creates the variable, the program crashes.

    I am using CGG on Windows XP (this version: MinGW Distro - nuwen.net) and I get that "a.exe has encountered a problem and needs to close, please tell Microsoft about this blah blah".

    Is there a limit of how big I can assign a memory space? If I declare "float M[256][256][7]" I get no issues, but changing that 7 to an 8 or a 9 crashes my program.

    I reallyyyyyyy would like to be able to have that 9 in there, otherwise my code becomes much more of a chore to write.

    And yes, I really do need to store 9 float attributes for each position in a 256x256 grid. Actually, I need 3 of these 3D-arrays of size [256][256][9] if possible.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Create the memory on the heap instead of the stack.

  3. #3
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    576kb is a bit much there, sport. Use the heap, as suggested by bithub, but also you be able to do whatever you are doing another way. Though I could be wrong....

    Code:
    typedef float[256][256][9] floatArray;
    
    int function(void)
      floatArray *m = malloc(sizeof(floatArray));
    
      free(m);
    }
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    Quote Originally Posted by bithub
    Create the memory on the heap instead of the stack.
    This appears to work, I will give it a go, thanks!


    Quote Originally Posted by cooloorful
    576kb is a bit much there, sport. Use the heap, as suggested by bithub, but also you be able to do whatever you are doing another way. Though I could be wrong....
    Oh, I realize that 576kb is a lot, but it really is a lot nicer to do this. I know, lots of people over-do it with their array declaration sizes, and I was expecting someone to say this, but in this case, yes, it is really helpful. As for your code suggestions, I will give that a try if defining the variables in the heap gives me problems. Thanks!

  5. #5
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    576kb isn't a lot of RAM, but it is kind of a lot when you are considering the stack sizes of most systems. There is no hard and fast rule for when to allocate memory or when to just use the stack. 64k seems like a lot. 4k is a lot for an embedded system. But those are guidelines at best.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    The size will be more than 576kb, a float size is 4 bytes.

    576kb * 4 = 2.3mb

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Scarlet7 View Post
    The size will be more than 576kb, a float size is 4 bytes.

    576kb * 4 = 2.3mb
    Yeah, and 2MB is a pretty common stack size.

  8. #8
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Good catch, Scarlet7. I guess your (presumeably compiled with debugging options) stack apparently capable of holding 1.75Mb without an issue. As Scarlet7 indicated though 2.25MB is high by any standard. Even with a 2MB stack, an array of [256][256][8] would be an issue since its exactly 2MB.
    Last edited by Cooloorful; 07-29-2009 at 03:11 PM.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    Quote Originally Posted by bithub View Post
    Yeah, and 2MB is a pretty common stack size.
    So is 1 MB. It's the default for Windows threads, I believe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM

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