Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the code snippet which is showing stack overflow error.

// CPPProg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
class Employee
{
	public:
		char *empName;
		int empRolNo;	
public:
	Employee()
	{	
		delete []empName;
		cout<<"Default Constructor"<<endl;
		empName = new char[3];
		strcpy_s(empName,strlen("aa") + 1,"aa");
		empRolNo = 10;
		Employee e5;
	}
	Employee(char *name,int r)
	{
		cout<<"Param Constructor\t"<<this<<endl;
		this->~Employee();
		empName = new char[strlen(name) + 1];
		strcpy_s(empName,strlen(name) + 1,name);
		empRolNo=r;
		Employee e1;
	}
	virtual ~Employee()
	{
		cout<<"Desstructor\t"<<this<<endl;
		delete []empName;
		empName=NULL;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	Employee e1,e2("Satya",123);
	getchar();
	return 0;
}
Posted
Comments
[no name] 13-Apr-15 1:31am    
Did you get any warnings from the compiler?
Satya Chamakuri 13-Apr-15 1:51am    
No

1 solution

you're declaring a new Employee instance inside the Employee constructors, which will then declare a new Employee in the ctor, which will then ...

This is trivial to find using a debugger - why aren't you using one?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Apr-15 1:08am    
5ed. Someone already asked exactly the same question long time ago. This is boring: people are not thinking in the same way again and again.

OP is advised to learn what is call stack and how everything works through this stack. Without understanding this fundamental thing, there is no real programming.

—SA
Satya Chamakuri 13-Apr-15 1:51am    
Sir I understood What is call stack ,how mwmory allocation will be.
Here i know why its crashing and why its showing stack overflow. Whats the reason i posted is is there anywhere to handle this exception by de-allocating the memory which is allocated by object before calling the constructor again of same type.
barneyman 13-Apr-15 1:58am    
no - you can't de-allocate this memory because you've created a recursive, infinite loop - it needs re-engineering ... quite WHY you're instancing a new Employee inside the constructor baffles me
Satya Chamakuri 13-Apr-15 2:03am    
No sir, some where i studied we can call constructor within constructor.So to check out that i tried it. But i found out this abnormal behavior and i wanted to to know if any way there to avoid this. If no way is there to do such things means fine. Simply to confirm myself is there any way only i posted this. If any foolishness i am sorry
barneyman 13-Apr-15 2:10am    
what you've implemented is a basic recursive mistake - you CAN call a contructor from itself, in the same way you CAN call a function from itself - but if you do that you need to method of 'short-circuiting' out, or you'll run out of stack

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900