Recursively freeing C structs

前端 未结 8 684
礼貌的吻别
礼貌的吻别 2020-12-31 23:23

I have a struct that only contains pointers to memory that I\'ve allocated. Is there a way to recursively free each element that is a pointer rather than calling free on eac

8条回答
  •  遥遥无期
    2020-12-31 23:53

    Not really - although you can write a method to do all six frees so that you never miss one.

    void freeModel( model* md1 ) {
        free (mdl->vertices);
        free (mdl->normals);
        free (mdl->uv_coords);
        free (mdl->quads);
        free (mdl->triangles);
        free (mdl);
    }
    

提交回复
热议问题