沐丶心 发表于 2021-4-14 14:40:17

使用对象时内存泄漏

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

头文件
#ifndef SHOOK_
#define SHOOK_
#include <iostream>
class string2
{
    private:
      char * str;
      int len;
   
    public:
      //初始化部分
      string2();
      string2(const char* c);
      string2(const string2 &c);
      ~string2();
      string2 & operator=(const string2 &c);
      //功能
      int has(const char ch);//返回ch在str中出现的次数
      void stringup();
      void stringlow();
      //重载运算符
      friend string2 operator+(const string2 &b,const string2 &c);//要返回一个对象。调用复制构造函数
      friendbooloperator==( const string2 &b,const string2 &c);
      friendstd::ostream & operator<<(std::ostream & os,const string2 &c);
      friendstd::istream & operator>>(std::istream & os, string2 &c);
};
#endif
类定义
#include <iostream>
#include "string2.h"
#include <cctype>
string2::string2()
{
    str = new char;
    str='\0';
}
string2::string2(const char* c)
{
    len = strlen(c);
    str = new char;
    strcpy(str,c);

}
string2::string2(const string2 &c)
{
    len = strlen(c.str);
    str = new char;
    strcpy(str,c.str);
}
string2::~string2()
{
    std::cout<<"/"<<str<<"/"<<std::endl;         ---------------------------------标记被删除的对象
    delete []str;

}

int string2::has(const char ch)
{
    int count = 0;
    while(*str++ !='\0')
      if(*str==ch)
            count++;
      

    return count;
}
void string2::stringup()
{
    for(int i=0;i<len;i++)
      if(islower(str))
            str=str-32;
}
void string2::stringlow()
{
    for(int i=0;i<len;i++)
      if(isupper(str))
            str=str+32;
}
string2 & string2::operator=(const string2 &c)
{
    if(this == &c)
      return *this;
    delete []str;
    len = strlen(c.str);
    str = new char;
    strcpy(str,c.str);
    return *this;
}
string2 operator+(const string2 &b,const string2 &c)
{
       string2 s;
       s.len = b.len +c.len;
       s.str = new char;//给出充足地址;
       strcpy(s.str, b.str);
       strcat(s.str, c.str);
       return s;
}
bool operator==(const string2 &b,const string2 &c)
{
    return (std::strcmp(b.str,c.str)==0);
}
std::ostream& operator<<(std::ostream &os,const string2 &c)
{
    os <<c.str<<std::endl;
    return os;
}
std::istream & operator>>(std::istream & is, string2 & c)
{
        char temp;
        is.get(temp, 20);
        if (is)
                c = temp;
        while (is && is.get() != '\n')
                continue;             //删除多余的字符
        return is;
}

使用类
#include <iostream>
using namespace std;
#include "string2.h"
int main()
{
    string2 s1(" and I am a C++ student. ");
    string2 s2= "Please enter your name: ";
    string2 s3;
    cout << s2;
    cin >>s3;
    s2 = "My name is "+s3;
    cout<<s2<<std::endl;
    s2=s2 +s1;
    s2.stringup();
    cout<<"The string\n"<<s2<<"\ncontains "<<s2.has('A')<<" 'A' characters in it.\n";
    s1= "red";
    string2 rgb={string2(s1),string2("green"),string2("blue")};
    cout<<"Enter the name of a primary color for mixing light: ";
    string2 ans;
    bool success = false;
    while(cin>>ans)
    {
      ans.stringlow();
      for(int i =0;i<3;i++)
      {
            if(ans==rgb)
            {
                cout<<"That's right!\n";
                success =true;
                break;
            }
      }
      if(success)
            break;
      else
      {
            cout<<"Try again1\n";
      }
      cout<<"Bye\n";
      return 0;
    }




}
运行结果
Please enter your name:
xiaoming
/xiaoming/
/My name is xiaoming/
/My name is /
My name is xiaoming

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

contains 5 'A' characters in it.
/red/
Enter the name of a primary color for mixing light: black
/black/
Try again1
Bye
/black/
/blue/
/green/
/red/
/xiaoming/
//----------------------貌似丢失了两个???
pe12_2(19954,0x102c07d40) malloc: *** error for object 0x15270414d: pointer being freed was not allocated
pe12_2(19954,0x102c07d40) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      ./pe12_2

