c++中的运算符new和delete

Operators new and delete in c++

本文关键字:delete new 运算符 c++      更新时间:2023-10-16

我需要一些关于操作员newdelete 的帮助

我试图创建一个名为big的类来处理巨大的数字

#include <iostream>
using namespace std;
class big
{
protected:
    char *a;
    long lenght,maxlenght;
public:
    big()
    {
        maxlenght=256;
        a=new char[maxlenght];
        memset(a,'',maxlenght);
    }
    void read();
    void set_maxlenght(long x);
    long msize();
    long size();
    char* return_big();
    long at(long poz);
    char* operator[](long poz);
    void operator=(big x);
    void modify (long poz,long val);
    void dec (long poz);
    void inc (long poz);
    void operator-(big x);
    int compare(big x);
};


//functions
void write(big x)
{
    char *ptr;
    ptr=x.return_big();
    cout<<ptr;
}

//class big
    void big::read()
    {
        char *buffer;
        buffer=new char[maxlenght];
        memset(buffer,'',maxlenght);
        cin>>buffer;
        delete[] a;
        a=new char[strlen(buffer)];
        memset(a,'',maxlenght);
        for(long i=0;i<(long)strlen(buffer);i++)
            a[i]=buffer[i];
        lenght=strlen(buffer);
        delete[] buffer;
    }
    void big::set_maxlenght(long x)
    {
        maxlenght=x;
    }
    long big::msize()
    {
        return maxlenght;
    }
    long big::size()
    {
        return lenght;
    }
    char* big::return_big()
    {
        char *x;
        x=a;
        return x;
    }
    long big::at(long poz)
    {
        if(poz>=lenght)
            return -1;
        else
            return this->a[poz];
    }
    char* big::operator[](long poz)
    {
        if(poz>=lenght)
            return NULL;
        else
            return a+poz;
    }
    void big::operator=(big x)
    {
        a=new char[x.size()];
        for(long i=0;i<(long)strlen(x.a);i++)
            this->modify(i,x.at(i));
        this->lenght=strlen(x.a);
    }
    void big::modify (long poz,long val)
    {
        a[poz]=(char)val;
    }
    void big::dec (long poz)
    {
        if(a[poz])
            a[poz]--;
    }
    void big::inc (long poz)
    {
        if(a[poz]<255)
            a[poz]++;
    }
    void big::operator-(big z)
    {
        long n,m,i,j,aux;
        big rez,x,y;
        if(compare(z))
        {
            x=*this;
            y=z;
        }
        else
        {
            y=*this;
            x=z;
        }
        n=x.size();
        m=y.size();
        i=n;
        j=m;
        while(j>=0)
        {
            if(x.at(i)<0)
            {
                x.modify(i-1,x.at(i-1)+x.at(i));
                x.modify(i,0);
            }
            aux=x.at(i)-y.at(j);
            if(aux<0)
            {
                x.dec(i-1);
                rez.modify(i,aux+10);
            }
            else
                rez.modify(i,aux);
        }
        while(i)
        {
            i--;
            if(x.at(i)<0)
            {
                x.modify(i-1,x.at(i-1)+x.at(i));
                x.modify(i,0);
            }
            rez.modify(i,x.at(i));
        }
    }
    int big::compare(big x)
    {
        return strcmp(this->a,x.return_big());
    }
    int main()
    {
        big a,b;
        a.read();
        b.read();
        write(a);
        endl(cout);
        write(b);
        return 0;
    }

第一次读取是可以的,但第二次读取在想要执行a=new char[strlen(buffer)]:时出现此错误

---Windows has triggered an error in MyProject.exe
This may be due to the corruption of the heap, which indicates a bug in MyProject.exe or any DLLs it has loaded.
This may also be due to the user pressing F12 while MyProject.exe has focus.
The output window may have more diagnostic information.---

还有两个按钮"断开"answers"继续"
如果我按下continue,它会显示以下内容:

Unhandled exception at 0x77b8380e in Olimpiada a IX-a.exe: 0xC0000374: A heap has been corrupted.

我对指针不是很有经验(6个月)。如果有任何答案,我将不胜感激。

您的故障发生在这里:

delete[] a;
a=new char[strlen(buffer)];
memset(a,'',maxlenght);

也就是说,您刚刚将指针的大小调整为较小的大小,但是,您的maxlength变量仍然具有原始大小,并且可以覆盖此处的内存。补丁是:

delete[] a;
maxlength = strlen(buffer);
a=new char[maxlenght];
memset(a,'',maxlenght);

另外,我使用了你的错误的maxlenght变量名。它应该是maxlength。

由于您使用的是C++而不是C,因此您可能希望使用string对象而不是char *。它们更容易使用。

但也就是说,这句话在我看来有点离谱:

memset(buffer,'',maxlenght);

如果您不向buffer中输入任何内容,则strlen(buffer)的结果将为零。因此,new语句将尝试为零个char元素分配空间。也许这就是这里发生的事?

传递给第二个memset的长度不对,应该是:

memset(a,'',strlen(buffer));

第二行的数组大小错误:

a=new char[strlen(buffer)];
memset(a,'',maxlenght);

而析构函数丢失会导致内存泄漏。strlen(buffer)的调用非常昂贵,应该缓存。CCD_ 11看起来比CCD_ 12好。