忘记包含< string.h>时可能出现运行时错误 [英] Runtime errors possible when I forgot include <string.h>

查看:74
本文介绍了忘记包含< string.h>时可能出现运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello List,


我或多或少有学术问题。


程序中是否会出现运行时错误,如果包括

< string.h>已被遗忘?如果函数的所有参数都是

< string.h>是正确的,是否可能发生错误,以及

这可能是一个错误?


问候


Alexander

Hello List,

I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions of
<string.h> are correct, is it possible that an error occurs, and what
an error might this be?

Regards

Alexander

推荐答案

是的,如果您尝试使用任何
$ b $,您将得到未定义的行为b< string.h>中定义的标准函数并且不包括标题。


例如:

char * strcpy(char * dst,const char * src);


char * strncpy(char * dst,const char * src,size_t n);


char * strdup(const char * s1);


char * strpbrk(const char * s1,const char * s2);


char * strstr(const char * s1,const char * s2);


char * strtok(char * s1,const char * s2);


char * strtok_r(char * s1,const char * s2,char **持续);


以上所有返回''char *''并被定义为< string.h>的一部分
pkg,现在如果你不包括< string.h>并尝试使用以上任何
fns,然后编译器认为这些函数返回''int''(默认情况下)

并以这种方式生成代码,这个结果错误的解释

返回值!


- Ravi


ABeck写道:
Yes, you will get undefined behaviour if you try to use any of the
standard functions defined in <string.h> and not include the header.

For ex:
char *strcpy(char *dst, const char *src);

char *strncpy(char *dst, const char *src, size_t n);

char *strdup(const char *s1);

char *strpbrk(const char *s1, const char *s2);

char *strstr(const char *s1, const char *s2);

char *strtok(char *s1, const char *s2);

char *strtok_r(char *s1, const char *s2, char **lasts);

all the above return ''char *'' and are defined as a part of <string.h> \
pkg, now if you dont include <string.h> and try using any of the above
fns, then the compiler thinks those functions return ''int'' (by default)
and generate code in that way, this results in wrong interpretation of
return values !

- Ravi


ABeck wrote:
Hello List,

我或多或少都有学术问题。

程序中是否会出现运行时错误,如果包含
< string.h>已被遗忘?如果所有参数都是
< string.h>的函数是正确的,是否可能发生错误,这可能是一个错误?

问候

亚历山大
Hello List,

I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions of
<string.h> are correct, is it possible that an error occurs, and what
an error might this be?

Regards

Alexander






Le 25-10-2005,ABeck< al ******************* @ aei.mpg.de> ; écrit*:
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit*:
我或多或少都有学术问题。

如果包含
< string,那么程序中是否会出现运行时错误。 h取代;已被遗忘?如果所有参数都是
< string.h>的函数是正确的,是否可能发生错误,这可能是一个错误?
I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions of
<string.h> are correct, is it possible that an error occurs, and what
an error might this be?




没有包含< string.h>,all函数是'隐含地'

定义'',即返回值应为int和

参数使用''default argument promotion'转换

(6.5.2.2/6),即整数提升和float-> double。


因此,生成的代码可能与
$ b不兼容$ b实施(UB)。如果

int和char *之间的隐式转换是不安全的(例如sizeof(int)< sizeof(char *)

),则会发生奇怪的事情。


但是,在大多数平台上,在int和char *之间隐含转换

表现为代码运行的方式。


Marc Boyer



Without include of <string.h>, all functions are ''implicitely
defined'', that is return value is supposed to be int and
parameters are converted using ''default argument promotion''
(6.5.2.2/6), ie integer promotion and float->double.

So, the generated code could be incompatible with the
implementation (UB). If implicit conversion between
int and char* is unsafe (sizeof(int) < sizeof(char*)
for example), then, strange things will occur.

But, in most platform, implict conversion
between int and char* behaves in a way such that
the code runs.

Marc Boyer


Marc Boyer写道:
Marc Boyer wrote:
Le 25-10-2005,ABeck< ;人******************* @ aei.mpg.de> écrit:
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit :
我或多或少都有学术问题。

如果包含
<,程序中是否会出现运行时错误; string.h中>已被遗忘?如果所有参数都是
< string.h>的函数是正确的,是否可能发生错误,这可能是一个错误?
I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions of
<string.h> are correct, is it possible that an error occurs, and what
an error might this be?



如果不包含< string.h>,则所有函数都是''隐含地定义'',即返回值应该是int和
参数使用''default argument promotion'转换为
(6.5.2.2/6),即整数提升和float-> double。

因此,生成的代码可能与
实现(UB)不兼容。如果
int和char *之间的隐式转换是不安全的(例如sizeof(int)< sizeof(char *)
),则会发生奇怪的事情。

但是在大多数平台上,int和char *之间的转换意味着代码运行的方式。

Marc Boyer


Without include of <string.h>, all functions are ''implicitely
defined'', that is return value is supposed to be int and
parameters are converted using ''default argument promotion''
(6.5.2.2/6), ie integer promotion and float->double.

So, the generated code could be incompatible with the
implementation (UB). If implicit conversion between
int and char* is unsafe (sizeof(int) < sizeof(char*)
for example), then, strange things will occur.

But, in most platform, implict conversion
between int and char* behaves in a way such that
the code runs.

Marc Boyer



在某些情况下(取决于所调用的函数和使用的
编译器),编译器将使用它自己的内置函数。

再次,这取决于正在使用的函数以及

编译器。


-Joe


In some cases (depending on what functions are bing called and what
compiler is used) the compiler will use it''s own built-in function(s).
Again, this depends on what functions are being used as well as the
compiler.

-Joe


这篇关于忘记包含&lt; string.h&gt;时可能出现运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