add_to_array call results in NULL list

梦想与她 提交于 2020-02-08 10:04:16

问题


I have a C struct that basically contains two 2D char arrays called List. One for appended items and another for inserted items. Then use external functions that add C strings to these arrays called add_to_array.

The problem I'm having is when I call add_to_array once it goes through without issues but once called a second time, I get a segmentation fault. With test code, I discovered for what ever reason that I can't figure out, the 2D array(s) in List remain NULL after calling add_to_array. I checked the result of add_to_array and it returns 1 (success) every time.

the target system/OS is Ubuntu linux.

typedef struct
{
  char** appended;
  char** inserted;
  size_t app_alloc;
  size_t app_elem;
  size_t ins_alloc;
  size_t ins_elem;
}
List;

void init_list(List* list)
{
  list->app_alloc = 0;
  list->ins_alloc = 0;
  list->app_elem = 0;
  list->ins_elem = 0;
  list->appended = NULL;
  list->inserted = NULL;
}

void free_list(List* list)
{
  size_t i = 0;

  for (; i < list->ins_elem; ++i)
  {
    free(list->inserted[i]);
  }
  free(list->inserted);

  i = 0;

  for (; i < list->app_elem; ++i)
  {
    free(list->appended[i]);
  }
  free(list->appended);
}


int add_to_array(const char* in, char** array, size_t* alloc, size_t* elem)
{
  if (*alloc == *elem)
  {
    if (*alloc == 0) *alloc = list_buff;
    else             *alloc = (*alloc) * 2;

    char** _tmp = (char**) realloc(array, (*alloc) * sizeof(char*));

    if (!_tmp) return 0;
    else       array = _tmp;
  }

  array[(*elem)] = (char*) malloc(strlen(in) + 1);

  strcpy(array[(*elem)], in);

  (*elem)++;

  return 1;
}

int append_list(const char* in, List* out)
{
  return add_to_array(in, out->appended, &out->app_alloc, &out->app_elem);
}

int insert_list(const char* in, List* out)
{
  return add_to_array(in, out->inserted, &out->ins_alloc, &out->ins_elem);
}

int main()
{
  List test;

  init_list(&test);

  append_list("test", &test);

  if (!test.appended)
  {
    printf("*%s*", "why is test.appended still NULL?");
  }

  //append_list("wwww", &test);
  //insert_list("ffff", &test);

  //printf("%s\n", get_element(0, &test));
  //printf("%s\n", get_element(1, &test));
  //printf("%s\n", get_element(2, &test));

  //free_list(&test);


  return 0;
}

Output: why is test.appended still NULL?

thanks to David's advice, I got my code working here's the changes:

int add_to_array(const char* in, char*** array, size_t* alloc, size_t* elem)
{
  if (*alloc == *elem)
  {
    if (*alloc == 0) *alloc = list_buff;
    else             *alloc = (*alloc) * 2;

    char** _tmp = (char**) realloc((*array), (*alloc) * sizeof(char*));

    if (!_tmp) return 0;
    else       (*array) = _tmp;
  }

  (*array)[(*elem)] = (char*) malloc(strlen(in) + 1);

  strcpy((*array)[(*elem)], in);

  (*elem)++;

  return 1;
}

int append_list(const char* in, List* out)
{
  return add_to_array(in, &out->appended, &out->app_alloc, &out->app_elem);
}

int insert_list(const char* in, List* out)
{
  return add_to_array(in, &out->inserted, &out->ins_alloc, &out->ins_elem);
}

回答1:


Because C is a pass-by-value language :-)

You seem to be expecting that when you call:

add_to_array(in, out->appended, &out->app_alloc, &out->app_elem);

and then do

int add_to_array(const char* in, char** array, size_t* alloc, size_t* elem)
{
....
array = _tmp;
....

that the change to array will also change out->appended.

If you want it to work that way, you would have to pass a pointer to out->appended, and make add_to_array look like

int add_to_array(const char* in, char*** array, size_t* alloc, size_t* elem)


来源:https://stackoverflow.com/questions/10907650/add-to-array-call-results-in-null-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!