What is the purpose of a memory address in C++?

The main memory (or simply the memory) is where variables and other information are stored while a program runs. Sometimes it is called the RAM, for random-access memory. From the perspective of a program, the computer's memory is a collection of bytes, each with an integer address. For example, there is a byte with address 1, another with address 2, etc., up to a very large number. A program can fetch the current contents of the byte at a given memory address and it can store a given value into that byte.

A byte is just 8 bits. Most of the data items that you use are larger than that. For example, a value of type int is usually 32 bits, so it occupies 4 bytes. A program refers to a block of memory using the address of the first byte in the block. For example, an integer stored in bytes 1000-1003 has address 1000.


Pointers and memory addresses as values

A memory address is called a pointer because you can think of it as pointing to a specific spot in memory.

From a machine language perspective, a pointer is the same as a long integer (32 bits or 64 bits, depending on the type of the executable program). So a program can treat a pointer as information in the same way that it treats an integer as information.

But C++ treats pointers a little differently from the way they are treated in machine language. Each pointer has a type that tells the type of thing in the memory that it points to. To write the type of a pointer, write an asterisk after another type. For example,

  • A value of type int* is a pointer to a location in memory that holds a value of type int. If memory address 1000 is thought of as having type int*, then the memory in addresses 1000-1003 contains a value of type int.

  • A value of type double* is a pointer to a location in memory that holds a value of type double.

  • A value of type char* is a pointer to a location in memory that holds a value of type char. So it refers to a single byte.

  • A value of type char** is a pointer to a location in memory that holds a value of type char*. That is, it points to another pointer. You can add a * to any type, including a pointer type.


Getting the address of a variable

If x is a variable then &x is the address where x is stored. So

  int v;
  int* p = &v;
makes variable p hold the address of variable v. Notice that, if v has type T, then &v has type T*.

The null pointer

Memory address 0 is called the null pointer. Your program is never allowed to look at or store anything into memory address 0, so the null pointer is a way of saying "a pointer to nothing". Note that a null pointer is not the same as a ; do not confuse the two.

Write a null pointer as NULL in your program. For example,

  char* p = NULL;
creates a pointer variable p and makes it hold memory address 0.

It turns out that NULL is not really part of C++. It is added using the preprocessor by defining it in certain , including in,. To get around that unpleasantness, the newest version of C++ defines a constant nullptr that is the null pointer. But many compilers to not recognize that yet. Please just use NULL. (optional)


Type void*

A value of type void* is a pointer to an unknown type of thing. That type is used in some places where a program needs to remember where something is stored without knowing what it points to. Constant NULL is defined to be (void*)(0). That is, convert 0 to type void*.


Exercises

  1. What sort of thing is stored in a variable of type long*? Answer

  2. Suppose that w is a variable of type long. Write a statement that creates a variable q of type long* and makes q hold the address of variable w. Answer

    In other words, variables are used to manipulate the values stored in memory whereas pointers are used to manipulate the variable addresses(memory location).

    Let's visualize the computer memory first.

    In computer memory, the basic memory unit is a byte (8 bits).

    Each byte will have unique memory address. For the sake of understanding, we will represent memory address using decimal value.

    Pictorial Explanation

    What is the purpose of a memory address in C++?


    In the above diagram, each block is a byte which consists 8 bits.

    And the memory address are linear and each byte will have a unique address.

    To store a character, the computer will allocate 1 byte of memory which is 1 * 8 = 8 bit.

    To store an integer or a float, the computer will allocate 4 byte of memory which is 4 * 8 = 32 bit.

    To store a double value, the computer will allocate 8 byte of memory which is 8 * 8 = 64 bit.




    Animated Tutorial

    Next topic we will understand, what will happen in the memory, if we declare a variable and assign a value to it.

    What is the purpose of memory address?

    A memory address is a unique identifier used by a device or CPU for data tracking. This binary address is defined by an ordered and finite sequence allowing the CPU to track the location of each memory byte.

    What is the use of address of operator in C?

    An address-of operator is a mechanism within C++ that returns the memory address of a variable. These addresses returned by the address-of operator are known as pointers, because they "point" to the variable in memory. The address-of operator is a unary operator represented by an ampersand (&).

    What is called memory address?

    A memory address is an exact assigned location in RAM used to track where information is stored. On a single computer memory IC, there can be 1 million, 2 million, or more memory addresses that can be accessed at randomly, which is why memory is called RAM (random access memory).