Thread: question about malloc

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    45

    question about malloc

    i notice that when my program outputs files that are >1GB in size, it always crashes with a 'segmentation fault'. i was told that this is because my computer has a RAM of 1GB, so if my program uses malloc to allocate memory of >1GB, then this exceeds the RAM and is the cause of the crash.

    is that right? does that mean that the RAM actually limits the size of files that a c program can work with? if so, is there a way to overcome this RAM-imposed limit?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by bored_guy View Post
    i notice that when my program outputs files that are >1GB in size, it always crashes with a 'segmentation fault'. i was told that this is because my computer has a RAM of 1GB, so if my program uses malloc to allocate memory of >1GB, then this exceeds the RAM and is the cause of the crash.

    is that right? does that mean that the RAM actually limits the size of files that a c program can work with? if so, is there a way to overcome this RAM-imposed limit?
    Depends on the storage allocated inside your program to store the file contents. Use mmap() to load the file piece-meal; and post the code snippet.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    the code looks something like this:

    Code:
    float *in1 = malloc(size * sizeof(float));
    float *in2 = malloc(size * sizeof(float));
    float *in3 = malloc(size * sizeof(float));
    ...
    where size can be as large as 100million. i have to declare quite a number of such mallocs in my program.

    i think doing it piece-meal for me might not work, because of the BSQ/BIL arrangement of my multi-band images.

  4. #4
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    So each malloc needs around 382 MiB of RAM...(32 bit float = 4 bytes, 400 million bytes). If you're doing all those mallocs at the same time, yeah you'll run out of memory.

    As itCbitC said, RAM doesn't have to limit the size of the output file though, just depends on the program. If you're going to keep the entire contents of the file in memory, then yeah, RAM is going to limit how big of a file you can make.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  4. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 05:54 PM
  5. Question about malloc()
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 10:57 AM