GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Apr-2006, 00:53
prasath prasath is offline
New Member
 
Join Date: Aug 2005
Posts: 4
prasath is on a distinguished road

reasons why malloc fails?


hi,

will someone point me the reasons why a call to malloc fails even if we have enough memory ?
  #2  
Old 05-Apr-2006, 05:17
Brenton S. Brenton S. is offline
Awaiting Email Confirmation
 
Join Date: Mar 2006
Location: Piscataway, NJ
Posts: 21
Brenton S. is on a distinguished road
Smile

Re: reasons why malloc fails?


Hi prasath,

From what I know, malloc() would only fail if there was insufficient memory or if the size requested was 0.

Is malloc() not working properly for you?
  #3  
Old 05-Apr-2006, 09:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: reasons why malloc fails?


Quote:
Originally Posted by prasath
hi,

will someone point me the reasons why a call to malloc fails even if we have enough memory ?

What is "enough memory"?

malloc() will fail if it can't allocate enough contiguous memory to satisfy the request. It is possible to malloc and free memory in such a way as to leave the space fragmented so that allocation of a block of a given size is not possible even though the total amount is OK:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

#define NBLOCKS 5000

int main()
{
  int chunk1 = 1024*1024; /* let's do it a megabyte at a time */
  int chunk2;
  int i;  /* generic loop index */
  int n1; /* number of chunk1 blocks from first  allocation */
  int f1; /* number of chunk1 blocks freed                  */
  int n2; /* number of chunk2 blocks from second allocation */
  int f2; /* number of chunk2 blocks freed                  */
  char *pointer1[NBLOCKS]; /* array of pointers */
  char *pointer2[NBLOCKS]; /* array of pointers */

  for (i = 0; i < NBLOCKS; i++) { /* make really, really sure that they are all zero */
    pointer1[i] = NULL;
    pointer2[i] = NULL;
  }

  for (n1 = 0; n1 < NBLOCKS ; n1++) {
    pointer1[n1] = malloc(chunk1);
    if (!pointer1[n1]) { /* malloc failed: returned NULL */
      break;
    }
  }
  printf("1: Allocated %4d chunks of %d bytes.\n", n1, chunk1);

  f1 = 0;
  for (i = 0; i < NBLOCKS; i += 2) { /* free every other block */
    if (pointer1[i]) {
      free(pointer1[i]);
      pointer1[i] = 0;
      f1++;
    }
  }
  printf("1: Freed     %4d chunks of %d bytes.\n\n", f1, chunk1);

#if 0
  chunk2 = chunk1;
#else
  chunk2 = 2 * chunk1;
  printf("New chunk size = %d\n\n", chunk2);
#endif

  for (n2 = 0; n2 < NBLOCKS; n2++) {
    pointer2[n2] = malloc(chunk2);
    if (!pointer2[n2]) {
      break;
    }
  }
  printf("2: Allocated %4d chunks of %d bytes.\n", n2, chunk2);

  f2 = 0;
  for (i = 0; i < NBLOCKS; i++) {
    if (pointer2[i]) {
      free(pointer2[i]);
      f2++;
    }
  }
  printf("2: Freed     %4d chunks of %d bytes.\n\n", f2, chunk2);

  f1 = 0;
  for (i = 0; i < NBLOCKS; i++) {
    if (pointer1[i]) {
      free(pointer1[i]);
      f1++;
    }
  }
  printf("1: Freed     %4d chunks of %d bytes.\n", f1, chunk1);

  return 0;
}

My output:
Code:
1: Allocated 1926 chunks of 1048576 bytes. 1: Freed 963 chunks of 1048576 bytes. New chunk size = 2097152 2: Allocated 0 chunks of 2097152 bytes. 2: Freed 0 chunks of 2097152 bytes. 1: Freed 963 chunks of 1048576 bytes.

So: Even though 963 megabytes had been deallocated by the time we got to the second allocation loop, there were no blocks large enough to allocate 2 megabytes.

When I changed #if 0 to #if 1 (on line 41), here is what I saw:

Code:
1: Allocated 1926 chunks of 1048576 bytes. 1: Freed 963 chunks of 1048576 bytes. 2: Allocated 963 chunks of 1048576 bytes. 2: Freed 963 chunks of 1048576 bytes. 1: Freed 963 chunks of 1048576 bytes.

Now, the blocks are available in loop 2, one megabyte at at time.

Regards,

Dave
Last edited by davekw7x : 05-Apr-2006 at 10:35.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't free after malloc nusstu C Programming Language 1 20-Apr-2005 09:28
CListCtrl problems - FindItem fails after Sort Items dotygr MS Visual C++ / MFC Forum 0 03-Mar-2005 14:23
segfault on malloc (C) spike666 C Programming Language 8 15-Jul-2004 14:45
malloc function Rosmayati C Programming Language 5 18-May-2004 20:08
Apache Make Fails beardo Apache Web Server Forum 6 09-Mar-2004 10:44

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:47.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.