Programming Memory

Memory Allocation

How can you notice a memory leak in real-life?

Taught to me by CY Chen, when you’ll notice a gradual increase in memory utilization.

Memory Leaks

You can still have memory leak in a program that has automatic memory management. Imagine a program that never terminates, always keeps requesting for more memory. Learned from Hemal Shah.

In C++

Use the new keyword.

Coord* p = new Coord {}; 
p->x = 10; 
p->y = 20; // play around 
delete p;

Do NOT Forget

Do NOT forget to free the memory at the end of the program by using the delete keyword, else you will have memory leak.

new vs. malloc?

In C++, you can still use malloc. The difference is that new allocates memory AND initializes.

Null pointers are represented by nullptr.

NEW Learning Jul-13-2023: Consider the following

Node* node;
delete node->left;
cout << node->left; // does NOT print nullptr (i.e. 0)
// you NEED to do this
node->left = nullptr;
cout << node->left; // works as intended, prints nullptr (i.e. 0)
  • I assumed that calling the delete keyword automatically sets it to a nullptr.
  • Rather, the pointer becomes a dangling pointer, meaning it still holds the memory address of the deallocated memory, but the memory itself is no longer valid for your program to use.

= delete keyword

= delete keyword

The = delete keyword is used to disable a certain function. Often used to disallow copying. Source

Disallow coying:

class X {
    X& operator=(const X&) = delete;  // Disallow copying
    X(const X&) = delete;

In C

We use malloc and free from stdlib.h library to allocate and deallocate memory from the Heap.

void *malloc(size_t size);  
// Allocates block of memory of size number of bytes but doesn't initialize. 
// Returns a pointer to it.
// If insufficient memory or size==0, returns NULL, the null pointer.
void free(void *p)  
// Frees a memory block that p is pointing at that was allocated by user (say using malloc). 

Failure to free the memory that you have allocated is called a memory leak.

More Functions (allocators)

void* calloc(size_t nmemb, size_t size) 
// Clear allocate.  
// Allocates nmemb elements of size bytes each initialized to 0
void* realloc(void *p, size_t size) 
// Resizes a previously allocated block  
// May need to create a new block and copy over old block contents.

From Hemal Shah

And we had a discussion about malloc, how stack and heap actually work. When a process starts, it gets stack allocated things.

We also talked about memory management

  • Modern OS will keep track of when memory is requested by a process, so that when the process dies, it will clean up that memory.
  • Older phones didn’t have that back then

And then I said: “Then where is the need to check for memory leak?” And he clarified how memory leaks is a whole different thing than automatic garbage collection.

  • Because you can have a program that doesn’t crash and continuously have memory leak. It will keep on leaking and leaking, requesting more and more memory, until the program sufffocates
  • So it’s important to still detect memory leaks, even though we have automatic garbage collection, because the program might not crash, and it will just continually leak memory.
  • Sidenote on valgrind: What valgrind does is that it creates a fake environment. Runs the program super slowly, and everything the program requests for memory, valgrind will remember it

How does malloc and free work under the hood?

They are not constant time operations. Kajanan told me that they are actually really complicated.

From ChatGPT:

  • malloc: Allocates a specified amount of memory by checking a list of free blocks. If a suitable block is found, it’s removed from the list and returned. If not, more memory is requested from the OS. It maintains metadata for memory management.
  • free: Deallocates memory allocated by malloc by adding the block back to the free list. It may merge adjacent free blocks (coalescing) to reduce Fragmentation. The function does not always return memory to the OS immediately.