This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS: free() in ti rtos

Tool/software: TI-RTOS

Hello,

I allocated dynamic memory to a variable using the malloc().

And then I wanted to free the memory after use. So I used the free(). But then after that when I try to access the variable, I still get the value there, it is not freed.

char *temp = malloc(10 * sizeof(char));

.........Some code here to assign value to temp........

free(temp);

printf(temp) -> Here it still shows up the value

Is the free() not working then?

Regards,

Shyam

  • Hello!

    That is correct behaviour. Call to free() marks relevant memory block as unused in heap internals, but free makes no attempt to invalidate the pointer you used in your application, or somehow clear memory block being freed. Its up to programmer to invalidate the pointer. If your application is sensitive to what was left in freed memory block, that again is responsibility of the user to clean it.

    Hope this helps.

  • Hello rrlagic,
    Thanks for the reply.
    "If your application is sensitive to what was left in freed memory block, that again is responsibility of the user to clean it."
    I had used the free() in the sense that it frees up the unwanted memory that was allocated.
    So could you mention how I would perform the clean up other than this?

    Regards,
    Shyam
  • If you are okay with just informing the system unused block could be reclaimed back to the heap, you my stop right there.
    There is a practice in work with dynamic memory to check pointer before use, especially if it was passed as argument. In that case right after free(ptr) making it ptr = NULL; may probably save you from using deallocated block. In both cases whatever was left in that block will sit there till next allocation and use. If you are not okay with that, which is very specific situation I believe, you may set that block to zero/whatever just before free().
    Hope this helps.
  • Hello!
    free() just releases part of heap and does not delete, erase, zeros, xors , 0xA5 & 0x5A, etc.
    free() just releases means that next malloc() call could return address belonging to the previously freed memory block.
  • Thanks Tomasz for clearing my query.