realloc problem?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

realloc problem?

1,391 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lumos on Fri Jun 14 15:11:09 MST 2013
Hello,
I am using  LPCXpresso v5.2.2 [Build 2108].
I am trying to use realloc function from stdlib.h (Redlib - semihost).

For test I am calling this part of code in the loop:

void* ptr1 = NULL;
void* ptr2 = NULL;
ptr1 = realloc(NULL, 300);
ptr2 = realloc(ptr1, 301);
//free(ptr1);
free(ptr2);

When deallocation of ptr1 is commented-out, code crashes very soon.
But, in case free(ptr1); is commented, code is running without any crash.
From description of realloc function, old space should be deallocated automatically, but from this test it looks different. Manual deallocation is needed.

Can someone explain whats going on here?
Thank you
0 Kudos
Reply
6 Replies

1,365 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lumos on Mon Jun 17 12:03:04 MST 2013

Quote: CodeRedSupport
It looks like you are correct - Redlib's realloc is not releasing memory correctly. We'll investigate for fixing in a future LPCXpresso release.



Yes, thank you for confirmation.
I am going to use own realloc function until fix will be available.
0 Kudos
Reply

1,365 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Mon Jun 17 03:15:25 MST 2013

Quote: Lumos
Hello,
I am using  LPCXpresso v5.2.2 [Build 2108].
I am trying to use realloc function from stdlib.h (Redlib - semihost).

For test I am calling this part of code in the loop:

void* ptr1 = NULL;
void* ptr2 = NULL;
ptr1 = realloc(NULL, 300);
ptr2 = realloc(ptr1, 301);
//free(ptr1);
free(ptr2);

When deallocation of ptr1 is commented-out, code crashes very soon.
But, in case free(ptr1); is commented, code is running without any crash.
From description of realloc function, old space should be deallocated automatically, but from this test it looks different. Manual deallocation is needed.



It looks like you are correct - Redlib's realloc is not releasing memory correctly. We'll investigate for fixing in a future LPCXpresso release.

Regards,
CodeRedSupport
0 Kudos
Reply

1,365 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Sun Jun 16 14:15:40 MST 2013
The definition for realloc states

" If the area pointed to was moved, a free(ptr) is done."

Thus, your free of ptr1 is NEVER correct.
0 Kudos
Reply

1,365 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ToBeFrank on Sun Jun 16 11:58:06 MST 2013

Quote: Lumos
Hello,
I am using  LPCXpresso v5.2.2 [Build 2108].
I am trying to use realloc function from stdlib.h (Redlib - semihost).

For test I am calling this part of code in the loop:

void* ptr1 = NULL;
void* ptr2 = NULL;
ptr1 = realloc(NULL, 300);
ptr2 = realloc(ptr1, 301);
//free(ptr1);
free(ptr2);

When deallocation of ptr1 is commented-out, code crashes very soon.
But, in case free(ptr1); is commented, code is running without any crash.
From description of realloc function, old space should be deallocated automatically, but from this test it looks different. Manual deallocation is needed.



Trying to free ptr1 is incorrect. As you you noticed, realloc deallocates the old space if a new memory location was needed. Why would you think you need to free ptr1 then? Your code crashes when free(ptr1) is uncommented because you're freeing a memory object twice.
0 Kudos
Reply

1,365 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lumos on Sat Jun 15 02:19:04 MST 2013

Quote: cfb
It looks like a misuse of void pointers to me. Try using typed pointers and cast the return result from realloc.



Typing of pointers does not have any effect.
But I tried to set Newlib (semihost) library type, without changing of code itself, ant it is working without problems.
So this leads me to the conclusion: realloc implementation in the Redlib (semihost) library type is not fully complaint with specification.

Please correct me if I am wrong.
0 Kudos
Reply

1,365 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cfb on Fri Jun 14 18:02:12 MST 2013
It looks like a misuse of void pointers to me. Try using typed pointers and cast the return result from realloc.
0 Kudos
Reply