限制例子 - 合理吗? [英] restrict example - reasonable?

查看:56
本文介绍了限制例子 - 合理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您要编译/运行下面的代码,并得到30的结果,那么我将非常有兴趣了解您正在使用的编译器 - 以及它的优化

设置[尤其是如果它支持''restrict''关键字]


#include< stdio.h>


int test(int * a, int * b)

{

* a = 5;

* b = 6;


返回(* a)*(* b);

}


int main(无效)

{

int x = 0;


int a = test(& x,& x);


printf( %d \ n,a);

}


代码的希望是找到gcc的情况优化器不会
获取* a和* b,但假设它们的值为5和6. gcc我正在使用

产生36而不管是什么永远的价值在-On开关中。


我希望能够编译一些带有各种优化的代码 -

显示代码当'和b指向main中的相同变量,

然后引入''restrict''以显示它被修复时,''break''。上面会是一个合理的候选人来展示这样的东西吗?


-

======= =======

*不是学究者*

==============

If you were to compile/run the code below, and get the result ''30'', I''d be
very interested to know what compiler you''re using - and its optimisation
settings [esp. if it supports the ''restrict'' keyword]

#include <stdio.h>

int test(int * a, int * b)
{
*a = 5;
*b = 6;

return (*a) * (*b);
}

int main(void)
{
int x = 0;

int a = test(&x, &x);

printf("%d\n", a);
}

The ''hope'' with the code was to find a case where the gcc optimiser doesn''t
fetch *a and *b, but assumes their values to be 5 and 6. The gcc I''m using
produces 36 irrespective of what ever n''s value is in -On switches.

I wanted to be able to compile some code with various optimisations - and
show that the code ''breaks'' when a and b point to the same variable in main,
and then to introduce ''restrict'' to show it get fixed. Would the above be a
reasonable candidate for showing such a thing?

--
==============
*Not a pedant*
==============

推荐答案

你好Pemo


你用2倍相同的内存区调用函数测试

所以a = = b(总是)


设置* a你也给* ba值,反之亦然


所以行* a = 5;也* * b = 5

line * b = 6;也使* a = 6;


行返回(* a)*(* b);返回6 * 6


没有编译器选项可以更改它。

这是指针工作的方式!!!


你正在做什么看起来像这个


int main(无效)

{

int a, * b;

b =& a;

* b = 3; //不是导入和

a = 6;

printf(" val =%d \ n",a *(* b));

返回0;

}

pemo schreef:
Hello Pemo

You are calling function test with 2 times the same memory area
so a == b (always)

by setting *a you also giving *b a value and visa versa

so the line *a = 5; makes also *b = 5
line *b=6; makes also *a = 6;

the line return (*a) * (*b); returns 6 * 6

There will be no compiler option to change it.
This is the way pointes are working!!!

what you are doing looks linke this

int main(void)
{
int a, *b;
b = &a;
*b = 3; // is not importand
a=6;
printf("val = %d\n", a * (*b));
return 0;
}
pemo schreef:
如果你要编译/运行下面的代码,得到结果30,我会非常有兴趣知道你正在使用什么编译器 - 以及它的优化设置[尤其是如果它支持''restrict''关键字]

#include< stdio.h>

int test(int * a,int * b)
{
* a = 5;
* b = 6;

返回(* a)*(* b);
}
int main(void)
int x = 0;

int a = test(& x,& x);

printf("%d \ n",a);
}

代码的''希望'是找到gcc优化器不能<的情况br /> fetch * a和* b,但假设它们的值为5和6. gcc我正在使用
产生36,而不管在-On开关中n'的值是什么。 />
我希望能够编译一些具有各种优化的代码 - 并且
显示当a和b指向main中的相同变量时,代码''中断',
然后引入限制来表明它得到修复。以上是否是展示此类事物的合理候选人?

-
==============
*不是学究者*
==============
If you were to compile/run the code below, and get the result ''30'', I''d be
very interested to know what compiler you''re using - and its optimisation
settings [esp. if it supports the ''restrict'' keyword]

#include <stdio.h>

int test(int * a, int * b)
{
*a = 5;
*b = 6;

return (*a) * (*b);
}

int main(void)
{
int x = 0;

int a = test(&x, &x);

printf("%d\n", a);
}

The ''hope'' with the code was to find a case where the gcc optimiser doesn''t
fetch *a and *b, but assumes their values to be 5 and 6. The gcc I''m using
produces 36 irrespective of what ever n''s value is in -On switches.

I wanted to be able to compile some code with various optimisations - and
show that the code ''breaks'' when a and b point to the same variable in main,
and then to introduce ''restrict'' to show it get fixed. Would the above be a
reasonable candidate for showing such a thing?

--
==============
*Not a pedant*
==============






mdler写道:
mdler wrote:
如果您要编译/运行下面的代码,并得到30的结果,
我会非常感兴趣知道你正在使用什么编译器 - 以及它的优化设置[尤其是如果它支持''restrict''关键字]

#include< stdio.h>

int test(int * a,int * b)
{
* a = 5;
* b = 6;

返回(* a)*(* b);
}
int main(void)
int x = 0;

int a = test(& x,& x);

printf("%d \ n",a);
}

