WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Intermediate · question 37 of 100

What is the difference between ’static’ and ’dynamic’ memory allocation in C++?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

In C++, memory can be allocated in two ways: static and dynamic memory allocation.

Static memory allocation refers to the allocation of memory at compile-time, which means that the memory is allocated before the program starts running. This type of allocation is typically used for variables with a fixed size and lifetime, such as global variables, static variables, and local variables declared with the static keyword. The memory for these variables is allocated in a dedicated area of memory called the data segment, which is separate from the stack and heap.

For example:

    #include <iostream>
    
    int main() {
        static int x = 10;  // static allocation
        int y = 20;  // automatic allocation
        
        std::cout << "x = " << x << std::endl;
        std::cout << "y = " << y << std::endl;
        
        return 0;
    }

In this example, x is declared as a static variable, which means that it is allocated in the data segment before the program starts running. y is declared as a regular local variable, which means that it is allocated on the stack when the function is called.

Dynamic memory allocation, on the other hand, refers to the allocation of memory at runtime, which means that the memory is allocated while the program is running. This type of allocation is typically used for objects with a variable size or lifetime, such as arrays and objects created with the new operator. The memory for these objects is allocated in a dedicated area of memory called the heap, which is separate from the stack and data segment.

For example:

    #include <iostream>
    
    int main() {
        int* p = new int(10);  // dynamic allocation
        
        std::cout << "p = " << *p << std::endl;
        
        delete p;  // release memory
        
        return 0;
    }

In this example, we use the new operator to allocate memory for an integer value 10 on the heap. We then use a pointer p to access the allocated memory. After we are done using the memory, we use the delete operator to release the memory back to the heap.

It’s important to note that dynamic memory allocation should be used with care, as it can lead to memory leaks and other memory-related errors if not managed properly. In general, it’s a good practice to use static memory allocation whenever possible, and to use dynamic memory allocation only when necessary.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics