2 min read
Notes on Memory

There are 2 types of memory, short lived and long lived. Short lived memory is called stack memory. It only lives within the function invocation and is automatically managed (allocation and deletion).

Heap memory on the other hand needs to be specifically allocated (malloc()) and deallocated (free())by the programmer and lives beyond the function invocation (on demand - dynamic). When the process is finished, the OS will then clean up the allocated memory.

types of errors

  • Segment Fault: OS keeps a few KB of memory (starting at address 0) empty and unmapped. This is a trap set specifically to catch programmers who forget to initialize their pointers.
  • Int* is basically like you get the memory address, but you want to know how much to increment the memory address by. An integer is 4 bytes, so it will tell you to increment by 4 bytes.
  • So when you try to access an index which is outside the allocated heap memory, it might access the heap, the head header, right? Which is actually invalid writes.
  • Memory leak is when you forget to have free memory.
  • free() does not usually “wipe” the data or remove the pointer’s ability to see that memory address; it simply tells the Operating System that the memory is now available for other things. Because the program hasn’t requested any new memory yet, the old data often remains physically sitting there in RAM.
  • Dangling pointer is when the pointer is still pointing to the same location, but that memory has been freed up for use so that other programs can use it.