Pointers and References – C++ programming interview questions

Q – What is output of below C++ Program?

int main()
{  
	int *ptr1 = new int(5); 
	int *ptr2 = new int(10);

	ptr2 = ptr1;

	cout<< "Val ="<<*ptr1;  
	cout<< "Val ="<<*ptr2; 	

 
   return 0;
}

Answer: Output:

5

5

Explanation: Program is allocating memory dynamically with value 5 initialized and assigning memory address to a pointer ptr1.

int *ptr1 = new int(5);

Same is happening for pointer ptr2 and memory is initialized with value 10.

At the statement ptr2 = ptr1;, When we assign address from ptr1 to ptr2, both  ptr2 and ptr1 will point to the same memory allocation that pointer is holding the memory address containing value 5.

As a result value 5 will be printed on console for both ptr1 and ptr2.

Sub Question: What is the problem with the above C++ program?

Answer:Actually, there is a memory leak in the program at statement ptr2 = ptr1;

When ptr2 is assigned by ptr1, the address in ptr2 will be overridden by address of ptr1. So, we missed the original address of memory that was allocated and initialized for value 10.

So,even we apply delete ptr1 or delete ptr2, only memory allocated for value 5 will be de-allocated.

More over, if we apply delete ptr1 and delete ptr2 in above program, it may crash as we are trying to delete the same memory twice. For example

int main()
{  
	int *ptr1 = new int(5); 
	int *ptr2 = new int(10);

	ptr2 = ptr1;

	cout<< "Val ="<<*ptr1;  
	cout<< "Val ="<<*ptr2; 	
	
	delete ptr1;
	delete ptr2;

 
   return 0;
}


Q – What is issue with the below C++ program?

class Test{

public:
	Test(){cout<<"Constructor"<<"\n";}
	~Test()
	{
		cout<<"Destructor"<<"\n";
	}
};

int main(){

	Test *ptr = new Test();
	free(ptr);

	return 0;
}

Answer: Issue is that in this program new and free is mixed. Allocating memory dynamically with “new” operator but deallocating it using free() function instead of delete operator.

Though, program is ok with new and free(), but, in C++, it is better not to use free() function to deallocate the memory, as free() does not call the destructor of the class but delete operator does call.

If we have done some resource de-allocation in the class desturctor resources leak will occur.

Below c++ program will be good

int main(){

	Test *ptr = new Test();
	delete ptr;
	return 0;
}


Q – Consider below CPP program

class X{

public:
	X(){}	
	~X(){}
};

int main()
{
	X *xPtr[5];
	xPtr[0] = new X();
	xPtr[1] = new X();
	xPtr[2] = new X();
	xPtr[3] = new X();
	xPtr[4] = new X();

	return 0;
}

Then what is difference between delete xPtr and delete [] xPtr ?

Answer:

First let’s know the difference between both, then we’ll analyse the above program.

Both xPtr and delete [] xPtr are used to de-allocate memory.

delete xPtr : It will call destructor only once and make the program hang or crash depending upon the compiler.

delete [] xPtr : it will call destructor 5 times that is for every object of the class and program will run fine.

The above program will hang or crash at run time whatever statement executes “delete xPtr” or delete [] xPtr as X *xPtr[5] is simply an array of 5 class X object pointers and is created on STACK MEMORY. “delete” cannot be applied to static array.

NOTE: delete statement can only be applied to dynamically created object not to statically.

Lets see the above same example by creating dynamic array of objects and output for both delete xPtr and delete xPtr[].

1 – Test “delete xPtr” with dynamic array

#include <iostream>
using namespace std;

class X{
public:
      X(){
            cout<<"Cotr..."<<endl;
      }
      ~X(){
            cout<<"Dstr..."<<endl;
      }   

};

int main()
{
      //Create a dynamic array of 5 objects
      X *xPtr = new X[5];

      delete xPtr;
      cout<<"done"<<endl;

      return 0;

}

Output:
Cotr…
Cotr…
Cotr…
Cotr…
Cotr…
Dstr…
// Program hangs /crash // done string is missing

2 – Test “delete []xPtr” with dynamic array – It should be working program.

#include <iostream>
using namespace std;

class X{
public:
      X(){
            cout<<"Cotr..."<<endl;
      }
      ~X(){
            cout<<"Dstr..."<<endl;
      }   

};

int main()
{
      //Create a dynamic array of 5 objects
      X *xPtr = new X[5];

      delete []xPtr;
      cout<<"done"<<endl;

      return 0;

}

Output:
Cotr…
Cotr…
Cotr…
Cotr…
Cotr…
Dstr…
Dstr…
Dstr…
Dstr…
Dstr…
done


Related Posts