当前位置:网站首页>[C language] about scanf() and scanf_ Some problems of s()
[C language] about scanf() and scanf_ Some problems of s()
2022-06-28 12:17:00 【Jia Pu】
About scanf() And scanf_s() Some of the problems
About scanf() The main defect of is that it can not limit the number of input characters , It is extremely easy to cause the problem of memory overflow , Cause the program to crash .
Let's take a look at an example :
Environmental Science :Visual Studio 2019
#include "stdio.h"
#include "stdlib.h"
#pragma warning(disable : 4996)
int main()
{
char* str = (char*)malloc(sizeof(char) * 11);
scanf("%s", str);
printf("%s\n", str);
return 0;
}
There is no problem with normal input , When we type more than 10 The following conditions will occur when the number of characters is :
Here you can see that the return value is no longer 0 了 , Actually in str The inside of the address has exploded , It will directly lead to program errors .
About the solution , Microsoft provides scanf_s() Limit it , Its use method is :
scanf_s("%s", str, 11);
This 11 This limits the reading of characters , At the same time, it will be right str Check the boundaries , Set the last byte as 0. It should be noted that ANSI C There is no scanf_s(), Only scanf(), It only applies to Microsoft Visual Studio.
Use this function to modify as follows ( At the same time, it avoids Visual Studio Of C4996 error ):
#include "stdio.h"
#include "stdlib.h"
int main()
{
char* str = (char*)malloc(sizeof(char) * 11);
scanf_s("%s", str, 11);
return 0;
}
After execution, you can see that the return value is normal .
Be careful :
There was a small problem in the middle of the trip , Use scanf_s() After that, it is not normal to cross the boundary printf 了 , Before 2017 It's still normal , do not know why 2019 This is a problem , Wait till you're free .
边栏推荐
- Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
- Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
- Convert black mask picture to color annotation file
- Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
- 6. calculation index
- Vivo手机的权限管理
- Allez, Meta - Cosme, comme prévu, cette chaleur ne durera pas longtemps.
- MapReduce project case 1
- 如何获取泛型的类型
- KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
猜你喜欢
【C语言】NextDay问题
Custom title bar view
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
Leetcode 705. 设计哈希集合
Simple understanding of ThreadLocal
Day30 JS notes BOM and DOM 2021.09.24
Using soapUI to obtain freemaker's FTL file template
Day29 JS notes 2021.09.23
Three ways to implement LRU cache (recommended Collection)
2018 joint examination of nine provinces & Merging of line segment trees
随机推荐
SoapUI rookie tutorial
Zero basic C language (I)
NFT card chain game system development DAPP construction technical details
Difference (one dimension)
What method is required for word, PDF and txt files to realize full-text content retrieval?
JS foundation 8
AcWing 608. Poor (implemented in C language)
Is there a threshold for opening futures accounts? How to open futures accounts safely on the Internet
Using MySQL database in the express framework of node
多维度监控:智能监控的数据基础
赛尔号抽奖模拟求期望
Convert black mask picture to color annotation file
Still using simpledateformat for time formatting? Be careful that the project collapses!
MapReduce project case 3 - temperature statistics
JNI函数的2种书写方式
ByteV搭建动态数字孪生网络安全平台----助力网络安全发展
Random forest and poetry maker trained by AMR
不到一小时,苹果摧毁了15家初创公司
【JS】斐波那契数列实现(递归与循环)
Many benefits of SEO optimization are directly related to traffic