簡體   English   中英

C ++中的運算符new和delete

[英]Operators new and delete in c++

我需要有關運算符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;
    }

第一次讀取是可以的,但是第二次讀取要執行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.---

還有兩個按鈕Break和Continue。
如果按繼續,它將顯示以下內容:

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

我對指針不是很熟練(6個月)。 我將不勝感激。

由於您使用的是C ++,而不是C,因此您可能想使用string對象而不是char * 它們易於使用。

但是,這句話對我來說有點離譜:

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

如果您不將任何內容放入buffer ,則strlen(buffer)的結果將為零。 結果,您的new語句將嘗試為零個char元素分配空間。 也許這是在這里發生?

傳遞給第二個內存集的長度不正確,應該是:

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

您的錯誤發生在這里:

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);

另外,我使用了錯誤的maxlenght變量名。 它應該是maxlength。

第二行的數組大小錯誤:

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

並且缺少析構函數,從而導致內存泄漏。 strlen(buffer)調用非常昂貴,應該緩存。 std::string看起來比char*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM