free 在分配的结构数组上导致段错误

free causing seg fault on allocated array of structs

我想我似乎在为数组中的每个结构分配 space(查看数组中每个结构的第一个元素的地址)

我显然不明白 C 是如何分配东西的... 使用 valgrind 我看到类似

的东西
==9852== Use of uninitialised value of size 8
==9852==    at 0x400740: main (Test.c:24)

我对此感到困惑。我已经查看了一些关于数组、结构和分配的 post,但似乎看不到正在发生的事情的微妙之处。

我还感到困惑的是,除免费外,一切正常,例如打印数组值显示预期结果。当分配给未分配的(NULL)内存或超出分配的大小(可能)时,我可以理解段错误,但不明白 free

发生了什么
// 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);
}
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(),因为您很幸运。

你的分配和释放对我来说看起来很好,问题是你是按值而不是按引用传递 Mesh 对象,这意味着在 MeshRelease 和 MeshAllocate 的范围内你正在处理新副本的网格。当您进入 MeshRelease 时,您正在尝试释放未分配的内存,因为该上下文中的 "mesh" 对象从未分配过内存(它与 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);