函数调用,强制编译器提升int参数tosize_t? [英] function calls, mandatory compiler promotion of int arguments tosize_t?

查看:81
本文介绍了函数调用,强制编译器提升int参数tosize_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个小测试程序:


#include< stdio.h>

void main(void){

char buffer [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZ \ n";

int size,count;

size = 27; / *总是一个小数字,适合int * /

count = 1; / * ditto * /

(void)fwrite(buffer,size,count,stdout);

}


int变量大小和计数都用于fwrite()参数

,其原型为size_t。如果size_t大于int(如

一些64位系统,其中size_t是8个字节,int是4个字节),那么C标准是否要求编译器提升将这两个int值传递给

size_t然后再将它们传递给函数,如果是这样,那究竟是什么呢?
它说什么?


我知道在_practice_上面的代码有效,但我很好奇,如果那个

必须始终如此。


谢谢,


David Mathog

For this small test program:

#include <stdio.h>
void main(void){
char buffer[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
int size, count;
size = 27; /* always a small number which will fit in an int */
count = 1; /* ditto */
(void) fwrite(buffer, size, count, stdout);
}

The int variables size and count are both used for fwrite() arguments
which are prototyped to be size_t. If size_t is bigger than int (as on
some 64 bit systems where size_t is 8 bytes and int is 4 bytes), does
the C standard require the compiler to promote these two int values to
size_t before passing them to the function, and if so, what exactly does
it say?

I know that in _practice_ the code above works, but I am curious if that
must always be the case.

Thanks,

David Mathog

推荐答案

David Mathog写道:
David Mathog wrote:

对于这个小测试程序:


#include< stdio.h>

void main(void){
For this small test program:

#include <stdio.h>
void main(void){



Tut。啧啧,啧啧,啧啧。考虑你的手腕拍打。

Tut. Tut, tut, tut. Consider your wrist slapped.


char buffer [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZ \ n" ;;

int size,count;

size = 27; / *总是一个小数字,适合int * /

count = 1; / * ditto * /

(void)fwrite(buffer,size,count,stdout);

}


int变量大小和计数都用于fwrite()参数

,其原型为size_t。如果size_t大于int(如

一些64位系统,其中size_t是8个字节,int是4个字节),那么C标准是否要求编译器提升将这两个int值传递给

size_t然后再将它们传递给函数,如果是这样,那究竟是什么呢?b
它说什么?
char buffer[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
int size, count;
size = 27; /* always a small number which will fit in an int */
count = 1; /* ditto */
(void) fwrite(buffer, size, count, stdout);
}

The int variables size and count are both used for fwrite() arguments
which are prototyped to be size_t. If size_t is bigger than int (as on
some 64 bit systems where size_t is 8 bytes and int is 4 bytes), does
the C standard require the compiler to promote these two int values to
size_t before passing them to the function, and if so, what exactly does
it say?



是和6.5.2.2p7:如果表示被调用的

函数的表达式具有包含原型的类型,参数是

隐式转换,就像通过赋值一样,转换为

对应参数的类型,将每个参数的类型设为

其声明类型的非限定版本。

Yes, and 6.5.2.2p7: "If the expression that denotes the called
function has a type that does include a prototype, the arguments are
implicitly converted, as if by assignment, to the types of the
corresponding parameters, taking the type of each parameter to be the
unqualified version of its declared type."


我知道在_practice_上面的代码有效,但我很好奇,如果那个

必须始终如此。
I know that in _practice_ the code above works, but I am curious if that
must always be the case.



是的,确实如此。请注意,原型控制的转换会同时削减两种方式,但是:如果您通过了广泛的转换。参数表达式为原型

函数,其中较窄的参数参数,从宽到窄的转换

可能会默默地丢失信息。类似地,如果将浮点

参数传递给原型整数参数,则小数部分将以静默方式消失

。 (这里有一个IOCCC条目的幼苗,我想:尝试

` malloc(atan(42.0))''分配一个88字节的对象......)


-
Er ********* @ sun。 com


Eric Sosman< Er ********* @ sun.comwrites:

[.. 。]
Eric Sosman <Er*********@sun.comwrites:
[...]

是的,确实如此。请注意,原型控制的转换会同时削减两种方式,但是:如果您通过了广泛的转换。参数表达式为原型

函数,其中较窄的参数参数,从宽到窄的转换

可能会默默地丢失信息。
Yes, it is. Note that the prototype-governed conversions cut both
ways, though: If you pass a "wide" argument expression to a prototyped
function with a "narrower" parameter, the conversion from wide to narrow
may lose information silently.



或者它可能会给你一些意想不到的结果。转换为已签名的

整数类型,如果该值不能在目标类型中表示,则
会产生实现定义的结果(或引发

实现定义的信号;后者是C99中的新信号。

Or it may give you some unexpected result. Conversion to a signed
integer type, if the value can''t be represented in the target type,
yields an implementation-defined result (or raises an
implementation-defined signal; the latter is new in C99).


同样,如果你传递一个浮点数

对一个原型整数参数的参数,小数部分默默地消失了

。 (这里有一个IOCCC条目的幼苗,我想:尝试

` malloc(atan(42.0))''分配一个88字节的对象......)
Similarly, if you pass a floating-point
argument to a prototyped integer parameter, the fractional part vanishes
silently. (There''s the seedling of an IOCCC entry here, I think: try
`malloc(atan(42.0))'' to allocate an 88-byte object ...)



对于某些值42.0。


实际上,* no *值为42.0; atan()的结果在

范围内[-pi / 2,+ pi / 2](弧度)。


但tan(1.5595)会做的。请注意,

参数中的微小变化会导致结果发生很大变化。


-

Keith Thompson(The_Other_Keith ) ks***@mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,Yes Minister

For certain values of 42.0.

Actually, for *no* values of 42.0; the results of atan() are in the
range [-pi/2, +pi/2] (radians).

But tan(1.5595) would do it. Note that very small changes in the
argument result in large changes in the result.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Keith Thompson写道:
Keith Thompson wrote:

Eric Sosman< Er ********* @ sun.comwrites:

[...]
Eric Sosman <Er*********@sun.comwrites:
[...]

>是的。请注意,原型控制的转换会切断两种方式,但是:如果你通过了宽的转换。参数表达式为具有较窄的原型
函数。参数,从宽到窄的转换可能会无声地丢失信息。
> Yes, it is. Note that the prototype-governed conversions cut both
ways, though: If you pass a "wide" argument expression to a prototyped
function with a "narrower" parameter, the conversion from wide to narrow
may lose information silently.



或者它可能会给你一些意想不到的结果。转换为已签名的

整数类型,如果该值不能在目标类型中表示,则
会产生实现定义的结果(或引发

实现定义的信号;后者是C99中的新功能。


Or it may give you some unexpected result. Conversion to a signed
integer type, if the value can''t be represented in the target type,
yields an implementation-defined result (or raises an
implementation-defined signal; the latter is new in C99).



这两项都属于丢失信息,在我看来。

Both of these qualify as "losing information," in my view.


>类似地,如果将浮点
参数传递给原型整数参数,则小数部分将以静默方式消失。 (这里有一个IOCCC条目的幼苗,我想:尝试使用malloc(atan(42.0))''来分配一个88字节的对象......)
> Similarly, if you pass a floating-point
argument to a prototyped integer parameter, the fractional part vanishes
silently. (There''s the seedling of an IOCCC entry here, I think: try
`malloc(atan(42.0))'' to allocate an 88-byte object ...)



对于某些值42.0。


实际上,* no *值为42.0; atan()的结果在

范围内[-pi / 2,+ pi / 2](弧度)。


For certain values of 42.0.

Actually, for *no* values of 42.0; the results of atan() are in the
range [-pi/2, +pi/2] (radians).



哦,drat!计算器在度数模式下运行而不是弧度,

大脑以la-la模式运行而不是通常的acu> ...

Oh, drat! Calculator operating in degree mode instead of radians,
brain operating in la-la mode instead of with its usual acuituity ...


但是晒黑(1.5595)就可以了。请注意,

参数中的非常小的更改会导致结果发生较大变化。
But tan(1.5595) would do it. Note that very small changes in the
argument result in large changes in the result.



我也考虑了log(1E30)和pow(42.0,2.40),但他们

似乎不够混淆。 />

-
Er ********* @ sun.com


这篇关于函数调用,强制编译器提升int参数tosize_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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