Visual Studio 2005 C 编译器优化 switch 语句时出现问题
其他人可能感兴趣的一般问题:
我认为我遇到了一个带有 switch 语句的 C++ 编译器优化 (Visual Studio 2005) 问题。我想知道是否有任何方法可以满足我的好奇心并找出编译器正在尝试但未能做到的事情。有没有我可以花一些时间(可能太多时间)破译的日志?
我的具体问题对于那些有足够好奇心继续阅读的人 - 我想听听您对为什么我在这个具体案例中遇到问题的想法。
我有一个小程序,大约有 500 行代码,其中包含 switch 语句。它的一些情况包含一些指针的赋值。
double *ptx, *pty, *ptz;
double **ppt = new double*[3];
//some code initializing etc ptx, pty and ptz
ppt[0]=ptx;
ppt[1]=pty; //<----- this statement causes problems
ppt[2]=ptz;
中间的语句似乎挂起编译器。编译永无止境。好吧,我没等太久就走过大厅,与一些人交谈,喝了一杯咖啡,然后回到我的办公桌,但这是一个很小的程序,通常在不到一秒钟的时间内编译完成。删除一行(上面代码中指示的行),问题就会消失,就像删除优化(在整个程序上或在函数上使用 #pragma)时一样。
为什么这条中间线会出现问题呢?编译器优化器不喜欢 pty。 程序中向量ptx、pty、ptz没有区别。我对 pty 所做的一切都是对 ptx 和 ptz 所做的。我尝试在 ppt 中交换它们的位置,但 pty 仍然是导致问题的行。
我问这个是因为我很好奇正在发生的事情。代码被重写并且工作正常。
编辑: 大约两周后,我检查了与上面描述的代码最接近的版本,但我无法对其进行编辑以使其崩溃。这实在是令人恼火、尴尬和恼怒。我会再试一次,但如果我没有很快解决它,我想问题的这一部分已经过时了,我会删除它。真的很抱歉耽误您的时间。
General Question which may be of interest to others:
I ran into a, what I believe, C++-compiler optimization (Visual Studio 2005) problem with a switch statement. What I'd want to know is if there is any way to satisfy my curiosity and find out what the compiler is trying to but failing to do. Is there any log I can spend some time (probably too much time) deciphering?
My specific problem for those curious enough to continue reading - I'd like to hear your thoughts on why I get problems in this specific case.
I've got a tiny program with about 500 lines of code containing a switch statement. Some of its cases contain some assignment of pointers.
double *ptx, *pty, *ptz;
double **ppt = new double*[3];
//some code initializing etc ptx, pty and ptz
ppt[0]=ptx;
ppt[1]=pty; //<----- this statement causes problems
ppt[2]=ptz;
The middle statement seems to hang the compiler. The compilation never ends. OK, I didn't wait for longer than it took to walk down the hall, talk to some people, get a cup of coffee and return to my desk, but this is a tiny program which usually compiles in less than a second. Remove a single line (the one indicated in the code above) and the problem goes away, as it also does when removing the optimization (on the whole program or using #pragma on the function).
Why does this middle line cause a problem? The compilers optimizer doesn't like pty.
There is no difference in the vectors ptx, pty, and ptz in the program. Everything I do to pty I do to ptx and ptz. I tried swapping their positions in ppt, but pty was still the line causing a problem.
I'm asking about this because I'm curious about what is happening. The code is rewritten and is working fine.
Edit:
Almost two weeks later, I check out the closest version to the code I described above and I can't edit it back to make it crash. This is really annoying, embarrassing and irritating. I'll give it another try, but if I don't get it breaking anytime soon I guess this part of the question is obsolete and I'll remove it. Really sorry for taking your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您需要使此代码可编译而不需要对其进行太多更改,请考虑使用
memcpy
,在其中为ppt[1]
分配一个值。这至少应该编译得很好。但是,您的问题似乎更像是源代码的另一部分导致了这种行为。
您还可以尝试将这些东西:放在
另一个函数中。
这也应该有助于编译器避免编译代码所采用的路径。
If you need to make this code compilable without changing it too much consider using
memcpy
where you assign a value toppt[1]
. This should at least compile fine.However, you problem seems more like another part of the source code causes this behaviour.
What you can also try is to put this stuff:
in another function.
This should also help compiler a bit to avoid the path it is taking to compile your code.
您是否尝试将 pty 重命名为其他名称(即 pt_y)?我遇到过几次(即使用变量“rect2”)某些名称似乎是“保留”的问题。
Did you try renaming pty to something else (i.e. pt_y)? I encountered a couple of times (i.e. with a variable "rect2") the problem that some names seem to be "reserved".
听起来像是编译器错误。您是否尝试过重新排序线路?例如,
如果您处理分配的值(这会在代码中引入错误,但可能表明问题是指针还是数组),也会发生什么,例如:(
或类似的)。
It sounds like a compiler bug. Have you tried re-ordering the lines? e.g.,
Also what happens if you juggle about the values that are assigned (which will introduce bugs in your code, but may indicator whether its the pointer or the array that's the issue), e.g.:
(or similar).
这可能是由于您声明了 ptx、pty 和 ptz,并且它们被优化为使用相同的地址。那么这个操作会导致你的代码稍后出现编译器问题。
尝试
It's probably due to your declaration of ptx, pty and ptz with them being optimised out to use the same address. Then this action is causing your compiler problems later in your code.
Try