Dynamically grown array of strings

前端 未结 2 1708
余生分开走
余生分开走 2021-01-13 20:10

I\'m trying to build a dynamically grown array of strings. Neither the number of strings nor the length of each string is known at compile time. Here\'s the code I came up w

2条回答
  •  鱼传尺愫
    2021-01-13 20:50

    The following statement makes both result[0] and temp point to the same memory address:

    result[0]=temp;
    

    Having performed the above assignment, you then free(temp) and try to access result[0]:

    free(temp);
    printf ("%s \n", result[0]);
    

    This is undefined behaviour, since you're accessing the memory that's just been deallocated.

    The same goes for the identical code you have for result[1].

提交回复
热议问题