是定义时语法问题吗。已经找了好几遍,还是不行啊{:10_250:}
求解答

人造人 发表于 2021-4-14 17:38:59

#ifndef SHOOK_
#define SHOOK_
#include <iostream>
class string2
{
private:
    char * str;
    int len;

public:
    //初始化部分
    string2();
    string2(const char* c);
    string2(const string2 &c);
    ~string2();
    string2 & operator=(const string2 &c);
    //功能
    int has(const char ch);//返回ch在str中出现的次数
    void stringup();
    void stringlow();
    //重载运算符
    friend string2 operator+(const string2 &b,const string2 &c);//要返回一个对象。调用复制构造函数
    friendbooloperator==( const string2 &b,const string2 &c);
    friendstd::ostream & operator<<(std::ostream & os,const string2 &c);
    friendstd::istream & operator>>(std::istream & os, string2 &c);
};
#endif


#include <iostream>
#include <cctype>
#include <cstring>
string2::string2()
{



    // *********************
    len = 0;





    str = new char;
    str='\0';
}
string2::string2(const char* c)
{
    len = strlen(c);
    str = new char;
    strcpy(str,c);

}
string2::string2(const string2 &c)
{
    len = strlen(c.str);
    str = new char;
    strcpy(str,c.str);
}
string2::~string2()
{
    std::cout<<"/"<<str<<"/"<<std::endl;    //         ---------------------------------标记被删除的对象
    delete []str;

}

int string2::has(const char ch)
{
    int count = 0;
    char *p = str;
    while(*p != '\0')
      if(*p++ == ch)
            count++;
    return count;
    /*
    int count = 0;
    while(*str++ !='\0')
      if(*str==ch)
            count++;


    return count;
    */
}
void string2::stringup()
{
    for(int i=0;i<len;i++)
      if(islower(str))
            str=str-32;
}
void string2::stringlow()
{
    for(int i=0;i<len;i++)
      if(isupper(str))
            str=str+32;
}
string2 & string2::operator=(const string2 &c)
{
    if(this == &c)
      return *this;
    delete []str;
    len = strlen(c.str);
    str = new char;
    strcpy(str,c.str);
    return *this;
}
string2 operator+(const string2 &b,const string2 &c)
{
    string2 s;



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




    s.len = b.len +c.len;
    s.str = new char;//给出充足地址;
    strcpy(s.str, b.str);
    strcat(s.str, c.str);
    return s;
}
bool operator==(const string2 &b,const string2 &c)
{
    return (std::strcmp(b.str,c.str)==0);
}
std::ostream& operator<<(std::ostream &os,const string2 &c)
{
    os <<c.str<<std::endl;
    return os;
}
std::istream & operator>>(std::istream & is, string2 & c)
{
    char temp;
    is.get(temp, 20);
    if (is)
      c = temp;
    while (is && is.get() != '\n')
      continue;             //删除多余的字符
    return is;
}

#include <iostream>
using namespace std;
int main()
{
    string2 s1(" and I am a C++ student. ");
    string2 s2= "Please enter your name: ";
    string2 s3;
    cout << s2;
    cin >>s3;
    s2 = "My name is "+s3;
    cout<<s2<<std::endl;
    s2=s2 +s1;
    s2.stringup();
    cout<<"The string\n"<<s2<<"\ncontains "<<s2.has('A')<<" 'A' characters in it.\n";
    s1= "red";
    string2 rgb={string2(s1),string2("green"),string2("blue")};
    cout<<"Enter the name of a primary color for mixing light: ";
    string2 ans;
    bool success = false;
    while(cin>>ans)
    {
      ans.stringlow();
      for(int i =0;i<3;i++)
      {
            if(ans==rgb)
            {
                cout<<"That's right!\n";
                success =true;
                break;
            }
      }
      if(success)
            break;
      else
      {
            cout<<"Try again1\n";
      }
      cout<<"Bye\n";
      return 0;
    }
}

沐丶心 发表于 2021-4-14 19:20:59

明白了str的两块内存冲突了。谢谢大佬{:10_264:}
页: [1]
查看完整版本: 使用对象时内存泄漏