VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1056|回复: 6

[已解决]第38课代码求组

[复制链接]
01_avatar_middle
最佳答案
0 
在线会员 发表于 2021-8-7 16:04:31 | 显示全部楼层 |阅读模式
求助各位大佬,我按视频上敲的代码为什么会出现这种错误:

0x5716F6E0 (ucrtbased.dll)处(位于 test_8.7.exe 中)引发的异常: 0xC0000005: 读取位置 0x00000000 时发生访问冲突。

是我的vs2017有问题吗?

代码:
#include<iostream>
#include<string>
using namespace std;

class Student
{
public:
        char*pname;
        char sex;
        int age;
        int num;
        Student() {};
        Student(char *t_name, char t_sex, int t_age, int t_num);
        ~Student();
        Student& operator= (const Student& zhangsan);
};

Student::Student(char *t_name, char t_sex, int t_age, int t_num) :sex(t_sex), age(t_age), num(t_num)
{
        int n_len = 0;
        char *pname = NULL;
        n_len = strlen(t_name) + 1;
        pname = new char[n_len];
        memset(pname, 0, n_len);
        strcpy(pname, t_name);
}
Student::~Student()
{
        if (pname)
        {
                delete[]pname;
                *pname = NULL;
        }
        cout << "~Student+++++++++++++" << endl;
}
Student &Student::operator= (const Student& zhangsan)
{
        if (pname)
        {
                delete[]pname;
                pname = NULL;
        }

        int name_len = strlen(zhangsan.pname);
        pname = new char[name_len + 1] ;
        memset(pname, 0, name_len);

        strcpy(pname, zhangsan.pname);
        /*sex = zhangsan.sex;
        age = zhangsan.age;
        num = zhangsan.num;*/
        return *this;
}

int main()
{
        Student zhangsan((char*)"zhangsan", 'f', 1001, 20), lisi;
        lisi = zhangsan;
        return 0;
}



最佳答案
31_avatar_small
2021-8-7 18:52:30
代码给你改了,改了的地方加了少量注释,以后我就不给人改代码了,这类的帖子之前就改过,改了也没什么帮助,参考帖子:https://www.cctry.com/thread-301170-1-1.html
要自己学会调试,可以试着单步调试,运行到出错的地方,查一下为什么这么写会出错,解决不了再带疑问来论坛发贴,多看书,有的东西有的细节书里才有的,你的基础还需要大大的加油!

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;

  4. class Student
  5. {
  6. public:
  7.         char*pname;
  8.         char sex;
  9.         int age;
  10.         int num;
  11.         Student() {};
  12.         Student(char *t_name, char t_sex, int t_age, int t_num);
  13.         ~Student();
  14.         Student& operator= (const Student& zhangsan);
  15. };

  16. Student::Student(char *t_name, char t_sex, int t_age, int t_num) :sex(t_sex), age(t_age), num(t_num)
  17. {
  18.         // int n_len = 0;        //可略
  19.         // char *pname = NULL;        //这叫局部指针变量,会覆盖类中同名的pname,出了这个方法就失效了
  20.         int n_len = strlen(t_name) + 1;
  21.         pname = new char[n_len] { 0 };
  22.         // memset(pname, 0, n_len);        //不需要       
  23.         strcpy_s(pname, n_len, t_name);        //高版本VS需要要用带_s的
  24. }
  25. Student::~Student()
  26. {
  27.         if (pname)
  28.         {
  29.                 delete[]pname;
  30.                 *pname = NULL;
  31.         }
  32.         cout << "~Student+++++++++++++" << endl;
  33. }
  34. Student &Student::operator= (const Student& zhangsan)
  35. {
  36.         if (pname)
  37.         {
  38.                 delete[]pname;
  39.                 pname = NULL;
  40.         }

  41.         int name_len = strlen(zhangsan.pname) + 1;
  42.         pname = new char[name_len]{ 0 };
  43.         // memset(pname, 0, name_len);        //不需要
  44.         strcpy_s(pname, name_len, zhangsan.pname);        //高版本VS需要要用带_s的

  45.         /*sex = zhangsan.sex;
  46.         age = zhangsan.age;
  47.         num = zhangsan.num;*/
  48.         return *this;
  49. }

  50. int main()
  51. {
  52.         Student zhangsan((char*)"zhangsan", 'f', 1001, 20), lisi;
  53.         lisi = zhangsan;
  54.         return 0;
  55. }
复制代码




上一篇:opencv视频处理卡顿
下一篇:关于MFCEditBrowseCtrl控件的问题
72_avatar_middle
最佳答案
1 
在线会员 发表于 2021-8-7 17:46:01 | 显示全部楼层
问题1:
我在vs2019试验代码strcpy报不安全警告。
解决方案1:
可以把strcpy换成strcpy_s

在右键项目->属性->c/c++->sdl检查改为否

