Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C free memory on stack

I created some utilities which help me to handle the management of a DinamicList. In the section that I use to handle the removing of a element in a list, if there is a element added that is stored in the stack, when I call free() an undefined behaviour is reached.

Surfing on the net I found out that there aren't ways to determine whether a pointer points to stack memory or heap memory.

So I think that to solve this problem I have to handle the error generated from free(). Is there a way to handle that exception when I call free()?

like image 496
P.Carlino Avatar asked Mar 05 '23 00:03

P.Carlino


1 Answers

No.

You need to not call free() for non heap pointers. Easiest way is let whoever allocated the memory take care of freeing it. I.e. your utilities look after whatever memory they allocate but someone else looks after the memory passed to your utilities.

like image 109
John3136 Avatar answered Mar 10 '23 09:03

John3136