如何用新的测试内存分配? [英] How to test memory allocation with new ?

查看:87
本文介绍了如何用新的测试内存分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


如何处理以下事项:


------>

#include< iostream>

使用命名空间std;


int main()

{

int n = 0x7FFFFFFF;

char * pp = new char(n);

if(pp!= NULL){

pp [0] = 0;

pp [n-1] = 0;

cout<< "确定]; <<结束;

}否则{

cout<< " FAILED" <<结束;

}

}

------>

编译好,但是我运行它,我得到一个分段错误。我认为测试一个NULL值足够用于测试内存分配(或者

也许这是一个错误?我使用gcc(GCC)3.3.4)

问候,

jjleto

Hello,

How do I handle the following :

------>
#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char(n);
if ( pp != NULL ) {
pp[0] = 0;
pp[n-1] = 0;
cout << "OK" << endl;
} else {
cout << "FAILED" << endl;
}
}
------>
It compiles OK, but when I run it, I get a segmentation fault. I thought
that testing a NULL value was enough for testing memory allocation (or
perhaps it is a bug ? I use gcc (GCC) 3.3.4)

Regards,
jjleto

推荐答案



" ; jjleto" < JJ **** @ laposte.net>在消息中写道

"jjleto" <jj****@laposte.net> wrote in message
你好,

如何处理以下内容:

------>
#include < iostream>
使用命名空间std;

int main()
int n = 0x7FFFFFFF;
char * pp = new char(n );


这不符合您的想法。它为一个char分配内存,并且
尝试使用0x7FFFFFFF初始化它。分配数组使用 -

char * pp = new char [...];

if(pp!= NULL){


这是一个很大的误解,即new在失败时返回NULL。事实是

它在失败时抛出std :: bad_alloc异常(除非你指定

nothrow)

pp [0] = 0;
pp [n-1] = 0;


这是您的细分错误的原因。您正在尝试写入

非法内存位置。

cout<< "确定]; << endl;
} else {
cout<< " FAILED" << endl;
}
}
Hello,

How do I handle the following :

------>
#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char(n);
This does not do what you are thinking. It allocates memory for one char and
tries to initialize it with 0x7FFFFFFF. To allocate an array use -
char *pp = new char[...];
if ( pp != NULL ) {
This is a big misconception that new returns NULL on failure. The fact is
that it throws std::bad_alloc exception on failure (unless you specify
nothrow)
pp[0] = 0;
pp[n-1] = 0;
This is the cause of your segmentation fault. You are trying to write to an
illegal memory location.
cout << "OK" << endl;
} else {
cout << "FAILED" << endl;
}
}



Sharad


Sharad


> char * pp = new char [...];


谢谢,分段错误实际上是你给出的原因。

尽管如此,我在运行这个程序时仍然有错误(放弃

但不是段错误)


#include< iostream>

使用命名空间std;


int main()

{

int n = 0x7FFFFFFF;

char * pp = new char [n];

if(pp!= NULL){

pp [0] = 0;

pp [n-1] = 0;

cout<< "确定]; <<结束;

}否则{

cout<< " FAILED" <<结束;

}

}


如何用新的测试内存分配?
> char *pp = new char[...];

Thanks, the segmentation fault was actually for the reason you gave.

Nevertheless, I still have an error when running this program ("give up"
but not a segfault)

#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char[n];
if ( pp != NULL ) {
pp[0] = 0;
pp[n-1] = 0;
cout << "OK" << endl;
} else {
cout << "FAILED" << endl;
}
}

How do I test memory allocation with new ?




" jjleto" < JJ **** @ laposte.net>在消息中写道

"jjleto" <jj****@laposte.net> wrote in message
char * pp = new char [...];
char *pp = new char[...];



谢谢,分段错误实际上是因为你给的原因。
然而,我在运行这个程序时仍然有错误(放弃
但不是段错误)

怎么做我用新的测试内存分配?



Thanks, the segmentation fault was actually for the reason you gave.

Nevertheless, I still have an error when running this program ("give up"
but not a segfault)

How do I test memory allocation with new ?




异常。你需要在try-catch块中包装new(捕获

std :: bad_alloc)。


Sharad



I told you in my earlier reply too that new throws std::bad_alloc on
exception. You need to wrap the new in a try-catch block (catching
std::bad_alloc).

Sharad


这篇关于如何用新的测试内存分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