在相同类型的构造函数中创建对象时,为什么会出现异常? [英] Why exception is coming when creating object in contructor of same type?

查看:117
本文介绍了在相同类型的构造函数中创建对象时,为什么会出现异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我附加了一段代码,它在删除析构函数中堆中分配的内存时给出异常。



Here i am attaching the piece of code which is giving exception while deleting the memory allocated in heap in destructor.

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

int _tmain(int argc, _TCHAR* argv[])
{
	Employee e1,e2("Satya",123);
	getchar();
	return 0;
}





这是例外情况下控制的代码。





This is the exception Code where control is going during exception.

/***
*int _CrtIsValidHeapPointer() - verify pointer is from 'local' heap
*
*Purpose:
*       Verify pointer is not only a valid pointer but also that it is from
*       the 'local' heap. Pointers from another copy of the C runtime (even in the
*       same process) will be caught.
*
*Entry:
*       const void * pUserData     - pointer of interest
*
*Return:
*       TRUE - if valid and from local heap
*       FALSE otherwise
*
*******************************************************************************/
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
        const void * pUserData
        )
{
        if (!pUserData)
            return FALSE;

        if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
            return FALSE;

        return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}









有谁能告诉我为什么它的显示异常?

提前谢谢。





Can anyone suggest me why its showing exception?
Thanks in advance.

推荐答案

你的默认ctor正在分配一个大小为3的字符(这不够大)那么你正在做一个strlen - strlen通过走指针直到它找到一个null来工作,所以它可以用任何数字返回



然后你将FAR大于3个字符的字符串复制到其中 - 此时你完全厌倦了你的堆



你在另一个ctor中做同样的复制 - 你需要目的地的大小(对于那个安全的副本),它没有strlen,因为它还没有任何东西
your default ctor is allocating a char of size 3 (which isn't big enough), then you're doing a strlen on that - strlen works by walking the pointer until it finds a null, so it could be coming back with ANY number

Then you copy a string that's FAR bigger than 3 chars into it - you've totally biffed your heap at that point

you're doing the same copy in the other ctor - you need the size of the destination (for that safe copy), it doesn't have a strlen because there's nothing in it yet


this->~Employee();



这行是什么?你永远不应该直接调用析构函数。这是非常高级的专家。




What is that line? You should never call a destructor directly. This is very advanced stuff for expert only.

strlen(empName)



上面的lione不正确。您正在尝试获取未初始化字符串的长度(请参阅解决方案1)。




The above lione is incorrect. You are trying to get the length of an uninitialized string (see Solution 1).

Employee e3; // Here exception is coming.



在其中一个构造函数的末尾创建另一个未使用的员工的目的是什么?




What is the purpose of creating another employee that is unused at the end of one of the constructor?

public:
	char *empName;
	int empRolNo;



为什么这些成员是公开的?他们应该是私人的。





顺便说一下,你应该使用 std :: string 而不是你自己的字符串处理。你的课程看起来与此类似:


Why are those members public? They should be private.


By the way, you should use std::string instead of your own string handling. Your class would look similar to this:

class Employee
{
private:
    string empName;
    int empRolNo;

public:
    Employee()
    {
        cout << "Default Constructor" << endl;
        empName = "Chamakuri";
        empRolNo = 111;
    }

    Employee(const char *name,int r)
    {
        cout << "Param Constructor\t" << name << endl;
        empName = name;
        empRolNo=r;
    }

    ~Employee()
    {
        cout << "Destructor\t" << empName << endl;
    }
};


Thank you barneyman.. I changed strcpy_s(empName,strlen(empName),"Chamakuri") to strcpy_s(empName,strlen("Chamakuri"),"Chamakuri") and strcpy_s(empName,strlen(empName),name) to strcpy_s(empName,strlen(name),name) in the above snippet. Its Working fine.
Thank you........


这篇关于在相同类型的构造函数中创建对象时,为什么会出现异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