分配数组时运算符new的问题 [英] problems with operator new when allocating arrays

查看:179
本文介绍了分配数组时运算符new的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在某些程序代码中,像这样(它是一个构造函数):

  MyObject(MyMesh * m,MyTexture * t,float * c = NULL,float * sr = NULL,int sh = 100){
texture = t;
mesh = m;
subObjects = NULL;
texCoords = NULL;
if(texture!= NULL){
texCoords = new float [mesh-> numSurfacePoints * 2];

new会抛出一个std :: bad_alloc异常。它在另一个地方是一样的。
是可能的,我耗尽了内存?我不这么认为,所以如果你能帮助我,我会很高兴!
bye!

解决方案


/ p>

std :: bad_alloc 被抛出时,程序使用多少内存?



当它崩溃时, mesh-> numSurfacePoints 的值是多少?你是否确定传入的指针是 mesh 是一个有效的指针?如果您有一个非常碎片的地址空间,可能没有足够的连续空间来分配大型数组。您的程序在 std :: bad_alloc 之前运行多长时间?



如果还没有,应该考虑使用 boost :: scoped_array 或其他形式的数组智能指针,以便在不再需要堆分配的对象时自动删除。


I'm having prblems with my C++/openGL program.

at some point of code, like these(it's a constructor):

MyObject(MyMesh * m, MyTexture* t, float *c=NULL, float *sr=NULL, int sh=100){
texture=t;
mesh=m;
subObjects=NULL;
texCoords=NULL;
if (texture!=NULL){
		texCoords=new float[mesh->numSurfacePoints*2];

the new throws an std::bad_alloc exception. it's the same at another place. is it possible, that i ran out of memory? i dont think so, so if you could help me, i would be glad! bye!

解决方案

is it possible, that i ran out of memory?

How much memory is your program using when std::bad_alloc is thrown?

What is the value of mesh->numSurfacePoints when it crashes? Are you absolutely sure that the pointer passed in as mesh is a valid pointer? If you have a very fragmented address space, there might not be enough contiguous space to allocate a large array. How long does your program run before std::bad_alloc is thrown?

If you aren't already, you should consider using boost::scoped_array or some other form of smart pointer for arrays so that deletion occurs automatically when heap-allocated objects are no longer needed.

这篇关于分配数组时运算符new的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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