当前位置:网站首页>[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 .
边栏推荐
- fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
- Database Series: is there any way to seamlessly upgrade the business tables of the database
- RemoteViews布局和类型限制源码分析
- SHA256加密工具类
- Usage and principle of precomputedtextcompat
- Graphics view framework for QT learning (to realize startup animation)
- Using MySQL database in the express framework of node
- Simulation of the Saier lottery to seek expectation
- Asynctask experience summary
- Is there a threshold for opening futures accounts? How to open futures accounts safely on the Internet
猜你喜欢

Day36 JS notes ecma6 syntax 2021.10.09

UGUI强制刷新Layout(布局)组件

The default point of this in JS and how to modify it to 2021.11.09

智联招聘基于 Nebula Graph 的推荐实践分享

Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly

Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration

深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...

Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques

JNI confusion of Android Application Security

Leetcode 705. 设计哈希集合
随机推荐
Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
JS foundation 8
【C语言】随机数文件对其进行三种排序方法
JS foundation 10
Fruit FL studio/cubase/studio one music host software comparison
【C语言】如何很好的实现复数类型
Day32 JS note event (Part 1) September 27, 2021
ArrayList源码解析
AsyncTask经验小结
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
MapReduce project case 3 - temperature statistics
Mutual conversion between mytipartfile and file
Redis principle - List
Vivo手机的权限管理
Timestamp and date conversion "suggested collection"
Leetcode 48. 旋转图像(可以,已解决)
Day31 JS notes DOM 2021.09.26
零基础C语言(一)
6. calculation index
Allez, Meta - Cosme, comme prévu, cette chaleur ne durera pas longtemps.