linux释放内存函数,LINUX上的getifaddrs()函数的内存释放有关问题

LINUX上的getifaddrs()函数的内存释放有关问题

LINUX上的getifaddrs()函数的内存释放有关问题

日期:2014-05-16  浏览次数:20386 次

LINUX下的getifaddrs()函数的内存释放问题

LINUX下的getifaddrs()函数的内存释放问题

在LINUX下获取网卡信息需要用到IOCTL或者getifaddrs

而我在用getifaddrs的时候遇到了内存方面的问题

先看相关定义:

==========

函数定义:

/* Create a linked list of `struct ifaddrs' structures, one for each

network interface on the host machine.  If successful, store the

list in *IFAP and return 0.  On errors, return -1 and set `errno'.

The storage returned in *IFAP is allocated dynamically and can

only be properly freed by passing it to `freeifaddrs'.  */

extern int getifaddrs (struct ifaddrs **__ifap) __THROW;

/* Reclaim the storage allocated by a previous `getifaddrs' call.  */

extern void freeifaddrs (struct ifaddrs *__ifa)  __THROW;

==============

此函数需要的结构体定义:

struct ifaddrs

{

struct ifaddrs *ifa_next;     /* Pointer to the next structure.  */

char *ifa_name;               /* Name of this network interface.  */

unsigned int ifa_flags;       /* Flags as from SIOCGIFFLAGS ioctl.  */

struct sockaddr *ifa_addr;    /* Network address of this interface.  */

struct sockaddr *ifa_netmask; /* Netmask of this interface.  */

union

{

/* At most one of the following two is valid.  If the IFF_BROADCAST

bit is set in `ifa_flags', then `ifa_broadaddr' is valid.  If the

IFF_POINTOPOINT bit is set, then `ifa_dstaddr' is valid.

It is never the case that both these bits are set at once.  */

struct sockaddr *ifu_broadaddr; /* Broadcast address of this interface. */

struct sockaddr *ifu_dstaddr; /* Point-to-point destination address.  */

} ifa_ifu;

/* These very same macros are defined by for `struct ifaddr'.

So if they are defined already, the existing definitions will be fine.  */

# ifndef ifa_broadaddr

#  define ifa_broadaddr ifa_ifu.ifu_broadaddr

# endif

# ifndef ifa_dstaddr

#  define ifa_dstaddr   ifa_ifu.ifu_dstaddr

# endif

void *ifa_data;               /* Address-specific data (may be unused).  */

};

=============

我在调用了getifaddrs()之后,正常地完成了需要的工作

但是最后如果用freeifaddrs,则出现运行时错误

*** glibc detected *** d: free(): invalid pointer: 0x0804a4d4 ***

======= Backtrace: =========

/lib/libc.so.6[0xb7eda911]

/lib/libc.so.6(__libc_free+0x84)[0xb7edbf84]

/lib/libc.so.6(freeifaddrs+0x1d)[0xb7f512dd]

d[0x8048989]

d[0x80486a5]

/lib/libc.so.6(__libc_start_main+0xdc)[0xb7e8c87c]

d[0x8048491]

======= Memory map: ========

08048000-08049000 r-xp 00000000 03:07 48637 /home/souldump/bin/d

08049000-0804a000 rw-p 00000000 03:07 48637 /home/souldump/bin/d

0804a000-0806b000 rw-p 0804a000 00:00 0 [heap]

b7d00000-b7d21000 rw-p b7d00000 00:00 0

b7d21000-b7e00000 ---p b7d21000 00:00 0

b7e76000-b7e77000 rw-p b7e76000 00:00 0

b7e77000-b7f90000 r-xp 00000000 03:05 16184 /lib/libc-2.4.so

b7f90000-b7f92000 r--p 00118000 03:05 16184 /lib/libc-2.4.so

b7f92000-b7f94000 rw-p 0011a000 03:05 16184 /lib/libc-2.4.so

b7f94000-b7f98000 rw-p b7f94000 00:00 0

b7fab000-b7fb5000 r-xp 00000000 03:05 20108 /lib/libgcc_s.so.1

b7fb5000-b7fb6000 rw-p 00009000 03:05 20108 /lib/libgcc_s.so.1

b7fb6000-b7fb7000 rw-p b7fb6000 00:00 0

b7fb7000-b7fd1000 r-xp 00000000 03:05 16177 /

免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值