there are multiple ways to allocate memory dynamic with c:

  • malloc()
  • calloc()
  • realloc()
  • alloca() (not ANSI C standard but implemented in most c compilers, advantage: no need to call free() after variable end of life, the reserved space will be freed automatically (similar to python open(file))
  • RAM that was freed with free() is not immediately released back to the kernel, to (maybe) re-use it during the runtime of the program, when the program exits, the RAM is given back to kernel
  • if after malloc() the dev forgets to free() RAM it’s mostly affecting long running server services (get slower and slower because of “memory leaks”)
    • that’s why it is important to free reserved RAM directly after use and initialize all variables with NULL

conversation with ChatGPT: ram management: heap vs stack?

ChatGPT 4.0:

“In programming, memory management often involves two primary areas: the heap and the stack. These two regions serve different purposes and have distinct characteristics:

Stack Memory:

  1. Definition: Stack memory is used for static memory allocation, typically for variables declared inside functions, method calls, and primitive types.
  2. Structure: It follows a Last In, First Out (LIFO) order, meaning the most recently added item is the first to be removed.
  3. Allocation/Deallocation: Memory allocation and deallocation on the stack is automatic and efficient. When a function is called, memory for its local variables is allocated on the stack, and when the function exits, that memory is automatically deallocated.
  4. Size: Stack memory is generally limited in size, which can cause a stack overflow if too much data is pushed onto the stack (e.g., deep recursion or excessive local variables).
  5. Access Speed: Fast access due to the sequential nature of the stack.
  6. Lifetime: The lifetime of stack variables is bound to the function’s scope. Once the function exits, stack variables are destroyed.
  7. Usage Example: Local variables and function call information, such as return addresses and parameters.

Heap Memory:

  1. Definition: Heap memory is used for dynamic memory allocation, typically for objects and data structures that need to persist longer or vary in size.
  2. Structure: The heap is a larger pool of memory used for allocating memory blocks of arbitrary sizes.
  3. Allocation/Deallocation: Memory on the heap must be manually managed using functions like malloc()/free() in C/C++ or using new/delete in languages like C++ or automatic garbage collection in languages like Java and Python.
  4. Size: The heap is usually larger than the stack, allowing for more flexible storage. However, excessive allocation can cause heap fragmentation or out-of-memory errors.
  5. Access Speed: Slower access compared to the stack because heap allocations are scattered in memory, making them less cache-friendly.
  6. Lifetime: The lifetime of heap variables is not limited to the function scope; they persist until explicitly freed (or garbage collected).
  7. Usage Example: Large data structures like linked lists, trees, or objects that need to live independently of function calls.

Comparison:

Aspect Stack Heap
Memory Size Limited and fixed Large and variable
Access Speed Faster (more cache-friendly) Slower (requires pointer dereferencing)
Allocation Type Static (predefined at compile time) Dynamic (allocated during runtime)
Lifetime Function-bound Managed manually or by garbage collection
Fragmentation No fragmentation Can cause fragmentation
Use Case Function calls, local variables Long-lived objects, large data structures

When to Use Each:

  • Stack is ideal for small, short-lived variables such as function parameters and local variables.
  • Heap is necessary for larger data structures and objects whose lifetimes exceed the function scope or need dynamic resizing.”

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin