VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 794|回复: 6

[已解决]delete时候为什么会出错啊

[复制链接]
65_avatar_middle
最佳答案
0 
在线会员 发表于 2021-9-10 10:22:55 | 显示全部楼层 |阅读模式
为什么运行到delete的时候会出错啊  求助!!

#include <iostream>
using namespace std;
#include <string>
#include "Student.h"


int main()
{

        CStudent *stud=new CStudent("ddddddddddddddd",'m',50,40);
        delete stud;
        return 0;
}



#pragma once
#include <string>
#include <iostream>
using namespace std;
enum        EstudentType
{
        EstudentType_Error,
        EstudentType_Xiao,
        EstudentType_Zhong

};
class CStudent
{
public:
        char *p_name;
        char sex;
        int num;
        int age;       
};

#include "Student.h"
CStudent::CStudent(char* t_name,char t_sex,int t_num,int t_age)
{
        int n_len = strlen(t_name);
        p_name = new char(n_len + 1);
        memset(p_name, 0, n_len + 1);
        strcpy(p_name, t_name);

        cout << "oooo" << endl;
        sex = t_sex;
        num = t_num;
        age = t_age;
}
CStudent::~CStudent()
{

        if (p_name)
        {
                delete[] p_name;
        }
        cout << "niubi" << endl;

}



最佳答案
31_avatar_small
2021-9-10 16:09:27
本帖最后由 cpp2019 于 2021-9-10 16:10 编辑
不忘初心 发表于 2021-9-10 14:33
我声明了呀  在枚举下边就声明了,,而且改了一下按您刚刚说的还是到断点处会出错


怪我,我只是稍微瞟了一眼你的代码,只看到没有声明构造和析构,没仔细看!
你的构造函数中有错误,给你改过来了!
new的时候有[],delete时才能用delete []xx;
  1. #include <cstring>
  2. #include <iostream>
  3. #pragma warning(disable : 4996)
  4. using namespace std;

  5. enum EstudentType
  6. {
  7.         EstudentType_Error,
  8.         EstudentType_Xiao,
  9.         EstudentType_Zhong
  10. };

  11. class CStudent
  12. {
  13. public:
  14.         char *p_name = nullptr;
  15.         char sex;
  16.         int num;
  17.         int age;
  18. public:
  19.         CStudent(char* t_name, char t_sex, int t_num, int t_age);
  20.         ~CStudent();
  21. };

  22. CStudent::CStudent(char* t_name, char t_sex, int t_num, int t_age)
  23. {
  24.         int n_len = strlen(t_name);
  25.         p_name = new char[n_len + 1];
  26.         memset(p_name, 0, n_len + 1);
  27.         strcpy(p_name, t_name);

  28.         cout << "oooo" << endl;
  29.         sex = t_sex;
  30.         num = t_num;
  31.         age = t_age;
  32. }
  33. CStudent::~CStudent()
  34. {
  35.         if (p_name)
  36.         {
  37.                 delete[] p_name;
  38.                 p_name = nullptr;
  39.         }
  40.         cout << "niubi" << endl;

  41. }

  42. int main()
  43. {

  44.         CStudent *stud = new CStudent((char*)"ddddddddddddddd", 'm', 50, 40);
  45.         delete stud;
  46.         return 0;
  47. }
复制代码
微信图片_20210910102130.png
微信图片_20210910102124.png
微信图片_20210910102119.png
微信图片_20210910102112.png




上一篇:求指点,这个vs2013的帮助文档MSDN,如何打开使用?
下一篇:extern和函数类外实现求助
57_avatar_middle
最佳答案
25 
在线会员 发表于 2021-9-10 17:48:01 | 显示全部楼层
p_name = new char(n_len + 1);
这个写法动态分配了一个字节(1个char)的空间,并初始化为n_len + 1
这里你应该想 动态分配n_len + 1个字节(n_len + 1个char)的空间
正确写法 p_name = new char[n_len + 1];
65_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-9-10 11:37:54 来自手机 | 显示全部楼层
就是断点的这个地方,一运行到这就出错
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-9-10 13:18:33 | 显示全部楼层
本帖最后由 cpp2019 于 2021-9-10 13:29 编辑

