c - Have wrong(unwanted) values in pointer array elemnt -
i have pointer array named ipn_details
has type ip_details
:
typedef struct { char name[64]; char ip[16]; char flags; char mac[17] ; } ip_details;
after calling function get_ip_details
when print elements of ipn_details
contains wrong value element of mac
whenever array contains more 1 member. (all other elements have right values expected.) example, when array has 2 member value of mac
in first member concatenated of last characters of name
of second member.
first member: name=mina ip=9.9.9.9 flags=a mac=3f:fd:df:fdakh
second member: name=simakh ip=9.9.9.1 flags=ab mac=3f:gv:hj:fd
i have tried debug code using gdb
after memcpy
line , printed elemnt of mac
, contains right value,i have no idea why contains wrong value when print using cout
(i call cout
after calling function get_ip_details
). printed contents of ipn_details
array after memcpy line using gdb
. right, think problem not relate function extract_ip
.
int get_ip_details(ip_details **ipn_details, int *ip_details_len) { int_details *int_details_list=0; int int_details_list_len = 0; int ret = get_int_details(&int_details_list,&int_details_list_len,"eth"); if(ret == 0) { int k = 0; for(int i=0; < int_details_list_len; i++) { ip_details * ipn_details=0; int ipn_details_len=0; int int_index = int_details_list[i].index; extract_ip(int_index, &ipn_details,&ipn_details_len); // here read ipn_details_len number of members ipn_details array using function /* * wanna add members 1 one array ip_details(using loop j), * increase allocated * memory of array using realloc function , memcpy members * ipn_details function ip_details, 1 one */ if (ipn_details_len != 0) { *ip_details_len += ipn_details_len; *ip_details = (ip_details *)realloc(*ip_details, (*ip_details_len) * sizeof(ip_details)); for(int j=0; j < ipn_details_len; j++) { memcpy(&((*ip_details)[k]), & (ipn_details[j]),sizeof(ip_details)); k++; } } free(ipn_details); } free(int_details_list); } }
i call function this:
ip_details* ips_details=0; int ips_details_len; get_ip_details(&ips_details, &ips_details_len); for(int i=0; < ips_details_len; i++){ cout << string(ips_details[i].name) << "-"<< string(ips_details[i].ip_address) << "-" << string(ips_details[i].mac_address) << "-" << ips_details[i].flags << endl; }
i can't code compile because missing int_details
, get_int_details
, , extract_ip
. can please include definitions (or replace them hard-coded values instead of function calls) can compile , recreate/fix problem?
my suspicion aren't allocating enough space ip_details
pointer, end writing beyond allocated space, , second member mention overwriting wrote in first member because memory in first member resides not reserved.
a quick observation, have 17-char array reserved mac
, , try print string 14 characters long. i'm assuming don't null terminate string supposed to before reading cout ... string(...)
. string
function going read until finds null-terminator, or reads 17 chars, whichever comes first. that's why reads farther should, 3 undefined characters @ end of string when print it. reason why 3 akh take bit more logic/information, thing doesn't matter are, undefined territory , null terminator should end string before that.
Comments
Post a Comment