r/ProgrammerHumor 2d ago

Meme bugIntroducedDebugging

Post image
1.1k Upvotes

120 comments sorted by

View all comments

2

u/LostgamerFJ 2d ago

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

2

u/abc9hkpud 2d ago edited 2d ago

Malloc allocates memory. It takes as input the number of bytes, so in this case you need something like

Int* x = (int) malloc(1sizeof(int))

For an int array of length 1. Free is used to free the pointer.

After free, you shouldn't access the memory (or return it for someone else to use)

Modern C++ uses new and delete instead. Malloc and free are used in C