c++中的运算符new和delete
Operators new and delete in c++
我需要一些关于操作员new
和delete
的帮助
我试图创建一个名为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好。
相关文章:
- g++用户定义的动态链接库上的全局new和delete运算符
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- 为什么 std::vector 使用 std::分配器而不是运算符 new 和 delete?
- 重载 new 和 delete 会导致 valgrind 错误
- 基类指针:在哪里使用 new 和 delete
- C++:为什么可以在没有事先使用 new 的情况下在指向结构的指针上使用 delete?
- 重载运算符 new 和运算符 delete 带有三个参数
- 如果我为一个类编写new和delete运算符,我是否必须编写它们的所有重载
- new[] / delete[] 并在C++中抛出构造函数/析构函数
- QQuickView new-delete-type-mismatch
- 使用重载的 new 和 delete 运算符跟踪已用内存
- 创建 myNew/myDelete 以替换 new/new[]/delete/delete[]
- 为多线程环境包装 c++ new/delete 的安全/好方法
- C++ VS:多 DLL 解决方案中的'new'和'delete'覆盖
- 如何使用 new 和 delete 与 OpenGL 的缓冲区对象一起使用?
- c++ 运算符 new[]/delete [] 是否调用运算符 new/delete?
- 继承类上的运算符new和delete.操作员删除未执行
- 在不同的编译器版本中调用new[]和delete[]
- `delete []`on`int*````new'''给出了malloc错误
- C++内存分配失败(使用 new & delete)