gpt4 book ai didi

c++ - 为什么此代码会生成错误 : Heap has been corrupted

转载 作者:行者123 更新时间:2023-11-28 00:07:32 29 4
gpt4 key购买 nike

使用 visual studio 2013,我一直在尝试运行下面的代码,但是当变量 vertexPointer 达到数字 7172 时,我以某种方式收到“堆已损坏”异常。有时我会收到错误消息:“未加载 igdusc32.pdb”

请帮帮我!!

#define VERTEX_COUNT 128
#define TERRAIN_SIZE 800

int count = VERTEX_COUNT * VERTEX_COUNT;
int size3 = count * 3;
int size2 = count * 2;
float* vertices = (float*)malloc(size3);
float* normals = (float*)malloc(size3);
float* uvs = (float*)malloc(size2);

int vertexPointer = 0;

for (int i = 0; i<VERTEX_COUNT; i++){
for (int j = 0; j<VERTEX_COUNT; j++){

vertices[vertexPointer*3] = (float)j / ((float)VERTEX_COUNT - 1) * TERRAIN_SIZE;
vertices[(vertexPointer * 3) +1] = 0.0f;
vertices[(vertexPointer * 3) + 2] = (float)i / ((float)VERTEX_COUNT - 1) * TERRAIN_SIZE;

normals[vertexPointer * 3] = 0.0;
normals[(vertexPointer * 3) +1] = 1.0f;
normals[(vertexPointer * 3) + 2] = 0.0f;

uvs[vertexPointer * 2] = (float)j / ((float)VERTEX_COUNT - 1);
uvs[(vertexPointer * 2)+1] = (float)i / ((float)VERTEX_COUNT - 1);

vertexPointer++;
}
}

最佳答案

您正在分配例如vertices作为size3 bytes,但你需要分配size3 float 。所以改为:

float* vertices = (float*)malloc(size3 * sizeof(float));
float* normals = (float*)malloc(size3 * sizeof(float));
float* uvs = (float*)malloc(size2 * sizeof(float));

或者,这是 C++,使用 new相反:

auto vertices = new float[size3];
auto normals = new float[size3];
auto uvs = new float[size2];

(然后您的清理必须更改为 delete[] vertices 等)。

您还可以使用 std::vector<float>这更可取。

std::vector<float> vertices(size3);
std::vector<float> normals(size3);
std::vector<float> uvs(size2);

关于c++ - 为什么此代码会生成错误 : Heap has been corrupted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647719/

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