gpt4 book ai didi

c++ - 与 vector 关联的 NULL 指针

转载 作者:行者123 更新时间:2023-11-28 03:21:06 26 4
gpt4 key购买 nike

在我的光线追踪器中,我正在构建边界体积层次结构。我花了几天时间尝试修复此错误,但我似乎做错了根本性的事情。

第一个方法是被调用的构造是下面使用 typedef std::vector 对象数组的方法。

void BVH::build(Objects* objs)
{
// construct the bounding volume hierarchy
int size = objs->size();

// Calculate the bounding box for this node
BBox bb = (*objs)[0]->boundingBox();
for ( int p = 1; p < size; p++ )
bb.expandToInclude( (*objs)[p]->boundingBox());
Vector3 pivot = (bb.max + bb.min) * 0.5f;
tree.bbox = bb;

int split = qsplit(objs, size, pivot.x, 0);

tree.left = subdivision(objs, split, 1);
tree.right = subdivision(&objs[split], size - split, 1);
}

下面的方法使用此方法来构造我的二叉树中的叶节点。

Node* BVH::makeLeaf(Objects* objs, int num)
{
Node* node = new Node;
if ( num == 1 ) { node->objs = &objs[0]; }
else if ( num == 2 ) { node->objs = ((&objs)[0],(&objs)[1]); }
node->isLeaf = true;
return node;
}

这是一个递归的方法,使用Object数组拆分构造节点和叶节点。

Node* BVH::subdivision(Objects* objs, int size, int axis)
{
if ( size == 1 ) { return makeLeaf(objs, 1); }
if ( size == 2 ) { return makeLeaf(objs, 2); }

Node* node = new Node;
node->isLeaf = false;

BBox bb = (*objs)[0]->boundingBox();
for ( int p = 1; p < size; p++ )
bb.expandToInclude((*objs)[p]->boundingBox());
node->bbox = bb;
Vector3 pivot = (bb.max + bb.min) * 0.5f;

int split = qsplit(objs, size, pivot[axis], axis);

node->left = subdivision(objs, split, (axis + 1) % 3);
node->right = subdivision(&objs[split], size - split, (axis + 1) % 3);

return node;
}

运行此代码会在 segmentation 内出现段错误。

这是来自 gdb 的空指针的帖子:

debug: Loading "teapot.obj"...
debug: Loaded "teapot.obj" with 576 triangles

Program received signal SIGSEGV, Segmentation fault.
0x000000000040bb9e in BVH::subdivision (this=0x61f0c0, objs=0x61f100, size=5, axis=1) at BVH.cpp:44
44 BBox bb = (*objs)[0]->boundingBox();
(gdb) bt
0 0x000000000040bb9e in BVH::subdivision (this=0x61f0c0, objs=0x61f100, size=5, axis=1) at BVH.cpp:44
1 0x000000000040bdef in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=9, axis=0) at BVH.cpp:56
2 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=18, axis=2) at BVH.cpp:53
3 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=36, axis=1) at BVH.cpp:53
4 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=72, axis=0) at BVH.cpp:53
5 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=144, axis=2) at BVH.cpp:53
6 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=288, axis=1) at BVH.cpp:53
7 0x000000000040b9ca in BVH::build (this=0x61f0c0, objs=0x61f0a0) at BVH.cpp:21
8 0x00000000004121ac in Scene::preCalc (this=0x61f0a0) at Scene.cpp:42
9 0x0000000000402fde in makeTeapotScene () at assignment1.cpp:113
10 0x000000000040f973 in main (argc=1, argv=0x7fffffffe798) at main.cpp:65
(gdb) p (*objs)[0]
$1 = (Object *&) @0x0: <error reading variable>

最佳答案

(&objs)[0]

相同
*(&objs)

相同
objs

同理,

(&objs)[1]

相同
objs + 1

这很可能不是有效对象的地址。

线

node->objs = ((&objs)[0],(&objs)[1]);

因为它使用逗号运算符,所以丢弃了 (&objs)[0] 的值并将 node->objs 设置为 objs + 1,无效。

不清楚您希望该作业完成什么,所以我不知道您应该用什么替换它。

关于c++ - 与 vector 关联的 NULL 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15396267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com