Shop OBEX P1 Docs P2 Docs Learn Events
Incorrect Malloc Operation? — Parallax Forums

Incorrect Malloc Operation?

SyncrosisSyncrosis Posts: 3
edited 2014-01-18 19:32 in Propeller 1
Hello All,

I'm doing some work with dynamic memory allocation, specifically linked list queues. I think I got my queue working just fine, but in testing it, I started testing the limits. I wanted to see if malloc would stop allocating memory if it was unavailable. It looks like malloc will go ahead and allocated memory beyond what is available. I put together this little test program to verify this:
#include "simpletools.h"                      // Include simple tools
#include "fdserial.h"


int main()                                    // Main function
{


  size_t num_bytes;
  int *ptr;


  pause(500);


  while(1)
  {
    printf("Allocate how many bytes?: ");
    scanf("%d", &num_bytes);
    printf("  ...allocating...\n");
    
    ptr = malloc(num_bytes);
    printf("Allocated %d bytes at %d (0x%08x)\n", num_bytes, (int)ptr, (int)ptr);


    free(ptr);
    printf("Deallocated %d bytes\n", num_bytes);
  
    printf("Going for another round!\n");
    
  }
}


Under SimpleIDE 0.9.45, the program compiles a CMM binary of 18964 bytes (19664 total). If you ask the program to allocate 16384 bytes, malloc will do it and return a pointer beyond the 32767 byte max (18964 + 16384 > 32767). As I understand it, malloc is supposed to return NULL if it cannot allocate the requested size. Am I missing something?

Thank you!

Comments

Sign In or Register to comment.