r/C_Programming • u/JarvanSurfer77 • Jul 02 '26
Free memory of a static struct by pointer
SOLVED
I have two functions:
void debounce_set_IP(param_ip_old **old_val_ip,int initialize_count){
static int deb_counter;
static param_ip_old *old_val;
if(old_val_ip != NULL){
old_val = *old_val_ip;
}
//other condition....
if(initialize_count == 3){
deb_counter = 0;
*old_val_ip = (param_ip_old *) malloc(sizeof(param_ip_old));
old_val = *old_val_ip;
//(... other stuff ... )
}
// check debounce
if(debounce reached){
free(old_val); // i have also tried free(*old_val_ip)
}
}#############################
Void changVal(void){
//I save some old ip data into old_data and i ask the initialization of the variable
static param_ip_old *old_val = NULL;
static int deb_counter = 0;
//(...do something...)
if(old_val == NULL){
debounce_set_IP(&old_val,3);// here I request the malloc of old_val
}
//do other stuff
}##############################
void main(){
//many other things
debounce_set_IP(NULL,3); //i increment the debounce
//many other things
}Now when i do at the end of debounce in
Debounce_f free(old_data)
and go again in changVal i can still see it allocated. What am i doing wrong?
I tried both free(old_data) and free(old_data_app)
0
Upvotes
7
u/ByMeno Jul 02 '26
please format your code
11
u/flyingron Jul 02 '26
Calling free() on something that didn't come from malloc (or one of its related functions) is UNDEFINED BEHAVIOR. It's always best to make your allocation and deallocation reflections of each other to make sure things are paired up appropriately.
3
u/dmc_2930 Jul 02 '26 edited Jul 02 '26
You're dereferencing old_data in your malloc call. That's not correct.
Edit: I was misreading the code before OP reformatted it. Ignore the above.
2
u/SmokeMuch7356 Jul 02 '26
What do you mean you still see it allocated? How are you determining that?
freedoes not change what's stored inold_val(or*old_val_ip) - it won't set it toNULLor anything like that.old_valstill contains the last value written to it (the address of the previously-allocated memory). There's still storage at that address, you just don't "own" it anymore; trying to read or write to it may or may not work as expected (the behavior is undefined).If you want to make sure
old_valisNULLafter freeing it, you'll have to do that manually:which has no effect on
*old_val_ip; you'd have to manually null it out as well:Which brings us to the question - why are you creating the local
old_valvariable? What role does it play that*old_val_ipdoes not? Will it ever point to a different object than*old_val_ip?Various nits:
mainreturns anint, not avoid. Yes, you will seevoid main()used in many tutorials and references (even K&R) and many compilers won't complain about it (at least not without turning the warning levels up), but unless your compiler explicitly lists it as a valid signature the behavior is undefined. There historically have been platforms where programs that usedvoid main()would not load properly or would crash on exit. There are two (2) standard signatures formain:int main( void )int main( int argc, char **argv )An implementation may add other signatures, but they must be explicitly documented (N3220, 5.1.2.3.2).
Do not cast the result of
malloc. As of C89 it's not necessary,1 and under C89 it would suppress a useful diagnostic if you forgot to includestdlib.hor otherwise didn't have a declaration formallocin scope.2 The preferred way to write amalloccall isSo your malloc call should be
In K&R C
mallocreturned achar *, so a cast was necessary if the target was anything other than achar *variable:C89 introduced the
voidtype, along with the rule that avoid *could be converted to any other pointer type (and vice-versa) without needing an explicit cast. Anything that usedchar *as a "generic" pointer (likemalloc) was changed to usevoid *instead.Under C89, if you called a function that hadn't been previously declared the compiler assumed it returned an
int. So if you wrotewithout including
stdlib.h, you'd get a compile-time diagnostic that you couldn't assign anintto anint *, letting you know something was wrong. However, if you wrote:that cast would suppress the warning, and you wouldn't know anything was wrong until you got a runtime error. C99 got rid of implicit
intdeclarations so that particular issue is no longer a problem, but the cast is still an unnecessary maintainance burden; leave it off.1
u/mikeblas Jul 02 '26
I'm really struggling to understand what you're trying to do, and what it is you're actually observing. Maybe try to clarify your post with more details and color. But I do want to point this out in case you don't know: The variable in
old_valin thechange()function is entirely different than the variable namedold_valindebounce_set_IP()because of their static scoping.1
u/Key_River7180 Jul 03 '26
You free memory that is on the heap. That is, data that you malloc'ed, because data that is on the heap survives even after a function ends, while data on the stack dies when the function ends. Why do you want to free data on the stack?
2
u/Wertbon1789 Jul 02 '26
What do you mean by "I can still see it allocated"? If you mean that there's still an address in there, that's correct, nothing sets it to NULL, you need to do that yourself after the free call.
1
u/sciencekm Jul 02 '26 edited Jul 02 '26
My changes with *****