当前位置:网站首页>[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 .
边栏推荐
- Day24 JS notes 2021.09.15
- Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
- 【C语言】关于scanf()与scanf_s()的一些问题
- Url追加参数方法,考虑#、?、$的情况
- Multi dimensional monitoring: the data base of intelligent monitoring
- Difference (one dimension)
- FTP protocol for Wireshark packet capture analysis
- JS foundation 10
- 纯纯大怨种!那些年被劝退的考研专业
- Day36 JS notes ecma6 syntax 2021.10.09
猜你喜欢

Day32 JS note event (Part 1) September 27, 2021

Django -- MySQL database reflects the mapping data model to models

Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%

KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
![Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed](/img/9a/97813f5ac4d7c15711891cff25b9dd.jpg)
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed

If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience

Prefix and (2D)

Data analysis learning notes

水果FL Studio/Cubase/Studio one音乐宿主软件对比

【C语言】如何产生正态分布或高斯分布随机数
随机推荐
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
cdc同步 如果数据库表的主键发生了变化,会同步成两个数据 还是会同步更新主键呢?
Map排序工具类
Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%
Day36 JS notes ecma6 syntax 2021.10.09
Intranet penetration in the working group environment: some basic methods
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
NFT card chain game system development DAPP construction technical details
面试步骤的面试技巧
Self use demo of basic component integration of fluent
期货开户有门槛吗,如何网上安全的开通期货账户
ArrayList源码解析
6. calculation index
来吧元宇宙,果然这热度一时半会儿过不去了
ProCAST finite element casting process simulation software
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...
recent developments
Day33 JS note event (Part 2) September 28, 2021
Redis principle - List
Pre parsing, recursive functions and events in day25 JS 2021.09.16