orangeduck / tgc

A Tiny Garbage Collector for C
Other
968 stars 64 forks source link

how do i override a pointer to avoid memory leak #25

Open olwethumlimi opened 1 year ago

olwethumlimi commented 1 year ago

e.g

char *  message;
 message = tgc_alloc(&gc, 10); // it allocate
 strcpy(message, "test"); 
 message ="update" // override the value, 
RolandMarchand commented 1 year ago

In your example at line 4, the pointer from line 2 became unreachable., since message is now pointing at the string literal "update". The memory allocated at line 2 will be freed automatically.

If you're really concerned about memory leaks, you can run tgc_free() on the original pointer.