运算符new和c ++中的删除

时间:2012-02-04 08:14:42

标签: c++ new-operator delete-operator

我需要操作员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,'\0',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,'\0',maxlenght);
        cin>>buffer;
        delete[] a;
        a=new char[strlen(buffer)];
        memset(a,'\0',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;
    }

第一次读取正常,但第二次读取时出现此错误{... 1}}:

a=new char[strlen(buffer)]

还有两个按钮Break and Continue 如果我按继续它会告诉我这个:

---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.---

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

4 个答案:

答案 0 :(得分:1)

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

但是那说,这条线看起来有点偏离我:

memset(buffer,'\0',maxlenght);

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

你的错在这里发生:

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

即。你刚刚调整了指针,可能是一个较小的尺寸,但是,你的maxlength变量仍然具有原始大小,并且可以在这里覆盖内存。补丁将是:

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

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

答案 3 :(得分:0)

第二行的数组大小错误:

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

错过了析构函数导致内存泄漏。 调用strlen(buffer)非常昂贵,应该缓存。 std::string看起来比char*更好。