Hi Martin,
From your referenced link, i took the program and made slight modifications which i have posted below..
#include "stdio.h"
#include "stdlib.h"
int main()
{
printf("Start\r\n");
int i =409600;
printf("i is at %x\r\n", (long)(&i));
int j=0;
while(1)
{
printf("While:j is at %d.\n",j);
fflush(stdout);
void* test = malloc(i);
int i2 = 2;
printf("Mallocced %x bytes at %d, diff = %x\r\n", i, test, (long)(&i2) - (long)test);
if (test == NULL)
{
printf("Malloc failure..No Memory available\n");
break;
}
j++;
}
}
Below is the output:
Start
i is at bffffc64
While:j is at 0.
Mallocced 64000 bytes at 1076625416, diff = 7fd3fc50
While:j is at 1.
Mallocced 64000 bytes at 1077039112, diff = 7fcdac50
While:j is at 2.
Mallocced 64000 bytes at 1077452808, diff = 7fc75c50
.......................................
While:j is at 5201.
Mallocced 64000 bytes at -1978253280, diff = 35e9bc38
While:j is at 5202.
Mallocced 64000 bytes at -1977614312, diff = 35dffc40
While:j is at 5203.
Mallocced 64000 bytes at -1977204704, diff = 35d9bc38
While:j is at 5204.(It just stopped here..)
As mentioned above,it just stopped(literally Hanged forever) after printing 5204 on the console. The program niether printed malloc failure or exited... Why is it not entering the failure loop? Please share your thoughts on this?
Is there any progress on how "new" works when dynamically allocating an instance of a class?
As I understand it, when the allocation fails it should do one of two things. Either it should return NULL or an exception is generated. I suspect in this environment it will return NULL. In practice it just seems to hang. For example, in the following program:
It counts up to 192 and then stops, like this:
I haven't bothered to include the code for Rectangle.h but it doesn't do anything particularly clever or dangerous.
I have seen other posts about this and it has been suggested that this bug has been fixed (or should have been) but I am using the latest update of the mbed library and it still fails.
Any chance of a fix on this? I realise there are various ways of figuring out how much memory you have left but it would be a much cleaner solution if I could just check for NULL after a call to new.
I am using an LPC11U24.