Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Understanding Memory Management and the new Operator in C++

Notes May 15 34

Memory Layout in C++ Programs

During execution, a C++ program organizes memory into four primary regions:

  1. Code Segment: Stores compiled binary instructions, managed by the OS
  2. Global/Static Segment: Contains global variables, static variables, and constants
  3. Stack: Automatically managed memory for function parameters and local variables
  4. Heap: Dynamically allocated memory controlled by the programmer

Pre-Runtime Memory Areas

Before program execution begins, two memory regions are established:

Code Segment Characteristics

  • Contains CPU-executable machine instructions
  • Shared across processes to optimize memory usage
  • Marked read-only to prevent accidental modification

Global/Static Segment Contents

  • Stores global and static variables
  • Includes constant data (string literals, const globals)
  • Memory is released by the OS upon program termination

Memory Address Demonstration

// Global variables
int global_x = 20;
int global_y = 20;

// Global constants
const int const_global_x = 20;
const int const_global_y = 20;

int main() {
    // Local variables
    int local_x = 20;
    int local_y = 20;

    // Display addresses
    cout << "Local x: " << (long)&local_x << endl;
    cout << "Local y: " << (long)&local_y << endl;

    // Static variables
    static int static_x = 20;
    static int static_y = 20;
    
    cout << "Static x: " << (long)&static_x << endl;
    cout << "Static y: " << (long)&static_y << endl;

    // Constants
    cout << "String literal: " << (long)&"sample text" << endl;
    cout << "Global const x: " << (long)&const_global_x << endl;
    
    const int const_local_x = 20;
    cout << "Local const x: " << (long)&const_local_x << endl;

    return 0;
}

Runtime Memory Areas

Stack Memory Behavior

  • Automatically managed by the compiler
  • Stores function parameters and local variables
  • Warning: Never return pointers to stack-allocated data
int* badFunction() {
    int stack_var = 30;
    return &stack_var; // Dangerous!
}

Heap Memory Operations

  • Requires explicit allocation/deallocation
  • Primary allocation method: new operator
int* safeFunction() {
    int* heap_var = new int(30);
    return heap_var; // Valid
}

Dynamic Memory Allcoation with new

The new operator allocates heap memory and returns a typed pointer. Memory must be explicitly freed using delete.

Basic Usage Example

int* createInt() {
    int* val = new int(40);
    return val;
}

int main() {
    int* ptr = createInt();
    cout << *ptr << endl;
    delete ptr; // Critical cleanup
    return 0;
}

Array Allocation

For arrays, use delete[] to properly deallocate memory:

int main() {
    int* array = new int[5];
    
    for(int i = 0; i < 5; i++) {
        array[i] = i * 10;
    }
    
    delete[] array; // Array-specific deallocation
    return 0;
}
Tags: C++

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Spring Boot MyBatis with Two MySQL DataSources Using Druid

Required dependencies application.properties: define two data sources and poooling Java configuration for both data sources MyBatis mappers for each data source Controller endpoints to verify both co...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.