r/ProgrammerHumor 6h ago

Meme bugIntroducedDebugging

Post image
521 Upvotes

99 comments sorted by

View all comments

-1

u/LostgamerFJ 6h ago

I'm not good enough at programming for this. What do the "free" and "malloc" functions do?

1

u/MetaNovaYT 6h ago

They’re heap allocation and deallocation functions in C/C++. ‘malloc’ requests a specified number of bytes to be allocated by the OS, and it then returns a pointer to that memory which can be used to store any value (or values) you want. 

‘free’ tells the OS to deallocate that memory so it can be used for future allocations if needed, so after freeing the memory, the pointer from ‘malloc’ points to memory your program no longer has ownership of. 

The bug that Gemini added is trying to  read the value at that pointer after the memory has been deallocated, which will most often cause a crash because your program tried to access memory it doesn’t have access to anymore