大连做公司网站的公司,wordpress页面链接如何修改,分发平台,网站建设做网站可以吗这个是今天在微信群里讨论的一个问题#xff0c;先看图片点击查看大图代码流程大概是这个样子的点击查看大图查看 length 和 space1 的值#xff0c;明显看到 length 小于 space1 的值#xff0c;即使是这样小白都能搞懂流程的情况下#xff0c;代码还是跑到else里面区执行… 这个是今天在微信群里讨论的一个问题先看图片点击查看大图代码流程大概是这个样子的点击查看大图查看 length 和 space1 的值明显看到 length 小于 space1 的值即使是这样小白都能搞懂流程的情况下代码还是跑到else里面区执行调试查看数据然后 我们就在群里讨论有的大神说这个是内存越界也有大神说可能是人品有问题也有大神说这个是因为写代码前没有选好一个良辰吉日反正大家想法都非常多也非常古怪这可能就是讨论群存在的一个原因了。经过不断的验证发现这个问题是因为编译器优化的问题。如果在设置里面把优化选项去掉代码就执行正确编译器对代码优化当然还有一个问题就是如果我想开启优化毕竟代码太大占用的存储空间是很大的如果能节省点空间是最好的了。所以就出现了一种只针对某些代码不优化的设置像这样我们使用GCC编译的时候也是有可能因为代码优化导致这样的问题的庆幸的是GCC也有设置不进行优化的开关。#使用GCC编译器设置选择性不优化某段代码#pragma GCC push_options
#pragma GCC optimize (O0)
#pragma GCC pop_options
push 的意思是把当前的编译优化选项压栈然后再设置当前的优化选项之后再出栈把之前压栈的选项提取出来。具体可以参考链接https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Function-Specific-Option-Pragmas.html链接里面还介绍了一些其他的用法原文如下5.52.12 Function Specific Option Pragmas
#pragma GCC target (string...)
This pragma allows you to set target specific options for functions defined later in the source file. One or more strings can be specified. Each function that is defined after this point will be as if attribute((target(STRING))) was specified for that function. The parenthesis around the options is optional. See Function Attributes, for more information about the target attribute and the attribute syntax.
The #pragma GCC target pragma is not implemented in GCC versions earlier than 4.4, and is currently only implemented for the 386 and x86_64 backends.#pragma GCC optimize (string...)
This pragma allows you to set global optimization options for functions defined later in the source file. One or more strings can be specified. Each function that is defined after this point will be as if attribute((optimize(STRING))) was specified for that function. The parenthesis around the options is optional. See Function Attributes, for more information about the optimize attribute and the attribute syntax.
The #pragma GCC optimize pragma is not implemented in GCC versions earlier than 4.4.#pragma GCC push_options
#pragma GCC pop_options
These pragmas maintain a stack of the current target and optimization options. It is intended for include files where you temporarily want to switch to using a different #pragma GCC target or #pragma GCC optimize and then to pop back to the previous options.
The #pragma GCC push_options and #pragma GCC pop_options pragmas are not implemented in GCC versions earlier than 4.4.#pragma GCC reset_options
This pragma clears the current #pragma GCC target and #pragma GCC optimize to use the default switches as specified on the command line.
The #pragma GCC reset_options pragma is not implemented in GCC versions earlier than 4.4.
#当然还有指定某个函数设置优化等级int max(int a, int b) __attribute__((optimize(O0)));
{return a b ? a : b;
}
#使用volatile 关键字避免编译器优化volatile 的作用是提醒CPU如果遇到被volatile 修饰的变量要从内存里面去取值而不要偷懒直接从缓存里面取值我们一般是用在那些被中断处理函数使用的那些变量。如果有些代码你不希望CPU偷懒那你就可以加上volatile 让CPU从内存取数据。CSDN上有这样一个例子https://blog.csdn.net/qq_28637193/article/details/88988951今天碰到一个gcc优化相关的问题为了让一个页变成脏页页表中dirty位被置上需要执行下面这段代码1 uint32_t *page;
2 // ...
3 page[0] page[0];
最后一行代码很有可能被gcc优化掉因为这段代码看起来没有任何实际的作用。那么如何防止gcc对这段代码做优化呢设置gcc编译时优化级别为-O0肯定是不合适的这样对程序性能影响会比较大。stackoverflow上的Dietrich Epp给出了一个强制类型转换的方案((unsigned char volatile *)page)[0] page[0];
通过volatile关键字禁止gcc的优化#总结、什么情况会导致这样的问题1、堆栈溢出应该是一个原因之前我有遇到的情况是栈空间设置太小然后溢出到堆空间导致问题。2、使用某个函数导致溢出我们使用的函数比如内存拷贝函数如果长度设置不对也会导致影响到其他的代码。3、还有就是上面说的编译器优化导致的问题。评论说说你在开发过程总遇到过哪些奇葩的问题又是如何解决的呢#推荐阅读 专辑|Linux文章汇总 专辑|程序人生 专辑|C语言嵌入式Linux微信扫描二维码关注我的公众号