当前位置:网站首页>[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 .
边栏推荐
- 6. calculation index
- 1. print hourglass
- Convert black mask picture to color annotation file
- AcWing 604. Area of circle (implemented in C language)
- Apache2 configuration denies access to the directory, but can access the settings of the files inside
- Redis hash hash type string (5)
- Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
- 【Unity编辑器扩展基础】、GUI
- Source code analysis of ArrayList
- Day24 JS notes 2021.09.15
猜你喜欢

What is data compliance? How to achieve data compliance?

Ali three sides: what is the difference between using on or where in the left join associated table and the condition

Using soapUI to obtain freemaker's FTL file template
Three ways to implement LRU cache (recommended Collection)

开源项目维权成功案例: spug 开源运维平台成功维权

自定义标题栏View

多维度监控:智能监控的数据基础

Deployment and optimization of vsftpd service

Leetcode 705. 设计哈希集合
![[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
随机推荐
已知两个点和中间一个比例的点,求该点坐标
Pre parsing, recursive functions and events in day25 JS 2021.09.16
cdc同步 如果数据库表的主键发生了变化,会同步成两个数据 还是会同步更新主键呢?
What method is required for word, PDF and txt files to realize full-text content retrieval?
5. Sum of N numbers
AcWing 607. Average 2 (implemented in C language)
Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
ByteV搭建动态数字孪生网络安全平台----助力网络安全发展
Which programming language will attract excellent talents?
Is there a threshold for opening futures accounts? How to open futures accounts safely on the Internet
2018 joint examination of nine provinces & Merging of line segment trees
【C语言】随机数文件对其进行三种排序方法
智联招聘基于 Nebula Graph 的推荐实践分享
RemoteViews布局和类型限制源码分析
Three ways to implement LRU cache (recommended Collection)
Prefix and (one dimension)
ArrayList源码解析
Redis 原理 - List
recent developments
期货开户有门槛吗,如何网上安全的开通期货账户