C中的常量表达式 [英] Constant Expressions in C

查看:75
本文介绍了C中的常量表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个例子:


u8 my_array [10];


for(i = 0; i<(sizeof(my_array) )/ sizeof(u8)); i ++){

...

}


在标准C规范中,是评估

''(sizeof(my_array)/ sizeof(u8))''到10保证在编译时发生

时间?或者它是编译器优化?从我所读到的,它看起来像是b $ b看起来它是一个编译器优化,但我想得到

确认。


现在如果在编译时没有保证会发生这种情况,那么最好是做下面的事情:


const u8 my_array_size =(sizeof(my_array) )/ sizeof(u8));


保证你没有在运行时划分?在我所使用的

系统上,划分需要很长时间。但是,

我所处理的几乎所有编译器都在

编译时进行评估。

解决方案

4月20日晚上11点33分,skibud2< mike.hallo ... @ gmail.comwrote:


现在如果是没有保证在编译时发生,是否更好做

如下:


const u8 my_array_size =(sizeof(my_array)/ sizeof( u8));


保证你在运行时没有划分?在我所使用的

系统上,划分需要很长时间。但是,

我所处理的几乎所有编译器都在

编译时进行评估。



好​​奇:哪个编译器没有?




" skibud2" < mi *********** @ gmail.comwrote in message

news:11 ******************* ***@o5g2000hsb.googlegro ups.com ...


考虑这个例子:


u8 my_array [10] ;


for(i = 0; i<(sizeof(my_array)/ sizeof(u8)); i ++){

...

}


在标准C规范中,是评估

''(sizeof(my_array)/ sizeof(u8)) ''到10岁时保证在编译时发生

时间?或者它是编译器优化?从我所读到的,它看起来像是b $ b看起来它是一个编译器优化,但我想得到

确认。


现在如果在编译时没有保证会发生这种情况,那么最好是做下面的事情:


const u8 my_array_size =(sizeof(my_array) )/ sizeof(u8));


保证你没有在运行时划分?在我所使用的

系统上,划分需要很长时间。但是,

几乎我处理过的所有编译器都在

编译时进行评估。



有效保证编译器将在编译时计算常量

表达式。只要行为正确,标准就没有指定任何特定的机器代码指令序列,因为决定不试图强加性能要求。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


skibud2写道:


考虑这个例子:


u8 my_array [10];


for(i = 0; i<(sizeof(my_array)/ sizeof(u8)); i ++){

...

}


在标准的C规范中,是评估

''(sizeof(my_array)/ sizeof(u8))''到10保证在编译时发生

时间?或者它是编译器优化?从我读过的内容来看,它是b $ b看起来它是一个编译器优化,但我想得到

确认。



sizeof(my_array)/ sizeof(u8)必须是编译时常量,

考虑


int my_array [10];

int other [sizeof(my_array)/ sizeof(int)];


现在,如果在编译时没有保证,是最好做什么

类似如下:


const u8 my_array_size =(sizeof(my_array)/ sizeof(u8));



从某种意义上讲,它使代码更易于阅读。


-

Ian Collins。


Consider this example:

u8 my_array[10];

for (i = 0; i < (sizeof(my_array)/sizeof(u8)); i++) {
...
}

In the standard C specification, is the evaluation of
''(sizeof(my_array)/sizeof(u8))'' to 10 guarenteed to happen at compile
time? Or is it a compiler optimization? From what I have read, it
looks like it is a compiler optimization, but I would like to get
confirmation.

Now if is not guarenteed to happen at compile time, is it better to do
something like the following:

const u8 my_array_size = (sizeof(my_array)/sizeof(u8));

to guarentee that you are not doing a divide a run time? On the
systems I have worked on, divides take a very long time. However,
almost every compiler that I have dealt with does the evaluation at
compile time.

解决方案

On Apr 20, 11:33 pm, skibud2 <mike.hallo...@gmail.comwrote:

Now if is not guarenteed to happen at compile time, is it better to do
something like the following:

const u8 my_array_size = (sizeof(my_array)/sizeof(u8));

to guarentee that you are not doing a divide a run time? On the
systems I have worked on, divides take a very long time. However,
almost every compiler that I have dealt with does the evaluation at
compile time.

Just curious: Which compiler didn''t?



"skibud2" <mi***********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...

Consider this example:

u8 my_array[10];

for (i = 0; i < (sizeof(my_array)/sizeof(u8)); i++) {
...
}

In the standard C specification, is the evaluation of
''(sizeof(my_array)/sizeof(u8))'' to 10 guarenteed to happen at compile
time? Or is it a compiler optimization? From what I have read, it
looks like it is a compiler optimization, but I would like to get
confirmation.

Now if is not guarenteed to happen at compile time, is it better to do
something like the following:

const u8 my_array_size = (sizeof(my_array)/sizeof(u8));

to guarentee that you are not doing a divide a run time? On the
systems I have worked on, divides take a very long time. However,
almost every compiler that I have dealt with does the evaluation at
compile time.

Effectively you are guaranteed that the compiler will evaluate constant
expressions at compile time. The standard doesn''t specify any particular
sequence of machine code instructions as long as the behaviour is correct,
because it was decided not to try impose performance requirements at all.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


skibud2 wrote:

Consider this example:

u8 my_array[10];

for (i = 0; i < (sizeof(my_array)/sizeof(u8)); i++) {
...
}

In the standard C specification, is the evaluation of
''(sizeof(my_array)/sizeof(u8))'' to 10 guarenteed to happen at compile
time? Or is it a compiler optimization? From what I have read, it
looks like it is a compiler optimization, but I would like to get
confirmation.

sizeof(my_array)/sizeof(u8) is required to be a compile time constant,
consider

int my_array[10];
int other[sizeof(my_array)/sizeof(int)];

Now if is not guarenteed to happen at compile time, is it better to do
something like the following:

const u8 my_array_size = (sizeof(my_array)/sizeof(u8));

Better in the sense it makes the code easier to read.

--
Ian Collins.


这篇关于C中的常量表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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