Hello I have a problem in my code. I want to convert vector of strings in C++ to array of strings in C but I got a error "double free or corruption (out):"
in my code I have two functions
C++ function: void get_num_apn( unsigned int *num_apn , char **arr)
: that convert the vector of strings to array of strings
C function : in my C function I have called get_num_apn
but I need to call this function one time
any one can help please and thanks.
c++ code:
void get_num_apn( unsigned int *num_apn , char **arr)
{
vector<std::string>apn={ "ghadz", "internet.ooredoo.tn", "mms.ooredoo.tn","internet.tn","gprs.tn",
"mms.tn",
"weborange",
"mms.otun" } ;
*num_apn=apn.size();
for (int i = 0; i < apn.size(); i++)
{
arr[i] = strdup(apn[i].c_str());
}
}
c code :
int main()
{
unsigned int num_apn=0;
char **arr = NULL;
arr = (char**)malloc(num_apn*sizeof(char*));
get_num_apn(&num_apn, arr);
for (int i=0 ; i<num_apn ; i++)
{
printf("arr[i]=%s\n" , arr[i]);
}
for (int i = 0; i <num_apn; i++)
{
free(arr[i]);
}
free(arr);
return 0;
}