Size definition of strcat() function

前端 未结 4 1138
既然无缘
既然无缘 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:57

    char buffer1[] = "computer";
    

    Creates a buffer big enough to hold 9 characters (strlen("Hello" + 1 byte for \0)). If you write anymore data to it what you end up with is Undefined behavior (UB). This is what happens when you do a strcat.
    UB means the program might crash or show literally any behavior. You are rather lucky that a program with UB crashes because it does not need to, but if it does atleast there is a indication of something wrong in it. Most of the times programs with UB will continue running correctly and crash when you least expect or want them to.

提交回复
热议问题