鱼C论坛

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

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

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

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

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

x
本帖最后由 沐丶心 于 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);//要返回一个对象。调用复制构造函数
        friend  bool  operator==( const string2 &b,const string2 &c);
        friend  std::ostream & operator<<(std::ostream & os,const string2 &c);
        friend  std::istream & operator>>(std::istream & os, string2 &c);
};
#endif
类定义
#include <iostream>
#include "string2.h"
#include <cctype>
string2::string2()
{
    str = new char[1];
    str[0]='\0'; 
}
string2::string2(const char* c)
{
    len = strlen(c);
    str = new char[len+1];
    strcpy(str,c);

}
string2::string2(const string2 &c)
{
    len = strlen(c.str);
    str = new char[len+1];
    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[i]))
            str[i]=str[i]-32;
}
void string2::stringlow()
{
    for(int i=0;i<len;i++)
        if(isupper(str[i]))
            str[i]=str[i]+32;
}
string2 & string2::operator=(const string2 &c)
{
    if(this == &c)
        return *this;
    delete []str;
    len = strlen(c.str);
    str = new char[len+1];
    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[s.len + 1];//给出充足地址;
         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[20];
        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[3]={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[i])
            {
                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
是定义时语法问题吗。已经找了好几遍,还是不行啊
求解答
最佳答案
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);//要返回一个对象。调用复制构造函数
    friend  bool  operator==( const string2 &b,const string2 &c);
    friend  std::ostream & operator<<(std::ostream & os,const string2 &c);
    friend  std::istream & operator>>(std::istream & os, string2 &c);
};
#endif


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



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





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

}
string2::string2(const string2 &c)
{
    len = strlen(c.str);
    str = new char[len+1];
    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[i]))
            str[i]=str[i]-32;
}
void string2::stringlow()
{
    for(int i=0;i<len;i++)
        if(isupper(str[i]))
            str[i]=str[i]+32;
}
string2 & string2::operator=(const string2 &c)
{
    if(this == &c)
        return *this;
    delete []str;
    len = strlen(c.str);
    str = new char[len+1];
    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[s.len + 1];//给出充足地址;
    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[20];
    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[3]={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[i])
            {
                cout<<"That's right!\n";
                success =true;
                break;
            }
        }
        if(success)
            break;
        else
        {
            cout<<"Try again1\n";
        }
        cout<<"Bye\n";
        return 0;
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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);//要返回一个对象。调用复制构造函数
    friend  bool  operator==( const string2 &b,const string2 &c);
    friend  std::ostream & operator<<(std::ostream & os,const string2 &c);
    friend  std::istream & operator>>(std::istream & os, string2 &c);
};
#endif


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



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





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

}
string2::string2(const string2 &c)
{
    len = strlen(c.str);
    str = new char[len+1];
    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[i]))
            str[i]=str[i]-32;
}
void string2::stringlow()
{
    for(int i=0;i<len;i++)
        if(isupper(str[i]))
            str[i]=str[i]+32;
}
string2 & string2::operator=(const string2 &c)
{
    if(this == &c)
        return *this;
    delete []str;
    len = strlen(c.str);
    str = new char[len+1];
    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[s.len + 1];//给出充足地址;
    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[20];
    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[3]={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[i])
            {
                cout<<"That's right!\n";
                success =true;
                break;
            }
        }
        if(success)
            break;
        else
        {
            cout<<"Try again1\n";
        }
        cout<<"Bye\n";
        return 0;
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-14 19:20:59 | 显示全部楼层
明白了str的两块内存冲突了。谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 17:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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