构造、析构方法没有声明!
在public:下方增加以下代码就行了。

  1. public:
  2. CStudent(char* t_name,char t_sex,int t_num,int t_age);
  3. ~CStudent();
复制代码


完整代码
  1. #include <cstring>
  2. #include <iostream>
  3. using namespace std;
  4. enum        EstudentType
  5. {
  6.         EstudentType_Error,
  7.         EstudentType_Xiao,
  8.         EstudentType_Zhong

  9. };
  10. class CStudent
  11. {
  12. public:
  13.         char *p_name;
  14.         char sex;
  15.         int num;
  16.         int age;
  17. public:
  18.         CStudent(char* t_name,char t_sex,int t_num,int t_age);
  19.         ~CStudent();
  20. };

  21. CStudent::CStudent(char* t_name,char t_sex,int t_num,int t_age)
  22. {
  23.         int n_len = strlen(t_name);
  24.         p_name = new char(n_len + 1);
  25.         memset(p_name, 0, n_len + 1);
  26.         strcpy(p_name, t_name);

  27.         cout << "oooo" << endl;
  28.         sex = t_sex;
  29.         num = t_num;
  30.         age = t_age;
  31. }
  32. CStudent::~CStudent()
  33. {

  34.         if (p_name)
  35.         {
  36.                 delete[] p_name;
  37.         }
  38.         cout << "niubi" << endl;

  39. }

  40. int main()
  41. {

  42.         CStudent *stud=new CStudent((char*)"ddddddddddddddd",'m',50,40);
  43.         delete stud;
  44.         return 0;
  45. }
复制代码
65_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-9-10 14:33:28 | 显示全部楼层
cpp2019 发表于 2021-9-10 13:18
构造、析构方法没有声明!
在public:下方增加以下代码就行了。

我声明了呀  在枚举下边就声明了,,而且改了一下按您刚刚说的还是到断点处会出错
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-9-10 16:09:27 | 显示全部楼层    本楼为最佳答案   
bestAnswer
本帖最后由 cpp2019 于 2021-9-10 16:10 编辑
不忘初心 发表于 2021-9-10 14:33
我声明了呀  在枚举下边就声明了,,而且改了一下按您刚刚说的还是到断点处会出错


怪我,我只是稍微瞟了一眼你的代码,只看到没有声明构造和析构,没仔细看!
你的构造函数中有错误,给你改过来了!
new的时候有[],delete时才能用delete []xx;
  1. #include <cstring>
  2. #include <iostream>
  3. #pragma warning(disable : 4996)
  4. using namespace std;

  5. enum EstudentType
  6. {
  7.         EstudentType_Error,
  8.         EstudentType_Xiao,
  9.         EstudentType_Zhong
  10. };

  11. class CStudent
  12. {
  13. public:
  14.         char *p_name = nullptr;
  15.         char sex;
  16.         int num;
  17.         int age;
  18. public:
  19.         CStudent(char* t_name, char t_sex, int t_num, int t_age);
  20.         ~CStudent();
  21. };

  22. CStudent::CStudent(char* t_name, char t_sex, int t_num, int t_age)
  23. {
  24.         int n_len = strlen(t_name);
  25.         p_name = new char[n_len + 1];
  26.         memset(p_name, 0, n_len + 1);
  27.         strcpy(p_name, t_name);

  28.         cout << "oooo" << endl;
  29.         sex = t_sex;
  30.         num = t_num;
  31.         age = t_age;
  32. }
  33. CStudent::~CStudent()
  34. {
  35.         if (p_name)
  36.         {
  37.                 delete[] p_name;
  38.                 p_name = nullptr;
  39.         }
  40.         cout << "niubi" << endl;

  41. }

  42. int main()
  43. {

  44.         CStudent *stud = new CStudent((char*)"ddddddddddddddd", 'm', 50, 40);
  45.         delete stud;
  46.         return 0;
  47. }
复制代码
65_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-9-13 20:11:59 | 显示全部楼层
JLDawson 发表于 2021-9-10 17:48
p_name = new char(n_len + 1);
这个写法动态分配了一个字节(1个char)的空间,并初始化为n_len + 1
这 ...

对的   谢谢  是我对new的理解不到位
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-6-2 03:36

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表