What makes a C standard library function dangerous, and what is the alternative?

前端 未结 9 871
广开言路
广开言路 2020-12-04 06:05

While learning C I regularly come across resources which recommend that some functions (e.g. gets()) are never to be used, because they are either difficult or

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 06:29

    To add something about strncpy most people here forgot to mention. strncpy can result in performance problems as it clears the buffer to the length given.

    char buff[1000];
    strncpy(buff, "1", sizeof buff);
    

    will copy 1 char and overwrite 999 bytes with 0

    Another reason why I prefer strlcpy (I know strlcpy is a BSDism but it is so easy to implement that there's no excuse to not use it).

提交回复
热议问题