我有一个指向结构数组的指针,如下所示:
class Terrian {
...
private:
Vector *terrian_vertices;
...
}
指针的数据是在“construct_vertices”函数中生成的
Terrian::Terrian(int width, int height) {
this->width = width;
this->height = height;
std::cout << "Width: " << width << " Height: " << height << "\n";
std::cout << "Vertices\n";
construct_vertices();
std::cout << "Element\n";
construct_elements();
std::cout << "Buffers\n";
construct_buffers();
}
void Terrian::construct_vertices() {
terrian_vertices = new Vector[width * height];
std::cout << "Generating data\n";
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int index = x + y * width;
Vector *pos = new Vector((GLfloat)x, 0.0f, (GLfloat)-y);
memcpy(pos, terrian_vertices, sizeof(Vector) * index);
std::cout << terrian_vertices[index].x;
Color *color = new Color(0, 255, 0);
memcpy(color, terrian_colors, sizeof(Color) * index);
}
}
}
这是程序的输出(我在主函数中所做的就是实例化对象)
Width: 32 Height: 32
Vertices
Generating data
5.2349e-039
Process returned -1073741819 (0xC0000005) execution time : 10.073 s
Press any key to continue.
当第一个指针被复制到数组时程序崩溃,'x' 的输出应该是 0。这令人费解。有谁知道是什么导致了这种情况发生?如果是这样,是否有更好的动态分配结构的方法 - 无需使用 memcpy?
最佳答案
Does anyone know what is causing this to happen?
memcpy
的使用是不正确的。任何引用文档都会告诉您这一点。
第一个参数是指向目标的指针,即index
。元素进入terrian_vertices
数组:terrian_vertices + index
.
第二个参数是指向源的指针,即pos
.
(如果你很好奇,目标出现在源之前的原因是因为它与赋值运算符平行:destination = source
)
第三个参数是要复制的数据量,在你的例子中就是sizeof(Vector)
。 : 这只是一个 Vector
它需要复制,而不是 index
.
误用 memcpy
就像代码一样容易导致未定义的行为,幸运的是表现为错误。
If so, is there a better way to allocate structs dynamically - without using memcpy?
是的。不要自己管理内存:使用 std::vector
和正常的复制语义。
class Terrian {
// ...
private:
std::vector<Vector> terrain_vertices;
// Hmm, this may need some touch up on naming,
// or it may get confusing with two "vector" thingies around
};
// ...
void Terrian::construct_vertices() {
terrain_vertices.reserve(width * height);
// reserve is actually optional,
// but I put it here to parallel the original code
// and because it may avoid unneeded allocations
std::cout << "Generating data\n";
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
terrain_vertices.emplace_back((GLfloat)x, 0.0f, (GLfloat)-y);
// or this if your compiler doesn't support C++11:
// terrain_vertices.push_back(Vector((GLfloat)x, 0.0f, (GLfloat)-y));
std::cout << terrian_vertices[index].x;
// same thing for colors
terrain_colors.emplace_back(0, 255, 0);
}
}
注意现在怎么没有 new
随处可见。这解决了原始代码的另一个问题:它泄漏了 Vector
的一个实例。和 Color
之一每次循环迭代。
关于c++ - 错误,动态分配对象到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11496889/