EXERCISES FOR C++ 1. Use dynamic memory allocation to allocate memory for a doub
ID: 3912610 • Letter: E
Question
EXERCISES FOR C++
1. Use dynamic memory allocation to allocate memory for a double. Assign value 99.95 to the double. Output the value to the console. Now free the memory.
2. Use the Point class in previous module and dynamic memory allocation to allocate 2 Point objects (1.2,3.4) and (6.7, 8.9). Output the Points' x and y values. Assuming p1 is the pointer points to Point (1.2,3.4), p2 is the pointer points to (6.7, 8.9). Write code to swap the pointers so that p1 will now point to (6.7, 8.9) and p2 points to (1.2,3.4). Finally deallocate the memory.
3. Assume the following class declaration
class Memory {
public:
Memory ( );
~Memory ( );
} ;
Memory::Memory ( ) { cout << "Memory ctor "; }
Memory::~Memory ( ) { cout << "Memory dtor "; }
What's the output of the below program?
int main ( ) {
Memory * p = new Memory ( );
}
What about this program?
int main ( ) {
Memory * p = new Memory ( );
delete p ;
}
What about this program?
int main ( ) {
Memory mem;
Memory * p = new Memory ( );
}
What about this program?
int main ( ) {
Memory mem;
Memory * p = new Memory ( );
delete p ;
}
In the last program which variables/objects are taking stack memory which variables/objects taking dynamic memory? Your answer must be very explicit.
Explanation / Answer
Answer are given below. Please do rate the answer if it helped. Thank you
Answer 1:
========
#include <iostream>
#include <string>
using namespace std;
int main(){
double *dPtr = new double; //allocate memory
*dPtr = 99.95; //assign value
cout << "value of double is " << *dPtr << endl;
delete dPtr; //deallocate
}
Answer 2:
=======
You have not provided the Point class done previously... Assuming it is defined in a file named Point.h , I am giving the code below
I am assuming you have getX() and getY() in Point class which return x and y co-ordinates respectively.
#include <iostream>
#include <string>
#include "Point.h"
using namespace std;
int main(){
Point *p1 = new Point(1.2,3.4);
Point *p2 = new Point(6.7, 8.9);
cout << "p1 = (" << p1->getX() << ", " << p1->getY() << ")" << endl;
cout << "p2 = (" << p2->getX() << ", " << p2->getY() << ")" << endl;
//swap the pointers
Point *temp = p1;
p1 = p2;
p2 = temp;
cout << "p1 = (" << p1->getX() << ", " << p1->getY() << ")" << endl;
cout << "p2 = (" << p2->getX() << ", " << p2->getY() << ")" << endl;
//deallocate
delete p1;
delete p2;
}
Answer 3)
=======
What's the output of the below program?
int main ( ) {
Memory * p = new Memory ( );
}
output is given below: (only constructor called, destructor not called since delete was not used)
Memory ctor
---------------------------
What about this program?
int main ( ) {
Memory * p = new Memory ( );
delete p ;
}
output is given below: (both consructor and destructor are called)
Memory ctor
Memory dtor
---------------------------
What about this program?
int main ( ) {
Memory mem;
Memory * p = new Memory ( );
}
output is given below: (constructor called for both mem and p, but p is not deallocated and hence only destructor is called for mem)
Memory ctor
Memory ctor
Memory dtor
---------------------------
What about this program?
int main ( ) {
Memory mem;
Memory * p = new Memory ( );
delete p ;
}
output is given below: (constructor and destructor called for both the objects mem and p)
Memory ctor
Memory ctor
Memory dtor
Memory dtor
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.