Metadata

  • 📅 Date :: 12-06-2025
  • 🏷️ Tags :: cpp

Notes

Detailed Notes: Introduction to Memory Addresses in C++

1. What is a Memory Address?

  • A memory address is a unique location in a computer’s memory where data is stored.
  • Every variable in C++ is stored at a particular memory location.
  • The memory address of a variable is where the variable’s data resides in the computer’s memory.

2. The Address-of Operator (&):

  • To access the memory address of a variable in C++, the address-of operator (&) is used.
  • Syntax: &variableName.
  • This operator returns the memory address where the variable’s data is stored.

3. Variables and Their Memory Addresses:

  • In the provided example, we have three variables:
    • string name = "John Doe"; - A string variable.
    • int age = 25; - An integer variable.
    • bool student = true; - A boolean variable.
  • These variables are stored at different memory locations.

4. Displaying Memory Addresses:

  • To show the memory addresses, the program uses std::cout and the & operator:

    std::cout << "Address of name: " << &name << std::endl;
    std::cout << "Address of age: " << &age << std::endl;
    std::cout << "Address of student: " << &student << std::endl;
     
  • Each time the program runs, the memory addresses might differ since they depend on the system’s memory allocation at runtime.

  • These memory addresses are displayed in hexadecimal format, which is a base-16 number system commonly used to represent memory addresses.

5. Understanding Hexadecimal and Decimal Conversion:

  • Memory addresses are typically represented in hexadecimal format (e.g., 0x7fffdc4c4a50).
  • However, these hexadecimal numbers can be converted to decimal format (base 10).
    • Example: The hexadecimal address 0x7fffdc4c4a50 converts to a decimal address.
  • This is just for easier visualization of the address, as it is not commonly used in practical programming. In real applications, hexadecimal is standard for working with memory addresses.

6. Memory Allocation and Data Types:

  • Different data types in C++ occupy different amounts of memory.
    • Booleans (bool) take 1 byte.
    • Integers (int) typically take 4 bytes.
    • Strings (string) can take a variable amount of memory depending on the length of the string.
  • The memory size used by the data type affects the distance between the memory addresses of variables.
    • For instance, the gap between the memory addresses of name (string) and age (int) is 4 bytes, as integers take 4 bytes of memory.
    • The gap between age and student (bool) is only 1 byte, as booleans occupy 1 byte.

7. Memory Address Gaps:

  • The gap between the memory addresses of variables is influenced by the size of each data type:
    • In the example, the difference between the memory address of name and age is 4 bytes (because an integer occupies 4 bytes of memory).
    • The difference between the addresses of age and student is only 1 byte, because a boolean takes only 1 byte of memory.

8. Significance of Memory Addresses:

  • Understanding memory addresses is essential for low-level programming and working with pointers (which will be explained in the next topic).
  • Memory addresses allow us to manipulate data at specific locations in memory, leading to more efficient programs.

9. Summary:

  • Memory addresses are locations where variables are stored in memory.
  • The address-of operator (&) gives the memory address of a variable.
  • Variables can take up different amounts of memory, and the gaps between their memory addresses depend on the size of the data types.
  • The hexadecimal format is used to represent memory addresses.
  • Understanding memory addresses is crucial for pointers and low-level memory management in C++.

Example Code:

Here’s a simple code snippet to demonstrate memory addresses:

#include <iostream>
#include <string>
 
int main() {
    // Declare variables
    std::string name = "John Doe";
    int age = 25;
    bool student = true;
 
    // Display the memory addresses
    std::cout << "Address of name: " << &name << std::endl;
    std::cout << "Address of age: " << &age << std::endl;
    std::cout << "Address of student: " << &student << std::endl;
 
    return 0;
}
 

Key Takeaways:

  • Memory Address: The location where a variable is stored in memory.
  • Address-of Operator: & used to get the memory address of a variable.
  • Hexadecimal Representation: Memory addresses are typically displayed in hexadecimal format.
  • Memory Gaps: Different data types use different amounts of memory, resulting in varying gaps between the memory addresses of variables.

References