Size definition of strcat() function

前端 未结 4 1185
既然无缘
既然无缘 2021-01-28 02:25

The question is why should I define size of string (string[] should be string[some-number]) When the program is as following it gives me Abort tr

4条回答
  •  死守一世寂寞
    2021-01-28 02:53

    From the man page of strcat:

    DESCRIPTION The strcat() function appends the src string to the dest string, overwriting the termi‐ nating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

    When you declare your string, the compiler allocate the size of your initial string to be 9 (resp. 8) for the buffer1 (resp. string) (includin '\0').

    Thus, strcat will result in 9 - 1 + 8 (i.e. 16 bytes) but only 9 are available.

提交回复
热议问题