Question

In: Computer Science

Consider the following program. There are 11 statements in the program starting with int x; Show...

Consider the following program. There are 11 statements in the program starting with int x; Show the memory allocated for the program in the stack and heap for each statement. Also, show any values assigned to each of these memory locations. In other words, you will show 11 stacks and 11 heaps as the answer to your question.

#include <iostream>

using namespace std;

int main() {

int x; // Stmt 1

int * y; // Stmt 2

y = new int; // Stmt 3

*y = 10; // Stmt 4

int * z; // Stmt 5

z = new int[5]; // Stmt 6

z[0] = 15; // Stmt 7

*(z + 1) = 20; // Stmt 8

delete y; // Stmt 9

y = z; // Stmt 10

delete [] z; // Stmt 11

return 0;

}

Solutions

Expert Solution

Note : New keyword is used to dynamically allocate memory in heap in C++, and delete keybord is used to free this memory from heap.

Lets see what happens for each statement :

int x; // Stmt 1

  • This will create a integer variable in stack, having size equivalent to integer. This integer

will contain some garbage value.

int * y; // Stmt 2

  • This will create a pointer variable in stack. This will contain NULL value as it has not assigned any value.

y = new int; // Stmt 3

  • This will create a integer variable in heap and y will point to this heap memory section.

*y = 10; // Stmt 4

  • This will assign value 10 to the integer space in the heap section, which was created in last section.

int * z; // Stmt 5

  • This will create a pointer variable in stack. This will contain NULL value as it has not assigned any value.

z = new int[5]; // Stmt 6

  • This will allocate memory in heap section equivalent to 5 integer in continuous form. z will point to first integer of this memory location.

z[0] = 15; // Stmt 7

  • This will write 15 in index 0 of memory assigned in last step.

*(z + 1) = 20; // Stmt 8

  • It will write 20 in index 1 of memory section, pointed by z.

delete y; // Stmt 9

  • This will deallocate heap memory pointed by y.

y = z; // Stmt 10

  • Now y will point to the same heap section, which is pointed by z pointer.Both will point to 1st integer of this heap section.

delete [] z; // Stmt 11

  • This will deallocate(Free) heap memory pointed by z.

Like, if this helped :)


Related Solutions

Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;...
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;       char B=10; cout<<”Enter Value of A”; cin<<A; show(A) show(50)          }       cin.get(); } int show(int x); { cout<<”Value is =” <<X cout<<endl; }
Consider the following program written in C syntax: void swap(int a, int b) { int temp;...
Consider the following program written in C syntax: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;} void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? Passed by value Passed by reference Passed...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
Convert the following C++ statements to an ARM assembly language program: const int size = 10;...
Convert the following C++ statements to an ARM assembly language program: const int size = 10; int x[size] = {8, 2, 9, 6, 7, 0, 1, 3, 5, 4}; int y[size] = {399, -87, 12, 0, 42, -367, 57, 92, -1000, 25}; for i = 0; i < size; i++) if (x([ i ] > y[ i ]) z[ i ] = 0 else z[ i ] = 1;
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int register i, j; for (j = 0; j< Size; j ++) { { for (i = 0; i< Size; i++) C[i; j] = A[i; j] + B[i; j]; } } Assume that the program is running on a system using demand paging and the page size is 1 Kilobyte. Each integer is 4 bytes long. It is clear that each array requires a 16-page...
#11.     Consider the (fictitious) data labelled “problem # 11” which gives starting salaries of fresh...
#11.     Consider the (fictitious) data labelled “problem # 11” which gives starting salaries of fresh graduates and their CGPA at the time of graduation. (i) Construct a simple linear regression model that predicts the starting salary of a fresh graduate based on his/her CGPA. (a) clearly write the model, and (b) quote the R2 from Excel output and interpret the number. (10 + 5 =15 points) (ii) Using your model in (i) estimate the starting salary of a fresh...
Consider the following statements: linkedStackTypemtack; linkedQueueType queue; int num; Suppose the input is 28 30 15...
Consider the following statements: linkedStackTypemtack; linkedQueueType queue; int num; Suppose the input is 28 30 15 11 10 -9 21 8 -3 33 17 14 Write a C++ code that processes these numbers as follows: If the number is an even number, it is pushed onto the stack. If the number is odd and divisible by 3, it is added into the queue; otherwise the top element, if any, of the stack is removed and the square of the number...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
A statistical program is recommended. Consider the following data for two variables, x and y. x...
A statistical program is recommended. Consider the following data for two variables, x and y. x 22 24 26 30 35 40 y 12 20 33 35 40 36 (a) Develop an estimated regression equation for the data of the form ŷ = b0 + b1x.  (Round b0 to one decimal place and b1 to three decimal places. ŷ = (b) Use the results from part (a) to test for a significant relationship between x and y. Use α = 0.05....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT