出错代码:
#include <iostream>
int main()
{
int* p = new int();
int num = 1;
p = #
delete p; //未加载wntdll.pdb
p = nullptr;
return 0;
}
重点:出现这个肯定是你的代码的问题 VS自己少东西的可能性几乎为0
而问题基本都出在指针的使用上 一般都是你调用你的指针指向了错误的东西 / 你调用你的指针释放了奇怪的内容
以下为示范代码:
#include<iostream>
#include<ctime>
#include<fstream>
#include<string>
#include<stack>
#include<vector>
#include<map>
using namespace std;
class Name {
private:
char* first;
char* second;
char* third;
public:
Name(string f = "", string s = "", string t = "")
{
first = new char[f.length() + 1];
second = new char[s.length() + 1];
third = new char[t.length() + 1];
strcpy_s(first, f.length() + 1, f.c_str());
strcpy_s(second, s.length() + 1, s.c_str());
strcpy_s(third, t.length() + 1, t.c_str());
}
Name(Name& name)
{
}
~Name()
{
cout << "发病了" << endl;
delete first;
delete second;
delete third;
}
void Printname()
{
cout << first << " " << second << " " << third << endl;
}
};
class Person
{
private:
Name n;
string sex;
string national;
public:
Person(Name a, string sex, string national) :n(a), sex(sex), national(national)
{
cout << "构造完成!" << endl;
}
void printName()
{
n.Printname();
}
void printNational()
{
cout << national << endl;
}
};
int main()
{
Name name("比利王", "搞比利", "大xx");
Person s(name, "男", "日暮里");
s.printName();
s.printNational();
}
你拿去跑 如果你的电脑不报未加载wntdll (使用windows10 vs2019) 那我当场把这个电脑连键盘鼠标一起吃下去
那问题出在哪里呢?
在那个被我删掉的复制构造函数体里面 新建Person对象时使用了其构造函数,这时候我们是使用对象name来为Person中n赋值的,这使得无论是传递给构造函数参数还是用name初始化n都调用了复制构造函数,而char*的默认复制是值传递。也就是你只是复制了指针的内容 并没有重新开辟内存,于是 在形参name传递给n之后 name被回收 调用析构函数 此时你的三个指针指向的内容已经去世了!!!!更不用提main函数结束后,析构Person 再析构name 你的指针早就被除名了 你再强调去除这个指针指向的内容(鬼知道他这个地址已经被操作系统分配给谁了)就出现了上述的错误
修改也很简单
#include<iostream>
#include<ctime>
#include<fstream>
#include<string>
#include<stack>
#include<vector>
#include<map>
using namespace std;
class Name {
private:
char* first;
char* second;
char* third;
public:
Name(string f = "", string s = "", string t = "")
{
first = new char[f.length() + 1];
second = new char[s.length() + 1];
third = new char[t.length() + 1];
strcpy_s(first, f.length() + 1, f.c_str());
strcpy_s(second, s.length() + 1, s.c_str());
strcpy_s(third, t.length() + 1, t.c_str());
}
Name(Name& name)
{
cout << "调用复制构造函数" << endl;
int length1 = strlen(name.first);
int length2 = strlen(name.second);
int length3 = strlen(name.third);
first = new char[length1+1];
second = new char[length2+1];
third = new char[length3+1];
strcpy_s(first, length1+1,name.first);
strcpy_s(second, length2+1,name.second);
strcpy_s(third,length3+1,name.third);
}
~Name()
{
cout << "发病了" << endl;
delete first;
delete second;
delete third;
}
void Printname()
{
cout << first << " " << second << " " << third << endl;
}
};
class Person
{
private:
Name n;
string sex;
string national;
public:
Person(Name a, string sex, string national) :n(a), sex(sex), national(national)
{
cout << "构造完成!" << endl;
}
void printName()
{
n.Printname();
}
void printNational()
{
cout << national << endl;
}
};
int main()
{
Name name("比利王", "搞比利", "大xx");
Person s(name, "男", "日暮里");
s.printName();
s.printNational();
}
程序完美运行
这个故事告诉我们 能用string 就不要用char* 来装逼 因为很多人都不知道如果你装逼失败了,怎么指出你到底哪里装错了。