[Solved] malloc void return char array somtimes not working (terry davis was right about C++);


One of the problem to cause undefined behavior is

In void update_typeArray(char * char_array, int index, char input)
you free the memory pointed by the __array_buffer using char_array.

 update_typeArray(__array_buffer, index, __char_buffer);

 free(char_array);

and you allocate the new memory to char_array

char_array = (char*)malloc(sizeof(char)*(index + 1));//+1 cause string

but your __array_buffer will be still pointing to freed memory.

and you continue to use the __array_buffer in main after freeing

compare_typeArray(__array_buffer,"a",size,blist);

free(__array_buffer)

To understand easily consider below pictures.

 ----------------             -------
| __array_buffer |  -------> | memory| 
  ---------------             -------

after call update_typeArray(__array_buffer, index, __char_buffer);

     ----------------             -------
    | __array_buffer |  -------> | memory| 
      ---------------             -------
                                   ^
                                   |
                            ----------------
                           |    char_array |   
                             ---------------  

after free(char_array);

 ----------------             
| __array_buffer |  ------->   X 
  ---------------             
                               ^
                               |
                        ----------------
                       |   char_array   |   
                         ---------------  

After char_array = (char*)malloc(sizeof(char)*(index + 1));//+1 cause string

     ----------------             
    | __array_buffer |  ------->   X 
      ---------------             


  ----------------           -------
 |  char_array    | -----> | memory| 
   ---------------           -------

Solution:

If you want to __array_buffer to point to new memory you got use pointer to pointer as below.

void update_typeArray(char ** char_array, int index, char input)

and you call the update_type.

update_typeArray(&__array_buffer, index, __char_buffer);

your update_typeArray may look like below.

void update_typeArray(char **char_array, int index, char input)
{


    char * new_array = (char*)malloc(sizeof(char)*(index + 1));//+1 cause string


    for (int i = 0; i <= index; i++)
    {
        if (i < index)
        {
            new_array[i] = (*char_array)[i];
        }
        if (i == index)
        {
            new_array[i] = '\0';//'\000' cause string
        }
    }

    free(*char_array);
    new_array[index - 1] = input;
    *char_array = (char*)malloc(sizeof(char)*(index + 1));//+1 cause string

    for (int i = 0; i <= index; i++)
    {
        if (i < index)
        {
            (*char_array)[i] = new_array[i];
        }
        if (i == index)
        {
            (*char_array)[i] = '\0';//'\000' cause string
        }
    }


    free(new_array);

}

13

solved malloc void return char array somtimes not working (terry davis was right about C++);