The if_freenameindex() function frees the dynamic-memory allocated by if_nameindex().
#include <net/if.h>
void if_freenameindex(struct if_nameindex *ptr);
The ptr parameter is the pointer returned by a previous if_nameindex() call.