[英]Operators new and delete in c++
I need some help with operators new
and delete
我需要有关运算符
new
和delete
一些帮助
I tried to create a class named big to handle huge numbers 我试图创建一个名为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;
}
The first read is ok but the second has this error when it want to execute a=new char[strlen(buffer)]
: 第一次读取是可以的,但是第二次读取要执行
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.---
There are also two buttons Break and Continue. 还有两个按钮Break和Continue。
If I press continue it shows me this: 如果按继续,它将显示以下内容:
Unhandled exception at 0x77b8380e in Olimpiada a IX-a.exe: 0xC0000374: A heap has been corrupted.
I'm not very experienced with pointers (6 months). 我对指针不是很熟练(6个月)。 I would appreciate any answers.
我将不胜感激。
Since you're doing C++ and not C, you might instead want to use the string
object instead of char *
. 由于您使用的是C ++,而不是C,因此您可能想使用
string
对象而不是char *
。 They are a lot easier to use. 它们易于使用。
But that said, this line looked a bit off to me: 但是,这句话对我来说有点离谱:
memset(buffer,'\0',maxlenght);
In the event that you don't put anything into buffer
, the result of strlen(buffer)
will be zero. 如果您不将任何内容放入
buffer
,则strlen(buffer)
的结果将为零。 As a result, your new
statement will try to allocate space for zero char
elements. 结果,您的
new
语句将尝试为零个char
元素分配空间。 Perhaps that's happening here? 也许这是在这里发生?
传递给第二个内存集的长度不正确,应该是:
memset(a,'\0',strlen(buffer));
Your fault occurs here: 您的错误发生在这里:
delete[] a;
a=new char[strlen(buffer)];
memset(a,'\0',maxlenght);
ie you've just resized your a pointer to, potentially, a smaller size, but, your maxlength variable still has the original size and can overwrite memory here. 也就是说,您只是将指针的大小调整为可能较小的大小,但是,您的maxlength变量仍然具有原始大小,并且可以在此处覆盖内存。 The patch would be:
该补丁将是:
delete[] a;
maxlength = strlen(buffer);
a=new char[maxlenght];
memset(a,'\0',maxlenght);
Also, I've used your mispelt maxlenght variable name. 另外,我使用了错误的maxlenght变量名。 It should be maxlength.
它应该是maxlength。
Wrong array size on second line: 第二行的数组大小错误:
a=new char[strlen(buffer)];
memset(a,'\0',maxlenght);
And destructor is missed that causes memory leaks. 并且缺少析构函数,从而导致内存泄漏。 Calls of
strlen(buffer)
are very expensive and should be cached. strlen(buffer)
调用非常昂贵,应该缓存。 std::string
looks better then char*
. std::string
看起来比char*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.