realloc() invalid old size

后端 未结 5 1037
慢半拍i
慢半拍i 2021-02-09 00:09

I am doing an exercise for fun from KandR C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it.

Her

5条回答
  •  野的像风
    2021-02-09 00:53

    if _getline() reads 10 or more characters, it will call realloc() on memory that was not allocated with malloc(). This is undefined behavior.

    Additionally, the memory allocated from realloc() will be leaked at the end of the call to _getline().

    Additionally, let's assume that the input string is "0123456789\n". Then you will attempt to write to longest that value, but you will never call realloc() before you do that, which is required.

提交回复
热议问题