2015-11-05 95 views
1

我覺得我好像在一個陣列被分配爲每結構空間(查看每個結構的第一個元素的數組中的地址)自由造成賽格故障

我顯然使用不理解ç如何分配的東西... 的valgrind我看到的東西像

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

這我迷茫了。我查看了一些關於數組,結構和分配的文章,但似乎無法看到正在發生的事情的微妙之處。

我還發現令人費解的是,除了自由之外,一切都可以正常工作,例如打印數組值顯示預期結果。分配給未分配(NULL)內存或分配的(可能)的大小外,當我可以理解一個賽格故障,但不明白是怎麼回事了免費

// 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時,你試圖釋放未分配的內存,因爲在這個上下文中的「mesh」對象從來沒有分配過內存(它與MeshAllocate的操作不一樣)。

您可以通過將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
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(),因爲您很幸運。

+0

實際上並沒有回答這個問題 –

+0

@ChrisCamacho我想要幫助,但不是爲你做功課。 –

+0

30年來一直沒有在學校附近...... –