使用拷贝函数为什么在return 0处产生了溢出错误

C++语言 码拜 8年前 (2016-09-22) 1144次浏览
使用拷贝构造函数进行函数拷贝,前面断点调试通过,F11进入析构函数中,然后就出现溢出错误了。

// Day8_上机基础_03_02.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
using namespace std;
class CStudent
{
private:
	char * m_szName;                    //姓名
	int m_nAge;	                              //年龄
	int m_nID;	                              //学号
public:
	CStudent() :m_nAge(0), m_nID(0), m_szName(NULL){};
	CStudent(char * szName, int nAge, int nID) :m_nAge(nAge), m_nID(nID)
	{
		unsigned int nSize = strlen(szName);
		m_szName = new char[nSize+1];
		strcpy_s(m_szName,sizeof(m_szName)+1, szName);
	};
	CStudent(CStudent & o_Student)
	{
		/*
		*
		*
		*      深拷贝方法
		*
		*/
		//unsigned int nSize = strlen(o_Student.m_szName) ;
		//m_szName = new char[nSize + 1];
		//strcpy_s(m_szName, nSize + 1, o_Student.m_szName);
		/*
		*
		*
		*      浅拷贝方法
		*
		*/
		m_szName = o_Student.m_szName;
		m_nAge   = o_Student.m_nAge;
		m_nID    = o_Student.m_nID;
	};
	void result_velue()
	{ 

		cout <<"Name: "<< m_szName
			<<" Age: "<< m_nAge
			<<" Id: "<< m_nID << endl;
	};
	~CStudent()
	{
		delete[] m_szName;
	};
};
int _tmain(int argc, _TCHAR* argv[])
{
	CStudent objA("zcm",23,1);
	CStudent objB(objA);
	objA.result_velue();
	objB.result_velue();
	system("pause");
	return 0;
}
解决方案

5

浅拷贝要用引用:

你好,本人另外一个例子中采用了深度拷贝,也一样出现了溢出的问题,代码如下

// Day8_上机基础_03_02.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
using std::cout;
using std::endl;
class CStudent
{
private:
	char * m_szName;                    //姓名
	int m_nAge;	                        //年龄
	int m_nID;	                        //学号
	static int num;
public:
	static int CStudent::GetStudentNum();
	CStudent() :m_nAge(0), m_nID(0), m_szName(NULL){};
	CStudent(char * szName, int nAge, int nID) :m_nAge(nAge), m_nID(nID), m_szName(nullptr)
	{
		if (szName != nullptr){
			unsigned int nSize = strlen(szName) + 1;
			m_szName = new char[nSize];
			strcpy_s(m_szName, nSize, szName);
		}
		num++;
	};
	CStudent(CStudent & o_Student)
	{
		unsigned int nSize = strlen(o_Student.m_szName) + 1;
		m_szName = new char[nSize];
		strcpy_s(m_szName, nSize, o_Student.m_szName);
		m_nAge = o_Student.m_nAge;
		m_nID = o_Student.m_nID;
	};
	void result_velue()
	{
		cout << "Name: " << m_szName
			<< " Age: " << m_nAge
			<< " Id: " << m_nID << endl;
		cout <<"学生人数为:"<< num << endl;
	};
	~CStudent()
	{
		delete[] m_szName;
	};
};
int CStudent::num = 0;
int CStudent::GetStudentNum(){
	return num;
};
int _tmain(int argc, _TCHAR* argv[])
{
	CStudent objA("李四", 23, 1);
	CStudent objB("张三", 23, 1);
	CStudent objC1("王五", 23, 1);
	CStudent objC2("无名1", 23, 1);
	CStudent objC3("无名2", 23, 1);
	CStudent objC4("虫鸣3", 23, 1);
	CStudent objC5("重名4", 23, 1);
	CStudent objC6("789", 23, 1);
	CStudent objC7("789", 23, 1);
	CStudent objBbb(objA);
	objA.result_velue();
	int nStudentNum = CStudent::GetStudentNum();
	cout << "学生个数为:" << nStudentNum << endl;
	system("pause");
	return 0;
}

这个没问题

4

完整的办法是,
使用引用计数机制,结合写时拷贝技术,把 深拷贝,前拷贝结合起来
就可以了,
另一种方法是 使用,share_ptr 封装之中操作
这种方法,假如涉及到 对象修改,也还是需要引入相似写时拷贝技术 的

VC的CString
和标准库的 string 的实现,可以拿来作为参考

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明使用拷贝函数为什么在return 0处产生了溢出错误
喜欢 (0)
[1034331897@qq.com]
分享 (0)