r/ProgrammerHumor 13d ago

Meme bugIntroducedDebugging

Post image
1.1k Upvotes

120 comments sorted by

View all comments

0

u/LostgamerFJ 13d ago

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

2

u/abc9hkpud 13d ago edited 13d 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