鱼C论坛

 找回密码
 立即注册
查看: 1068|回复: 2

[已解决]使用对象时内存泄漏

[复制链接]
发表于 2021-4-14 14:40:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 沐丶心 于 2021-4-14 14:43 编辑

头文件
  1. #ifndef SHOOK_
  2. #define SHOOK_
  3. #include <iostream>
  4. class string2
  5. {
  6.     private:
  7.         char * str;
  8.         int len;
  9.    
  10.     public:
  11.         //初始化部分
  12.         string2();
  13.         string2(const char* c);
  14.         string2(const string2 &c);
  15.         ~string2();
  16.         string2 & operator=(const string2 &c);
  17.         //功能
  18.         int has(const char ch);//返回ch在str中出现的次数
  19.         void stringup();
  20.         void stringlow();
  21.         //重载运算符
  22.         friend string2 operator+(const string2 &b,const string2 &c);//要返回一个对象。调用复制构造函数
  23.         friend  bool  operator==( const string2 &b,const string2 &c);
  24.         friend  std::ostream & operator<<(std::ostream & os,const string2 &c);
  25.         friend  std::istream & operator>>(std::istream & os, string2 &c);
  26. };
  27. #endif
复制代码

类定义
  1. #include <iostream>
  2. #include "string2.h"
  3. #include <cctype>
  4. string2::string2()
  5. {
  6.     str = new char[1];
  7.     str[0]='\0';
  8. }
  9. string2::string2(const char* c)
  10. {
  11.     len = strlen(c);
  12.     str = new char[len+1];
  13.     strcpy(str,c);

  14. }
  15. string2::string2(const string2 &c)
  16. {
  17.     len = strlen(c.str);
  18.     str = new char[len+1];
  19.     strcpy(str,c.str);
  20. }
  21. string2::~string2()
  22. {
  23.     std::cout<<"/"<<str<<"/"<<std::endl;         ---------------------------------标记被删除的对象
  24.     delete []str;

  25. }

  26. int string2::has(const char ch)
  27. {
  28.     int count = 0;
  29.     while(*str++ !='\0')
  30.         if(*str==ch)
  31.             count++;
  32.         

  33.     return count;
  34. }
  35. void string2::stringup()
  36. {
  37.     for(int i=0;i<len;i++)
  38.         if(islower(str[i]))
  39.             str[i]=str[i]-32;
  40. }
  41. void string2::stringlow()
  42. {
  43.     for(int i=0;i<len;i++)
  44.         if(isupper(str[i]))
  45.             str[i]=str[i]+32;
  46. }
  47. string2 & string2::operator=(const string2 &c)
  48. {
  49.     if(this == &c)
  50.         return *this;
  51.     delete []str;
  52.     len = strlen(c.str);
  53.     str = new char[len+1];
  54.     strcpy(str,c.str);
  55.     return *this;
  56. }
  57. string2 operator+(const string2 &b,const string2 &c)
  58. {
  59.          string2 s;
  60.          s.len = b.len +c.len;
  61.          s.str = new char[s.len + 1];//给出充足地址;
  62.          strcpy(s.str, b.str);
  63.          strcat(s.str, c.str);
  64.          return s;
  65. }
  66. bool operator==(const string2 &b,const string2 &c)
  67. {
  68.     return (std::strcmp(b.str,c.str)==0);
  69. }
  70. std::ostream  & operator<<(std::ostream &os,const string2 &c)
  71. {
  72.     os <<c.str<<std::endl;
  73.     return os;
  74. }
  75. std::istream & operator>>(std::istream & is, string2 & c)
  76. {
  77.         char temp[20];
  78.         is.get(temp, 20);
  79.         if (is)
  80.                 c = temp;
  81.         while (is && is.get() != '\n')
  82.                 continue;             //删除多余的字符
  83.         return is;
  84. }
复制代码

