if one complains about gets(), why not do the same with scanf(“%s”,…)?

前端 未结 5 670
情深已故
情深已故 2021-01-06 03:57

From man gets:

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters ge

5条回答
  •  爱一瞬间的悲伤
    2021-01-06 04:13

    It may be simply that scanf will allocate space on the heap based on how much data is read in. Since it doesn't allocate the buffer and then read until the null character is read, it doesn't risk overwriting the buffer. Instead, it reads into its own buffer until the null character is found, and presumably copies that buffer into another of the correct size at the end of the read.

提交回复
热议问题