我想我似乎在为数组中的每个结构分配空间(查看数组中每个结构的第一个元素的地址)
我显然不明白C如何分配东西...... 使用valgrind我看到像
这样的东西==9852== Use of uninitialised value of size 8
==9852== at 0x400740: main (Test.c:24)
我很困惑。我已经查看了一些有关数组,结构和分配的帖子,但似乎无法看到正在发生的事情的微妙之处。
我还发现令人费解的是,除了免费之外,一切都有效,例如打印数组值会显示预期结果。在分配给未分配的(NULL)内存时,或者在分配的大小之外(可能),我可以理解一个seg错误但是不了解免费的内容
// Vector3.h
#include <stdio.h>
typedef struct {
double x,y,z;
} Vector3;
void Vector3Print(Vector3 v);
// Vector3.c
#include "Vector3.h"
void Vector3Print(Vector3 v) {
printf("%f, %f, %f\n", v.x, v.y, v.z);
}
// Mesh.h
#include "Vector3.h"
typedef struct {
Vector3 position;
Vector3 rotation;
Vector3* Vertices;
} Mesh;
void MeshAllocate( Mesh mesh, int size);
void MeshRelease( Mesh mesh);
// Mesh.c
#include <stdlib.h>
#include "Mesh.h"
void MeshAllocate( Mesh mesh, int size) { // size in verts
mesh.Vertices = malloc(size * sizeof(Vector3));
if (mesh.Vertices==NULL) {
printf("Error allocating memory!\n");
}
}
void MeshRelease( Mesh mesh) {
free(mesh.Vertices);
}
// test.c
// gcc -g -std=c99 *.c -o test
#include "Mesh.h"
int main () {
Mesh mesh;
printf("sizeof double %lu\n",sizeof(double));
printf("sizeof Vector3 %lu\n",sizeof(Vector3));
MeshAllocate(mesh,3);
printf("address v0.x %lu\n",(unsigned long)&mesh.Vertices[0].x);
printf("address v0.y %lu\n",(unsigned long)&mesh.Vertices[0].y);
printf("address v0.z %lu\n",(unsigned long)&mesh.Vertices[0].z);
printf("address v1.x %lu\n",(unsigned long)&mesh.Vertices[1].x);
mesh.Vertices[0] = (Vector3){0.1,2.3,4.5};
mesh.Vertices[1] = (Vector3){6.7,8.9,10.11};
mesh.Vertices[2] = (Vector3){12.13,14.15,16.17};
for (int i=0; i<3; i++ ) {
Vector3Print(mesh.Vertices[i]);
}
MeshRelease(mesh);
}
答案 0 :(得分:2)
你的分配和释放看起来很好,问题是你通过值而不是引用传入你的Mesh对象,这意味着你在MeshRelease和MeshAllocate的范围内处理你的新副本啮合。当你进入MeshRelease时,你试图释放未分配的内存,因为该上下文中的“网格”对象从未分配过它的内存(它与MeshAllocate操作的Mesh不同)。
您可以通过将Mesh的地址传递给这两个函数来修复它。
test.c的
#include "Mesh.h"
int main () {
Mesh mesh;
printf("sizeof double %lu\n",sizeof(double));
printf("sizeof Vector3 %lu\n",sizeof(Vector3));
MeshAllocate(&mesh,3);
printf("address v0.x %lu\n",(unsigned long)&mesh.Vertices[0].x);
printf("address v0.y %lu\n",(unsigned long)&mesh.Vertices[0].y);
printf("address v0.z %lu\n",(unsigned long)&mesh.Vertices[0].z);
printf("address v1.x %lu\n",(unsigned long)&mesh.Vertices[1].x);
mesh.Vertices[0] = (Vector3){0.1,2.3,4.5};
mesh.Vertices[1] = (Vector3){6.7,8.9,10.11};
mesh.Vertices[2] = (Vector3){12.13,14.15,16.17};
for (int i=0; i<3; i++ ) {
Vector3Print(mesh.Vertices[i]);
}
MeshRelease(&mesh);
}
Mesh.c
#include <stdlib.h>
#include "Mesh.h"
void MeshAllocate( Mesh* mesh, int size) { // size in verts
mesh->Vertices = malloc(size * sizeof(Vector3));
if (mesh->Vertices==NULL) {
printf("Error allocating memory!\n");
}
}
void MeshRelease( Mesh* mesh) {
free(mesh->Vertices);
}
Mesh.h
#include "Vector3.h"
typedef struct {
Vector3 position;
Vector3 rotation;
Vector3* Vertices;
} Mesh;
void MeshAllocate( Mesh* mesh, int size);
void MeshRelease( Mesh* mesh);
答案 1 :(得分:-1)
void MeshAllocate( Mesh mesh, int size) { // size in verts
mesh.Vertices = malloc(size * sizeof(Vector3));
if (mesh.Vertices==NULL) {
printf("Error allocating memory!\n");
}
}
您的错误在此功能中。
您的代码似乎可以使用free()
,因为您很幸运。