Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

13. [15 points] What does memory looks like after the following C++ code is exec

ID: 3889518 • Letter: 1

Question

13. [15 points] What does memory looks like after the following C++ code is executed? What is printed? If any of the lines would cause a compile-time error, state so, cross that line of code out, and write the memory diagram and program output as if that line were commented out. Since we are only dealing with ints (and pointers/references to such) here, you can write ain a spot in memory to signify that it is not initialized. int a 7; int *b = &a; int &c; = a; int d[5] = { 1, 2, 3, 4, 5 }; int *e = new int [5]; int *f NULL ; int &g; *f; c = b; cout

Explanation / Answer

#include<iostream>
using namespace std;
  
int main()
{
    int a = 7;   //a is allotted a memory for integer, with value 7.
    int *b = &a; //b is allotted a memory for integer pointer, and initialized with address of a.
    int &c = a;   //Sets the a's address to c's address, i.e., the same address is aliased with c. (Like giving 2 names.)
    int d[5] = {1, 2, 3, 4, 5}; //d is allotted memory for an array of 5 integers, and are initialized with values, 1 through 5.
    int *e = new int[5];   //e is allotted a memory for integer pointer initially, then another array of 5
                            //integers is allotted namelessly, and the starting address of that array is assigned to e.
    int *f = NULL;   //f is allotted a memory for integer pointer, and is initialized with NULL (pointing to nowhere)
    int &g = *f;   //Sets the f's address to g's address.
    //c = b;   //This leads to error as c is a normal variable, and is not allowed to hold the address (b is a pointer, and holds the address).
    c = *b;   //The value in at location whose address is in b (i.e., the value in a), is stored in c. This is pretty much like a = a, as a and c both aliases.
   
    cout << "a: " << a << endl;   //The value in a, i.e., 7 is printed to screen.
    cout << "b: " << b << endl; //The value in b, i.e., the address of a which is also the content of b is printed to screen.
    cout << "*b: " << *b << endl; //The value at address which is stored in b i.e., the value in a (7) is printed to screen.
    cout << "c: " << c << endl;   //This value in a/c, i.e., 7 is printed to screen. This is exactly same as the first cout instruction.
    cout << "d: " << d << endl; //The starting address of the array d will be printed to screen.
    cout << "e: " << e << endl; //The starting address of the unnamed array which is stored in e will be printed to screen.
    cout << "*e: " << *e << endl; //The unnamed array is not initialized, and the starting value of that array, (0 as it is not initialized) will be printed.
    cout << "f: " << f << endl; //The content of f, i.e., NULL (0x0) will be printed to screen.
    //cout << "*f: " << *f << endl; //This leads to error/segmentation fault, as f is not pointing anywhere, and therefore, value at that nowhere address seems absurd.
    //cout << "g: " << g << endl; //This is pretty same to the previous statement, as g, and f are aliases. And will lead to error/segmentation fault.
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote