C ++ programming interview questions on pointers, references and memory with answers and explanations. This list contains conceptual and tricky coding / output questions on pointers in c ++. These questions are real programming / quiz asked in software industries to freshers and experienced professionals in technical interviews that we should practice.
Topics – Pointer, Reference, new and delete operator, dynamic memory allocation C++
Q – Find Crash in below C++ program with reason?
class Book
{
int a;
public:
void bookInfo()
{
cout<<"Book Information";
delete this;
}
};
int main()
{
Book *b = new Book();
b->bookInfo();
Book book;
book.bookInfo();
return 0;
}
Answer: Program will crash when “delete this” in the function bookInfo() is called using the statically allocated object i.e. book.bookInfo();. Note that “Book book” object has been created statically.
this refers to current class object. Means, if you delete this current class object will be destroyed.
If we call a function using pointer to an object(b->bookInfo();) which is allocated dynamically(Book *b = new Book();), then the object will get destroyed using delete this statement. But,if we call function using statically allocated object then run time error will occur.
Note that delete operator is always applied on dynamically allocated object in C++.
Q – What is output of below C++ Program?
int main()
{
int val = 20;
int *ptr = new int;
ptr =&val;
cout<<"val = "<<val;
delete ptr;
return 0;
}
Answer: Program will print output 20 and it will crash at run time when it will try to de-allocate memory using statement “delete ptr”.
Reason: local variable val is stored on the stack memory and its address is assigned to ptr. Delete operator actually delete memory allocated from heap dynamically. Local variable from stack automatically get deleted from stack when it goes out of scope.
Hence, program will throw run time error when we try to delete local variable using delete operator in C++.
Below C++ program, improvement over above program will work fine.
int main()
{
int *ptr = new int(20);//initialize it by value 20
cout<<"val = "<<*ptr;
delete ptr;
return 0;
}
Q – Is this C++ program OK? If no, what best improvement you can make?
class Computer{
int Id;
public:
Computer(int id){
this->Id =id;
}
void process(){
cout<<"Computer::process()";
}
};
class Employee{
Computer *c;
public:
Employee(){
c = new Computer(123);
}
~Employee(){
}
void foo(){
cout<<"Employee::foo()"; c->process();
}
};
int main(){
Employee ob;
ob.foo();
return 0;
}
Answer: This C++ program works fine. But, it has a memory leak. To stop the memory leak we have to deallocate the object of class Computer using delete operator as it has been allocated dynamically from heap.
Ideally, if we make any dynamic allocation in a class constructor, we should delete in class destructor. Hence, best place to delete the Computer object is the destructor of the class Employee as we have created in Employee constructor.
So, once Employee object goes out of scope in main(), destructor will be automatically called and object of Computer will be deleted preventing memory leak.
NOTE:
As an another option, to delete Computer object, we could have introduced a function in Employee class for example, drop() function in which Compute object can be deleted. And call drop function on employee object in main()function. It is also fine and memory leak will be stopped.
For example,
class Employee{
Computer *c;
public:
Employee(){
c = new Computer(123);
}
~Employee(){
}
void foo(){
cout<<"Employee::foo()"; c->process();
}
//Introduce a function to delete Computer object
void drop(){
delete c;
cout<<"Computer object deleted";
}
};
int main(){
Employee ob;
ob.foo();
//call drop() to delete computer object
ob.drop();
return 0;
}
But, it would not be a good idea to take extra overhead to call in main program by introducing a new function unnecessarily.
Q – What is output of below C++ program?
void func1(int& a){
a=3;
}
void func2(int a){
a=4;
}
int main(){
int x =2 ;
int &a = x;
func1(a);
cout << "value = " << a<< '\n';
func2(a);
cout << "value = " << a<< '\n';
return 0;
}
Answer: 3 3
Explanation: In above C++ program, int a is the reference(alias) of x, hence value of a will also be 2. When it get passed to func1() function, it is also receiving the same variable reference. If we modify the value of a =3 inside the function func1(), the value of a in main() will also be reflected in main.
In func2(), the value of a i.e. 3, modified by func1() earlier is getting passed to func2() and func2() is receiving it as a copy ( pass by value). Hence, if we modify variable a inside the function func2() that is 4, it will not reflect to main() method variable.
Hence, answer will be 3 3.
Note that in C++ pass by reference, value get reflected as they refer to same variable location.
And in pass by value, calling function variable value will be not changed.