Incorrect Malloc Operation?
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:
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!
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
I'll have to build some mechanism in my code to limit the number of queue nodes it can create. Fortunately, without printf turned on, I still have a pretty reasonable amount of space, so I shouldn't run into the case where I run out of available queue nodes.