代码的''希望'是找到gcc优化器没有的情况不要取* a和* b,但假设它们的值为5和6.
我正在使用的gcc产生36而不管n的值是什么
in -On开关。

我希望能够编译一些带有各种优化的代码
- 并且当a和b指向相同的变量时显示代码''中断''在main中,然后引入''restrict''来显示它得到修复。以上是显示这样一个事物的合理候选者吗?
If you were to compile/run the code below, and get the result ''30'',
I''d be very interested to know what compiler you''re using - and its
optimisation settings [esp. if it supports the ''restrict'' keyword]

#include <stdio.h>

int test(int * a, int * b)
{
*a = 5;
*b = 6;

return (*a) * (*b);
}

int main(void)
{
int x = 0;

int a = test(&x, &x);

printf("%d\n", a);
}

The ''hope'' with the code was to find a case where the gcc optimiser
doesn''t fetch *a and *b, but assumes their values to be 5 and 6.
The gcc I''m using produces 36 irrespective of what ever n''s value is
in -On switches.

I wanted to be able to compile some code with various optimisations
- and show that the code ''breaks'' when a and b point to the same
variable in main, and then to introduce ''restrict'' to show it get
fixed. Would the above be a reasonable candidate for showing such a
thing?


你用2倍相同的内存区调用函数测试
所以a == b(总是)<设置* a你还给* ba值,反之亦然

所以行* a = 5;也* * b = 5
line * b = 6;也行* a = 6;

行返回(* a)*(* b);返回6 * 6

没有编译器选项可以更改它。
这是指针工作的方式!!!

你在做什么看起来像linke这个

{/>} a,* b;
b =& a;
* b = 3; //不是importand
a = 6;
printf(" val =%d \ n",a *(* b));
返回0;
}

You are calling function test with 2 times the same memory area
so a == b (always)

by setting *a you also giving *b a value and visa versa

so the line *a = 5; makes also *b = 5
line *b=6; makes also *a = 6;

the line return (*a) * (*b); returns 6 * 6

There will be no compiler option to change it.
This is the way pointes are working!!!

what you are doing looks linke this

int main(void)
{
int a, *b;
b = &a;
*b = 3; // is not importand
a=6;
printf("val = %d\n", a * (*b));
return 0;
}



是的,我知道代码是做什么的(这听起来不是这样吗?)但优化

编译器可能会假设,它''知道''是什么* a和* b值在test()中,并且

它可以在乘法中使用5和6 - 而不是deref

指针。


另外,如果你想避免被人大喊,请不要在这里发帖。


-

==============

*不是学生*

==============


Yes, I know what the code does (does it not sound so?) BUT an optimising
compiler might assume, it ''knows'' what *a and *b values are in test(), and
that it can use 5 and 6 in the multiplication - rather than deref the
pointers.

Also,if you want to avoid being shouted at, please don''t top post here.

--
==============
*Not a pedant*
==============


pemo写道:
如果您要编译/运行下面的代码,并得到30的结果,我会非常有兴趣知道你正在使用什么编译器 - 以及它的优化设置[尤其是如果它支持''restrict''关键字]

#include< stdio.h>

int test(int * a,int * b)
{
* a = 5;
* b = 6;

返回(* a)*(* b);
}
int main(void)
int x = 0;

int a = test(& x,& x);

printf("%d \ n",a);
}

代码的''希望'是找到gcc优化器不能<的情况br /> fetch * a和* b,但假设它们的值为5和6. gcc我正在使用
产生36,而不管在-On开关中n'的值是什么。 />
我希望能够编译一些具有各种优化的代码 - 并且
显示当a和b指向main中的相同变量时,代码''中断',
然后引入限制来表明它得到修复。以上是否是显示这种事情的合理候选人?
If you were to compile/run the code below, and get the result ''30'', I''d be
very interested to know what compiler you''re using - and its optimisation
settings [esp. if it supports the ''restrict'' keyword]

#include <stdio.h>

int test(int * a, int * b)
{
*a = 5;
*b = 6;

return (*a) * (*b);
}

int main(void)
{
int x = 0;

int a = test(&x, &x);

printf("%d\n", a);
}

The ''hope'' with the code was to find a case where the gcc optimiser doesn''t
fetch *a and *b, but assumes their values to be 5 and 6. The gcc I''m using
produces 36 irrespective of what ever n''s value is in -On switches.

I wanted to be able to compile some code with various optimisations - and
show that the code ''breaks'' when a and b point to the same variable in main,
and then to introduce ''restrict'' to show it get fixed. Would the above be a
reasonable candidate for showing such a thing?




我认为你应该以另一种方式工作。我的意思是,声明两个

参数作为限制并显示这允许某些优化

完成并且当两个参数指向
$时代码中断b $ b相同的变量。


然后在没有限制限定符的情况下编译源代码并显示

编译器不能再作出某些假设并且这会阻止

某些优化。


HTH



I think you should work the other way arround. I mean, declare both
parameters as restrict and show that this allows certain optimizations
to be done and that the code breaks when both parameters point to the
same variable.

Then compile the source without the restrict qualifier and show that
the compiler cannot longer make some assumptions and that that prevents
certain optimizations.

HTH


这篇关于限制例子 - 合理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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