问题2:
在Student::Student(char *t_name, char t_sex, int t_age, int t_num) :sex(t_sex), age(t_age), num(t_num)函数中
char *pname = NULL;
在函数中有两个pname一个是类中的变量,一个是局部变量,而直接使用pname开辟空间和赋值的是局部变量,而不是类中的变量。
解决方案2:
可以使用this->pname来区分变量

注释上述代码

问题3:
在Student &Student::operator= (const Student& zhangsan)函数中
if (pname)
{
        delete[] pname;
        pname = NULL;
}
中pname编译器会认为是lisi的,而lisi在初始化时没有开辟空间 ,所以delete[] pname会无法读取
解决方案3:
注释掉上面代码就完事了

再建一个空的构造函数来开辟内存。

第38课代码求组
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-8-7 18:52:30 | 显示全部楼层    本楼为最佳答案   
bestAnswer
代码给你改了,改了的地方加了少量注释,以后我就不给人改代码了,这类的帖子之前就改过,改了也没什么帮助,参考帖子:https://www.cctry.com/thread-301170-1-1.html
要自己学会调试,可以试着单步调试,运行到出错的地方,查一下为什么这么写会出错,解决不了再带疑问来论坛发贴,多看书,有的东西有的细节书里才有的,你的基础还需要大大的加油!

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;

  4. class Student
  5. {
  6. public:
  7.         char*pname;
  8.         char sex;
  9.         int age;
  10.         int num;
  11.         Student() {};
  12.         Student(char *t_name, char t_sex, int t_age, int t_num);
  13.         ~Student();
  14.         Student& operator= (const Student& zhangsan);
  15. };

  16. Student::Student(char *t_name, char t_sex, int t_age, int t_num) :sex(t_sex), age(t_age), num(t_num)
  17. {
  18.         // int n_len = 0;        //可略
  19.         // char *pname = NULL;        //这叫局部指针变量,会覆盖类中同名的pname,出了这个方法就失效了
  20.         int n_len = strlen(t_name) + 1;
  21.         pname = new char[n_len] { 0 };
  22.         // memset(pname, 0, n_len);        //不需要       
  23.         strcpy_s(pname, n_len, t_name);        //高版本VS需要要用带_s的
  24. }
  25. Student::~Student()
  26. {
  27.         if (pname)
  28.         {
  29.                 delete[]pname;
  30.                 *pname = NULL;
  31.         }
  32.         cout << "~Student+++++++++++++" << endl;
  33. }
  34. Student &Student::operator= (const Student& zhangsan)
  35. {
  36.         if (pname)
  37.         {
  38.                 delete[]pname;
  39.                 pname = NULL;
  40.         }

  41.         int name_len = strlen(zhangsan.pname) + 1;
  42.         pname = new char[name_len]{ 0 };
  43.         // memset(pname, 0, name_len);        //不需要
  44.         strcpy_s(pname, name_len, zhangsan.pname);        //高版本VS需要要用带_s的

  45.         /*sex = zhangsan.sex;
  46.         age = zhangsan.age;
  47.         num = zhangsan.num;*/
  48.         return *this;
  49. }

  50. int main()
  51. {
  52.         Student zhangsan((char*)"zhangsan", 'f', 1001, 20), lisi;
  53.         lisi = zhangsan;
  54.         return 0;
  55. }
复制代码
01_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-8-7 22:07:34 | 显示全部楼层
cpp2019 发表于 2021-8-7 18:52
代码给你改了,改了的地方加了少量注释,以后我就不给人改代码了,这类的帖子之前就改过,改了也没什么帮助 ...


多谢多谢,我把38到42行注释后就跑出来了。
01_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-8-7 22:10:10 | 显示全部楼层
Ayden 发表于 2021-8-7 17:46
问题1:
我在vs2019试验代码strcpy报不安全警告。
解决方案1:

多谢多谢,我在好好琢磨一下。
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-8-7 23:17:52 | 显示全部楼层
本帖最后由 cpp2019 于 2021-8-7 23:22 编辑
refugeee 发表于 2021-8-7 22:07
多谢多谢,我把38到42行注释后就跑出来了。


38-42行的代码没问题的,但是没有实际意义,可有可无。
新建一个控制台项目,复制3楼的代码粘贴到项目的cpp文件中,整理一下部分注释前边的空白符就可以直接F5运行。

给你打包了项目: 第38课代码求组 ConsoleApplication2.7z (5.33 KB, 下载次数: 3)
01_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-8-9 14:28:48 | 显示全部楼层
cpp2019 发表于 2021-8-7 23:17
38-42行的代码没问题的,但是没有实际意义,可有可无。
新建一个控制台项目,复制3楼的代码粘贴到项目 ...

多谢大佬,我在琢磨琢磨
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

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

关闭

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

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

GMT+8, 2023-9-28 01:36

Powered by CcTry.CoM

© 2009-2021 cctry.com

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