- 论坛徽章:
- 0
|
Linux态库中,要返回一个结构体类型,应该如何做呢?下面是我的接口函数,我用到了malloc函数,但这样做的话就无法free掉申请的空间,请大神给点提示,应该怎么做呢?
struct net_interface *getnet(char *devname)
{
int fd;
char *net_info;
struct ifreq ifr;
struct ethtool_value eth_val;
struct ethtool_cmd eth_cmd;
struct net_interface *net_card;
//net_card = (struct net_interface *)net_info;
net_card = malloc(sizeof(struct net_interface));
memset(&ifr, 0, sizeof(ifr));
memset(ð_val, 0, sizeof(eth_val));
memset(ð_cmd, 0, sizeof(eth_cmd));
strcpy(ifr.ifr_name, devname);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
perror("Cannot get control socket");
exit(1);
}
net_card->speed = get_net_speed(fd, &ifr, ð_cmd);
net_card->link_status = get_net_link(fd, &ifr, ð_val);
//free(net_card);
return &net_card;
}
|
|