9.2: Pointer and Dynamic Memory Allocation

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under not declared, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    9.2: Pointer and Dynamic Memory Allocation

    Why do we need a pointer? Why do we need to store the address of a memory cell? There are no simple answers to those questions. We may need to take next programming class to fully understand the power of C++ pointers. Here, we just discuss one of the applications of pointers.

    Memory Dynamical Allocation

    In a C++ program, we often need to declare a variable or an array for storing data.

    int a;

    int myArray[1000];

    No matter where we put those declaration statements in the program, the memory cell that a or myArray represent will be locked to the program as soon as the program is built even the program may not use those memory cells until the end of the program. If this program runs for days or weeks, then those memory cells will be locked the whole time and no other program or apps can use them, this is going to be a waste of computer resources. Is there any way that we can wait until we actually need those memory cells to declare/allocate them? The. answer is YES. We can use following statements to dynamically allocate a single memory cell or an array of memory cells:

    int *p;
    p = new int;

    or

    int *p;
    p = new int[1000];

    We first need to declare a pointer p, then use a "new-statement", p = new int; this statement dynamically allocate an integer memory cell to the program and send the address of this memory cell to the pointer p. So pointer p is holding the address of this memory cell. And this won't happen until the new-statement is executed. If we don't need this memory cell toward to the end of the program, we can use such new-statement to allocate a memory cell at the time when we need it.

    Once p is holding the address of this memory, we can use following statement to assign an integer into this memory:

    *p = 20;

    or we can use following statement to display the integer in the memory:

    cout<<*p<<endl;

    Similarly, We can also use a new-statement to dynamically allocate an array of memory cells to a program:

    int *p;
    p = new int[1000];

    We can even allow a user to determine the size of the array:

    int length;
    int *p;
    cout<<"Please enter the length of the array: ";
    cin>>length;
    p = new int[length];

    Once we use a pointer and a new-statement to allocate an array, the address of the first element of the array will be assigned into the pointer. We can use the name of the pointer as the name of the array, for example, once we use above statements to declare an integer array, the address of the first element is assigned into the pointer p, and we can use p as the name of the array, thus we can use following looping statement to store data into this array:

    for(int i=0; i< length; i++){
    cout<<"Please enter an integer:\n";
    cin>>p[i];
    }

    We can also use following statement to display the data stored in the array:

    for(int i=0; i< length; i++){
    cout<<p[i]<<endl;
    }


    9.2: Pointer and Dynamic Memory Allocation is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.