当前位置:网站首页>The performance of nonstandard sprintf code in different platforms
The performance of nonstandard sprintf code in different platforms
2022-06-23 05:42:00 【A little leisure】
sprintf The use of format code is not standardized, and its performance on different platforms (C Language )
author : Yu Han Gao
Time :2022.6.22 10:09
Blog :blog.csdn.net/cg_i
adults , Times have changed

Like many people, I study C Language is made up of 1978 year Dennis M. Ritchie and Brian W. Kernighan Co authored by 《The C Programming Language》 This book goes to C The world of language . First , The book describes C It's old-fashioned ( Sometimes called Kernighan and Ritchie[KERN 78], Or called K&R C) And has been ANSI C ( hereinafter referred to as standard ) It has fundamentally replaced . Although the vast majority of them are K&R C The program can be written in with very little modification ANSI C Environment is running , However, the standard does not stipulate that C The compiler checks for these differences , And most of them C The compiler does not check either . therefore , You must make sure that the code you write conforms to the specifications . If not used properly , They cause errors , Cause subtle and confusing symptoms , And it is extremely difficult to find the cause . It's very dangerous to use it with a little knowledge . In that case , It always brings pain, not joy .
Text
1. Usage environment
- CPU
HPUNIX: itanium 9700 series
LINUX:X86 framework
- operating system
HPUNIX:HP-UX xxxxx2 B.11.31 U ia64 2932500072 unlimited-user license
LINUX:Linux xxxxkf1 4.19.90-24.4.v2101.ky10.x86_64 #1 SMP Mon May 24 12:14:55 CST 2021 x86_64 x86_64 x86_64 GNU/Linux
- Compiler program and version
HPUNIX:cc: HP C/aC++ B3910B A.06.25 [Nov 30 2009]
LINUX:
Use built in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/7.3.0/lto-wrapper
The goal is :x86_64-linux-gnu
Configure to :../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,fortran,lto --enable-plugin --enable-initfini-array --disable-libgcj --without-isl --without-cloog --enable-gnu-indirect-function --build=x86_64-linux-gnu --with-stage1-ldflags=' -Wl,-z,relro,-z,now' --with-boot-ldflags=' -Wl,-z,relro,-z,now' --with-tune=generic --with-arch_32=x86-64 --disable-multilib
Threading model :posix
gcc edition 7.3.0 (GCC)
2. Code display
First look at the sample code :
#include<stdio.h>
int main(void)
{
char sPNGZXH[100];
int PNGZXH_LEN = 11;
sprintf(sPNGZXH, "%0*s",PNGZXH_LEN,"123");
printf("%s\n", sPNGZXH);
return 0;
}
- HPUNIX Compile and run
cc sp.c -o sp
./sp
00000000123
- LINUX Compile and run
gcc sp.c -o sp
./sp
123
sprintf
int sprintf( char *buffer, char const *format, … );
sprintf Take its result as a NUL The ending string is stored in the specified buffer Buffer instead of writing to the stream . The return value of the function is the actual number of characters printed or stored .
Warning :
sprintf Is a potential source of error . The size of the buffer is not sprintf One of the parameters of the function , So if the output result is very long and overflows the buffer , It is possible to overwrite the data in the memory location behind the buffer . We should put an end to this problem , There are two strategies . The first 1 One is to declare a very large buffer , But this scheme is a waste of memory , And although large scour zones can reduce the likelihood of spillage , But this possibility cannot be eradicated . The first 2 One method is to analyze the format , See how long the result output will be after the maximum possible value is converted . for example , stay 4 On a bit integer machine , The largest integers are 11 position ( Include a sign bit ), So the buffer can at least hold 12 Characters ( Including the ending NUL byte ). There is no limit to the length of a string , However, the number of characters in the string generated by the function can be limited by an optional field in the formatting code .
Format code
sprintf In the function prototype format The string may contain a format code , It formats the next value in the parameter list in the specified way , As for other characters, they are printed word for word . The format code starts with a percent sign , Followed by (1) Zero or more flag characters , Used to modify the execution mode of some transformations ,(2) An optional minimum field width ,(3) An optional precision ,(4) An optional modifier ,(5) Conversion type .
3. Problem description
With the background above , Look at the output difference between the two platforms .HPUNIX The output under is what we have been using and want at present . However, the code is ported to LINUX Behind the platform , The original normal code ( Look like ) I can't use it now ( String right justified using 0 Fill in the left unused column , It becomes a space ).
First, let's take a look at how the standard describes the symbol characters and their meanings ( Here are just the things we care about ).
| Code | Parameters | meaning |
|---|---|---|
| s | char * | Print a string |
| sign | meaning |
|---|---|
| 0 | When the value is right aligned , By default, unused columns are filled with spaces . This flag means to fill with zeros , It can be used for d,i,u,o,x,X,e,E,f,g and G Code . Use d,i,u,o,x and X Code , If the precision field is given , The zero flag is ignored . If a negative sign appears in the formatting code , The zero flag has no effect . |
| Width | meaning |
|---|---|
| * | The width is format ... is not specified in the string , But it will be placed as an additional integer value parameter before the parameter to be formatted . |
summary
Through careful study standard sign 0 It doesn't apply to s Code ,LINUX Just follow the standard strictly correct Output value in the format of , Instead, HPUNIX The output under is wrong . The root cause of this error : Is typically overly dependent on certain compiler features (HPUNIX Under the cc) Write non portable code . This will make it easier for the program to run in the future 、 Transplant and bury hidden dangers .
Reference resources
边栏推荐
- Introduction to JDBC (IV) - use of Druid connection pool
- 软件设计开发笔记2:基于QT设计串口调试工具
- Database connection exception: create connection error, url: jdbc: mysql://ip/ Database name, errorcode 0, state 08s01 problem handling
- Introduction to JDBC (III) implementation of transaction rollback function
- 啊哈C语言 第7章 有了它你能做更多的事(第27-28讲)
- AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
- Cloud native architecture (04) -cncf
- Pkav simple blasting
- fastjson中的@JSONField注解
- Today's sleep quality record 80 points
猜你喜欢

visdom画多条动态损失曲线

Redis缓存穿透解决方案-布隆过滤器

Build a gocd environment

51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭

Pkav simple blasting

Design and implementation of spark offline development framework

jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定

Facing new challenges and becoming a better self -- an advanced technology er

GDB data reading in GDAL (III) of GIS

Export PDF with watermark
随机推荐
@jsonfield annotation in fastjson
Go language -panic and recover
Wechat applet: elderly blessing short video
Composite API
树莓派assert初步使用练习
Fs2119a Synchronous Boost IC output 3.3V and fs2119b Synchronous Boost IC output 5V
电脑开机显示器黑屏是什么原因,电脑显示器黑屏怎么办
高等数学(第七版)同济大学 习题1-9 个人解答
fastjson中的@JSONField注解
Stm32cube serial port uses dma+idle to receive variable length data
Win11 app store keeps turning around solution
Lottery DDD code
IP6809三线圈15W无线充电发射端方案ic英集芯
数字藏品市场才刚刚开始
WebRTC[47] - WebRTC 保存 YUV 数据的常用方式
LeetCode-1757. Recyclable and low-fat products_ SQL
MySQL面试真题(二十七)——RFM分析法对用户进行分类
Cloud native database is the world of future databases
[opencv450] inter frame difference method
Win11应用商店一直转圈解决办法