+ 1

malloc and free- C++

I understand that malloc saves memory for some data, but what does it return? And does free() just does the opposite? Deletes the data from the memory?

14th Aug 2021, 2:48 PM
Yahel
Yahel - avatar
4 odpowiedzi
14th Aug 2021, 3:11 PM
Arsenic
Arsenic - avatar
0
malloc(): Allocates(registers) the amount of memory you requested. It returns the pointer to the memory that is allocated. The pointer is of type void*, so better cast it to the pointer type of your preference. free(): it deallocates the memory that the pointer you pass is holding. It's like it says the compiler: "Hey, I got some memory that I got earlier from you, but I don't need it anymore. Here, this pointer points to that memory, so that you can find it :D"
16th Aug 2021, 2:55 PM
Rishi
Rishi - avatar
0
Rishi so is it the same as using the "new" keyword and "delete" keyword?
16th Aug 2021, 2:58 PM
Yahel
Yahel - avatar
0
Yeah almost the same
17th Aug 2021, 1:50 AM
Rishi
Rishi - avatar