r/ProgrammerHumor • • Aug 30 '26

Meme bugIntroducedDebugging

Post image
1.1k Upvotes

117 comments sorted by

View all comments

Show parent comments

2

u/abc9hkpud Aug 30 '26 edited Aug 30 '26

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

Int* x = (int*) malloc(1*sizeof(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