DEV Community

Kaustuv Pokharel
Kaustuv Pokharel

Posted on

Memory and Variables

We know what memory is when we think about it. But do we really know what it is. Now I am reading a book called "Memory as a programming concept in C and C++" where it teaches me about how memory is working or how system manages memory for C++.
Memory is a fundamental aspect of computer systems, crucial for storing and managing data during program execution. While the concept of memory may seem straightforward, delving into the intricacies of how it works and how variables interact with it can deepen our understanding.

In the realm of programming, variables act as data containers, holding information assigned to them. For instance, consider the declaration

#include <iostream>

int main() {
    // Variable declaration and assignment
    int a = 10;

    // Display the value and memory address of the variable
    std::cout << "Value of 'a': " << a << std::endl;
    std::cout << "Memory address of 'a': " << &a << std::endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

in C++. It seems straightforward, but when we examine the final machine code or load module after compilation, the notion of a variable appears to vanish. During program execution, there's no explicit trace of a variable.

A load module, a term encountered during the reading, is a memory section containing the compiled machine-level code derived from the source code. This code, in the form of executable files (.exe), dynamic link libraries (.dll), or shared objects (.so), is crucial for the execution of computer programs. Load modules encapsulate the compiled code, enabling easy distribution and execution across different systems.

Let's unravel how variables and memory intertwine. Memory, in its essence, can only store binary code. Variables, akin to buckets in memory, are memory segments that store data. However, these buckets can only store binary information. Consequently, anything assigned to a variable is converted into binary within the load module.

To understand how more data can be accommodated in memory, consider each variable as having a specific size. For instance, a char data type typically occupies 1 byte, while an int might take 2 bytes on a 16-bit processor, 4 bytes on a 32-bit processor, and 8 bytes on a 64-bit processor. It's akin to having different-sized buckets – if you try to pour more water (data) than the bucket can hold, it will overflow. The existence of various data types (bucket sizes) such as double, float, and long double helps manage this, preventing overflow.

#include <iostream>

int main() {
    // Memory overflow example
    short smallBucket = 30000;  // 2 bytes
    char overflow = 'A';       // 1 byte

    // Attempting to overflow the small bucket
    smallBucket = smallBucket + overflow;

    // Display the result (may lead to unexpected behavior due to overflow)
    std::cout << "Result of overflow: " << smallBucket << std::endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Here's an approximate breakdown of what might happen:

The value of overflow ('A') is promoted to its ASCII value (65 in decimal).
The sum of smallBucket and the ASCII value is calculated.
The result is stored back in smallBucket.
The unexpected behavior arises from the fact that adding the ASCII value of 'A' (65) to smallBucket might exceed the representable range for a short variable (which is typically -32768 to 32767). This leads to an overflow and undefined behaviour.

If you want to see the actual machine code, you can use a disassembler on the compiled executable. Keep in mind that the specific machine code will vary based on your compiler, platform, and compiler optimization settings.

When a program runs, the computer searches the memory segment for the designated bucket and then examines the binary content stored within. Only after this process does the system proceed to analyze the rest of the instructions. It's a sequential journey – finding the bucket (memory location), inspecting the binary contents (data), and then executing subsequent instructions.

In essence, the interplay between variables, memory, and the load module is a crucial aspect of understanding how a program operates at the machine level. This knowledge empowers programmers to write efficient, error-free code that maximizes the potential of the underlying hardware.

Top comments (0)