运营商新的C++

问题描述:

删除我需要一些帮助运营商newdelete运营商新的C++

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

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

也有两个按钮和continue。
如果我按下去它表明我:

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

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

+0

您需要在长度上添加1以容纳尾部'NUL'字符。或者使用'strdup()'或'std :: string'对象。 – *foe 2012-02-04 08:16:57

+0

*注意:当你想使用大量的数据库时,试试''NTL'库http://www.shoup.net/ntl/ * – Vyktor 2012-02-04 08:18:16

+1

你不应该在这段代码中使用指针。你应该使用'std :: vector'。我觉得我应该推荐你[一本好的入门C++书](http://*.com/q/388242/46642)。 – 2012-02-04 08:20:11

你的故障发生在这里:

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。上第二线

+0

是的,这是我的错,我没有改变maxlenght感谢您的回答 – Vali 2012-02-04 14:23:11

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

虽如此,但此行显得有点过我:

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

在您不要将任何东西放入buffer事件中,strlen(buffer)结果将是零。因此,您的new语句将尝试分配零个空间以用于零个元素。也许这是在这里发生的?

+0

在这种情况下,'vector'可能比'string'更合适,因为'char *'不是真正的文本。 – 2012-02-04 08:23:09

+0

好点。改用'vector '! – 2012-02-04 08:34:48

+0

我想为我的C++经验创建一个类,我不知道如何使用字符串,我会尝试使用它们,感谢提示 – Vali 2012-02-04 14:27:09

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

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

错阵列尺寸:

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

和析构函数被遗漏引起内存泄漏。 调用strlen(buffer)是非常昂贵的,应该被缓存。 std::string看起来更好然后char*