使用类
  1. #include <iostream>
  2. using namespace std;
  3. #include "string2.h"
  4. int main()
  5. {
  6.     string2 s1(" and I am a C++ student. ");
  7.     string2 s2= "Please enter your name: ";
  8.     string2 s3;
  9.     cout << s2;
  10.     cin >>s3;
  11.     s2 = "My name is "+s3;
  12.     cout<<s2<<std::endl;
  13.     s2  =s2 +s1;
  14.     s2.stringup();
  15.     cout<<"The string\n"<<s2<<"\ncontains "<<s2.has('A')<<" 'A' characters in it.\n";
  16.     s1= "red";
  17.     string2 rgb[3]={string2(s1),string2("green"),string2("blue")};
  18.     cout<<"Enter the name of a primary color for mixing light: ";
  19.     string2 ans;
  20.     bool success = false;
  21.     while(cin>>ans)
  22.     {
  23.         ans.stringlow();
  24.         for(int i =0;i<3;i++)
  25.         {
  26.             if(ans==rgb[i])
  27.             {
  28.                 cout<<"That's right!\n";
  29.                 success =true;
  30.                 break;
  31.             }
  32.         }
  33.         if(success)
  34.             break;
  35.         else
  36.         {
  37.             cout<<"Try again1\n";
  38.         }
  39.         cout<<"Bye\n";
  40.         return 0;
  41.     }




  42. }
复制代码

运行结果
  1. Please enter your name:
  2. xiaoming
  3. /xiaoming/
  4. /My name is xiaoming/
  5. /My name is /
  6. My name is xiaoming

  7. /My name is xiaoming and I am a C++ student. /
  8. The string
  9. MY NAME IS XIAOMING AND I AM A C++ STUDENT.

  10. contains 5 'A' characters in it.
  11. /red/
  12. Enter the name of a primary color for mixing light: black
  13. /black/
  14. Try again1
  15. Bye
  16. /black/
  17. /blue/
  18. /green/
  19. /red/
  20. /xiaoming/
  21. //  ----------------------貌似丢失了两个???
  22. pe12_2(19954,0x102c07d40) malloc: *** error for object 0x15270414d: pointer being freed was not allocated
  23. pe12_2(19954,0x102c07d40) malloc: *** set a breakpoint in malloc_error_break to debug
  24. zsh: abort      ./pe12_2
复制代码

是定义时语法问题吗。已经找了好几遍,还是不行啊
求解答
最佳答案
2021-4-14 17:38:59
  1. #ifndef SHOOK_
  2. #define SHOOK_
  3. #include <iostream>
  4. class string2
  5. {
  6. private:
  7.     char * str;
  8.     int len;

  9. public:
  10.     //初始化部分
  11.     string2();
  12.     string2(const char* c);
  13.     string2(const string2 &c);
  14.     ~string2();
  15.     string2 & operator=(const string2 &c);
  16.     //功能
  17.     int has(const char ch);//返回ch在str中出现的次数
  18.     void stringup();
  19.     void stringlow();
  20.     //重载运算符
  21.     friend string2 operator+(const string2 &b,const string2 &c);//要返回一个对象。调用复制构造函数
  22.     friend  bool  operator==( const string2 &b,const string2 &c);
  23.     friend  std::ostream & operator<<(std::ostream & os,const string2 &c);
  24.     friend  std::istream & operator>>(std::istream & os, string2 &c);
  25. };
  26. #endif


  27. #include <iostream>
  28. #include <cctype>
  29. #include <cstring>
  30. string2::string2()
  31. {



  32.     // *********************
  33.     len = 0;





  34.     str = new char[1];
  35.     str[0]='\0';
  36. }
  37. string2::string2(const char* c)
  38. {
  39.     len = strlen(c);
  40.     str = new char[len+1];
  41.     strcpy(str,c);

  42. }
  43. string2::string2(const string2 &c)
  44. {
  45.     len = strlen(c.str);
  46.     str = new char[len+1];
  47.     strcpy(str,c.str);
  48. }
  49. string2::~string2()
  50. {
  51.     std::cout<<"/"<<str<<"/"<<std::endl;    //         ---------------------------------标记被删除的对象
  52.     delete []str;

  53. }

  54. int string2::has(const char ch)
  55. {
  56.     int count = 0;
  57.     char *p = str;
  58.     while(*p != '\0')
  59.         if(*p++ == ch)
  60.             count++;
  61.     return count;
  62.     /*
  63.     int count = 0;
  64.     while(*str++ !='\0')
  65.         if(*str==ch)
  66.             count++;


  67.     return count;
  68.     */
  69. }
  70. void string2::stringup()
  71. {
  72.     for(int i=0;i<len;i++)
  73.         if(islower(str[i]))
  74.             str[i]=str[i]-32;
  75. }
  76. void string2::stringlow()
  77. {
  78.     for(int i=0;i<len;i++)
  79.         if(isupper(str[i]))
  80.             str[i]=str[i]+32;
  81. }
  82. string2 & string2::operator=(const string2 &c)
  83. {
  84.     if(this == &c)
  85.         return *this;
  86.     delete []str;
  87.     len = strlen(c.str);
  88.     str = new char[len+1];
  89.     strcpy(str,c.str);
  90.     return *this;
  91. }
  92. string2 operator+(const string2 &b,const string2 &c)
  93. {
  94.     string2 s;



  95.     // ************************
  96.     delete []s.str;




  97.     s.len = b.len +c.len;
  98.     s.str = new char[s.len + 1];//给出充足地址;
  99.     strcpy(s.str, b.str);
  100.     strcat(s.str, c.str);
  101.     return s;
  102. }
  103. bool operator==(const string2 &b,const string2 &c)
  104. {
  105.     return (std::strcmp(b.str,c.str)==0);
  106. }
  107. std::ostream  & operator<<(std::ostream &os,const string2 &c)
  108. {
  109.     os <<c.str<<std::endl;
  110.     return os;
  111. }
  112. std::istream & operator>>(std::istream & is, string2 & c)
  113. {
  114.     char temp[20];
  115.     is.get(temp, 20);
  116.     if (is)
  117.         c = temp;
  118.     while (is && is.get() != '\n')
  119.         continue;             //删除多余的字符
  120.     return is;
  121. }

  122. #include <iostream>
  123. using namespace std;
  124. int main()
  125. {
  126.     string2 s1(" and I am a C++ student. ");
  127.     string2 s2= "Please enter your name: ";
  128.     string2 s3;
  129.     cout << s2;
  130.     cin >>s3;
  131.     s2 = "My name is "+s3;
  132.     cout<<s2<<std::endl;
  133.     s2  =s2 +s1;
  134.     s2.stringup();
  135.     cout<<"The string\n"<<s2<<"\ncontains "<<s2.has('A')<<" 'A' characters in it.\n";
  136.     s1= "red";
  137.     string2 rgb[3]={string2(s1),string2("green"),string2("blue")};
  138.     cout<<"Enter the name of a primary color for mixing light: ";
  139.     string2 ans;
  140.     bool success = false;
  141.     while(cin>>ans)
  142.     {
  143.         ans.stringlow();
  144.         for(int i =0;i<3;i++)
  145.         {
  146.             if(ans==rgb[i])
  147.             {
  148.                 cout<<"That's right!\n";
  149.                 success =true;
  150.                 break;
  151.             }
  152.         }
  153.         if(success)
  154.             break;
  155.         else
  156.         {
  157.             cout<<"Try again1\n";
  158.         }
  159.         cout<<"Bye\n";
  160.         return 0;
  161.     }
  162. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-14 17:38:59 | 显示全部楼层    本楼为最佳答案   
  1. #ifndef SHOOK_
  2. #define SHOOK_
  3. #include <iostream>
  4. class string2
  5. {
  6. private:
  7.     char * str;
  8.     int len;

  9. public:
  10.     //初始化部分
  11.     string2();
  12.     string2(const char* c);
  13.     string2(const string2 &c);
  14.     ~string2();
  15.     string2 & operator=(const string2 &c);
  16.     //功能
  17.     int has(const char ch);//返回ch在str中出现的次数
  18.     void stringup();
  19.     void stringlow();
  20.     //重载运算符
  21.     friend string2 operator+(const string2 &b,const string2 &c);//要返回一个对象。调用复制构造函数
  22.     friend  bool  operator==( const string2 &b,const string2 &c);
  23.     friend  std::ostream & operator<<(std::ostream & os,const string2 &c);
  24.     friend  std::istream & operator>>(std::istream & os, string2 &c);
  25. };
  26. #endif


  27. #include <iostream>
  28. #include <cctype>
  29. #include <cstring>
  30. string2::string2()
  31. {



  32.     // *********************
  33.     len = 0;





  34.     str = new char[1];
  35.     str[0]='\0';
  36. }
  37. string2::string2(const char* c)
  38. {
  39.     len = strlen(c);
  40.     str = new char[len+1];
  41.     strcpy(str,c);

  42. }
  43. string2::string2(const string2 &c)
  44. {
  45.     len = strlen(c.str);
  46.     str = new char[len+1];
  47.     strcpy(str,c.str);
  48. }
  49. string2::~string2()
  50. {
  51.     std::cout<<"/"<<str<<"/"<<std::endl;    //         ---------------------------------标记被删除的对象
  52.     delete []str;

  53. }

  54. int string2::has(const char ch)
  55. {
  56.     int count = 0;
  57.     char *p = str;
  58.     while(*p != '\0')
  59.         if(*p++ == ch)
  60.             count++;
  61.     return count;
  62.     /*
  63.     int count = 0;
  64.     while(*str++ !='\0')
  65.         if(*str==ch)
  66.             count++;


  67.     return count;
  68.     */
  69. }
  70. void string2::stringup()
  71. {
  72.     for(int i=0;i<len;i++)
  73.         if(islower(str[i]))
  74.             str[i]=str[i]-32;
  75. }
  76. void string2::stringlow()
  77. {
  78.     for(int i=0;i<len;i++)
  79.         if(isupper(str[i]))
  80.             str[i]=str[i]+32;
  81. }
  82. string2 & string2::operator=(const string2 &c)
  83. {
  84.     if(this == &c)
  85.         return *this;
  86.     delete []str;
  87.     len = strlen(c.str);
  88.     str = new char[len+1];
  89.     strcpy(str,c.str);
  90.     return *this;
  91. }
  92. string2 operator+(const string2 &b,const string2 &c)
  93. {
  94.     string2 s;



  95.     // ************************
  96.     delete []s.str;




  97.     s.len = b.len +c.len;
  98.     s.str = new char[s.len + 1];//给出充足地址;
  99.     strcpy(s.str, b.str);
  100.     strcat(s.str, c.str);
  101.     return s;
  102. }
  103. bool operator==(const string2 &b,const string2 &c)
  104. {
  105.     return (std::strcmp(b.str,c.str)==0);
  106. }
  107. std::ostream  & operator<<(std::ostream &os,const string2 &c)
  108. {
  109.     os <<c.str<<std::endl;
  110.     return os;
  111. }
  112. std::istream & operator>>(std::istream & is, string2 & c)
  113. {
  114.     char temp[20];
  115.     is.get(temp, 20);
  116.     if (is)
  117.         c = temp;
  118.     while (is && is.get() != '\n')
  119.         continue;             //删除多余的字符
  120.     return is;
  121. }

  122. #include <iostream>
  123. using namespace std;
  124. int main()
  125. {
  126.     string2 s1(" and I am a C++ student. ");
  127.     string2 s2= "Please enter your name: ";
  128.     string2 s3;
  129.     cout << s2;
  130.     cin >>s3;
  131.     s2 = "My name is "+s3;
  132.     cout<<s2<<std::endl;
  133.     s2  =s2 +s1;
  134.     s2.stringup();
  135.     cout<<"The string\n"<<s2<<"\ncontains "<<s2.has('A')<<" 'A' characters in it.\n";
  136.     s1= "red";
  137.     string2 rgb[3]={string2(s1),string2("green"),string2("blue")};
  138.     cout<<"Enter the name of a primary color for mixing light: ";
  139.     string2 ans;
  140.     bool success = false;
  141.     while(cin>>ans)
  142.     {
  143.         ans.stringlow();
  144.         for(int i =0;i<3;i++)
  145.         {
  146.             if(ans==rgb[i])
  147.             {
  148.                 cout<<"That's right!\n";
  149.                 success =true;
  150.                 break;
  151.             }
  152.         }
  153.         if(success)
  154.             break;
  155.         else
  156.         {
  157.             cout<<"Try again1\n";
  158.         }
  159.         cout<<"Bye\n";
  160.         return 0;
  161.     }
  162. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 19:20:59 | 显示全部楼层
明白了str的两块内存冲突了。谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 07:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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