+ 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?
4 Réponses
+ 5
Docs explains best
malloc () : https://en.cppreference.com/w/c/memory/malloc
free () : https://en.cppreference.com/w/c/memory/free
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"
0
Rishi so is it the same as using the "new" keyword and "delete" keyword?
0
Yeah almost the same