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

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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!