Where do your variables live?

Let's discuss stack and heap memory allocation in C with some sample code.

  1. Stack Allocation: Stack memory is used for storing local variables and function call information. The compiler automatically manages it.

     #include <stdio.h>
    
     // when stackExample is called, arg is allocated on the stack
     void stackExample(int arg) {
         int a = 10; // Variable 'a' is allocated on the stack
         printf("Stack Variable: %d, %d\n", a, arg);
     }
    
     int main() {
         stackExample(20);
         return 0;
     }
    

    In this example, the variables a and arg are allocated on the stack inside the stackExample function. The memory for a and arg is automatically released when the function returns.

  2. Heap Allocation: Heap memory is used for dynamic memory allocation, and it must be managed manually at runtime. The functions malloc, calloc, and realloc are used to allocate memory on the heap, and free is used to deallocate memory.

     #include <stdio.h>
     #include <stdlib.h>
    
     void heapExample() {
         int *b = (int *)malloc(sizeof(int)); // Allocate memory on the heap
         if (b == NULL) {
             fprintf(stderr, "Memory allocation failed\n");
             return;
         }
    
         *b = 20; // Assign a value to the heap-allocated variable
         printf("Heap Variable: %d\n", *b);
    
         free(b); // Deallocate memory when done
     }
    
     int main() {
         heapExample();
         return 0;
     }
    

    In this example, memory for the pointer variable b is allocated on the stack but the address it contains belongs to the heap, and that area of memory is allocated dynamically by using malloc. It's important to free the allocated memory using free to avoid memory leaks.

    Remember that stack memory is managed automatically, and its size is typically limited. On the other hand, heap memory needs to be explicitly managed, but it provides more flexibility and can be used for dynamic data structures.

Note: In modern C, it's common to use calloc instead of malloc for allocating memory, as it initializes the allocated memory to zero. Also, be cautious when using pointers and dynamic memory allocation to avoid memory leaks and undefined behavior.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.