03-21-2012 12:34 PM
When playing around with the Network Status example I was trying to get a list of interfaces using the following snippet with the essence of where I am having the problem:
if (netstatus_get_interfaces(interface_list) == BPS_FAILURE)
fprintf(stderr, "Get interfaces error # %d \n", errno);
Which prints out error #22 – an invalid argument error
Would someone please provide some suggestions as to what I am going wrong?
Solved! Go to Solution.
03-21-2012 01:35 PM - edited 03-21-2012 02:48 PM
netstatus_get_interfaces() doesn't allocate memory for the netstatus_interface_list_t structure, only its contents.
Do one of the following:
1. Allocate memory for netstatus_interface_list_t on the heap using malloc() and use free() when done with it.
2. Declare interface_list on the stack and pass its address to netstatus_get_interfaces().
03-21-2012 02:52 PM - edited 03-21-2012 02:54 PM
Thanks, I had assumed it allocated memory. Since it doesn't how much memory should I allocate as it appears the size is dependent on the number of interfaces found and the length of the associated interface names?
An after thought:
If it doesn't allocate memory why is netstatus_free_interfaces(interface_list) required?
03-21-2012 02:54 PM - edited 03-21-2012 02:55 PM
Actually the size of the struct doesn't change, so use sizeof(netstatus_interface_list_t).
netstatus_free_interfaces() frees the array inside the struct.