在Struct内部指向Struct的Free()指针 [英] Free() pointer to Struct, inside a Struct

查看:109
本文介绍了在Struct内部指向Struct的Free()指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到如何释放()子结构的方法.

I can't seem to find how to free() the sub struct.

结构:

typedef struct
{
    char ID[5];
    location *loc;
} observer;

typedef struct
{
    double lat;
    double lng;
} location;

我的Free()包装器是:

My Wrapper of Free() is:

void free_obs_list_node(void *data)
{
    free(*(observer **)data);
}

我可以释放观察者结构.我无法释放指向结构Struct的指针.这是我的问题:如何释放location *loc;

I can free the observer struct. I cannot free the pointer to the location Struct. This is my question: how do I free the location struct pointed to by location *loc;

我使用了free()的包装,因为这些是通用链接列表中的节点.

I used a wrapper of free() as these are node's in a generic linked list.

推荐答案

free函数将void*作为参数,因此不进行强制转换. 您只需要提供一个指向要释放的内存位置的指针:

The free function takes a void* as parameter, so that cast doesn't count. You just need to give a pointer to the memory location you want to free:

free(data->loc);

这篇关于在Struct内部指向Struct的Free()指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