Question

In: Computer Science

C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...

C++ Memory Allocation:

1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated.

a) Write code that allocates static memory (only include one declaration):

b) Write code that allocates stack memory (only include one declaration):

c) Write code that allocates heap memory (only include one declaration):

2)

  1. Edit the C++ program below to include a pointer and a reference to num. Print the number using both the pointer and the reference.

#include<iostream>

using namespace std;

int main()

{

    int num = 10;

    cout << "num = " << num << endl;

    return 0;

}

a) Write code that declares a pointer to num:

b) Write code that declares a reference to num:

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Please show the code and explain in comments! Show the output as well thank you

Solutions

Expert Solution

****This requires some effort so please drop a like if you are satisfied with the solution****

I have tried my best to make the explanation easy to understand through comments so please go through them for better understanding....

Q1. a,b,c

//static memory is allocated during compile time(static allocation)
//Heap memory is allocated during run time throught (dynamic allocation)

#include <iostream>
using namespace std;
//1. allocated on static memory
char * str1="Static memory";
//2. Staticlly allocated pointer
char * b;

int main(){
//3. this will be allocated on stack but will be destroyed when main() returns
char * str2="Allocated on stack";
//4. this is same as 2 but static tells that it should not be allocated on stack
static char * str3=str1;
//examples of static memory allocation
int a[10];
int x;
char c;
//examples of Heap memory allocation
int * array=(int*)malloc(10*sizeof(int));
//malloc make dynamic alloction which is on heap memory
}

Q2. a,b

#include <iostream>
using namespace std;
int main()
{
//declare an integer variable num=10
int num=10;
//declare a pointer variable p (a pointer variable is a variable which is capable of storing address or reference of other variable)
int *p;
//store the address or reference of num in p
p=&num;
//&x indicates the address of num which is stored in p i.e, p is the address where value of num is
//if you want to access value at num address which is p you need to specify *p here *p indicates the value of num
cout<<"num is "<<*p;
return 0;
}



Related Solutions

C programming Illustrate the stack and the heap allocation. Specify what each variable value holds and...
C programming Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { char str[20]; scanf("%[^\n]%*c", str); //illustrate how memory is allocated at this point printf("%s", str); return 0; }
C Programming Illustrate the stack and the heap allocation. Specify what each variable value holds and...
C Programming Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. char[] class = {'C','O','M','P','1','2','2'"}; #define int n = 4; long long fibb(long long a, long long b, int n) { return (--n>0)?(fibb(b, a+b, n)):(a); } int main() { fib(3); //illustrate what memory looks like at this point return 0; }
In CP/M why are the stack and the heap on opposite ends of memory? Why is...
In CP/M why are the stack and the heap on opposite ends of memory? Why is the OS at the top of upper memory?
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Write a program (main.cpp) to test the following: 1) Static allocation a. Create two integer variables...
Write a program (main.cpp) to test the following: 1) Static allocation a. Create two integer variables x and y, initialize them with different values. b. Use static memory allocation, declare px and py as address of x and y separately. c. Print out x, y, px, py, &x, &y, *px, *py.   d. Let py = px, and *py = 100 e. Print out x, y, px, py, &x, &y, *px, *py. g. Print out *px++, x, px 2) Dynamic allocation...
Illustrate the stack and the heap allocation. Specify what each variable value holds and where different...
Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. char[] class = {'C','O','M','P','1','2','2'"}; #define int n = 4; long long fibb(long long a, long long b, int n) { return (--n>0)?(fibb(b, a+b, n)):(a); } int main() { fib(3); //illustrate what memory looks like at this point return 0; }
Write your own version of a class template that will create a static stack of any...
Write your own version of a class template that will create a static stack of any data type. Demonstrate the class with a driver program. please make a version to copy.
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT