简体   繁体   English

优化switch语句时Visual Studio 2005 C编译器问题

[英]Visual Studio 2005 C compiler problem when optimizing a switch statement

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. 我碰到了一个,我相信,C ++ - 编译器优化(Visual Studio 2005)问题与switch语句。 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. 我有一个包含switch语句的大约500行代码的小程序。 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). 删除一行(上面代码中指示的那一行)并且问题消失了,因为它在删除优化时也是如此(在整个程序上或在函数上使用#pragma)。

Why does this middle line cause a problem? 为什么这条中间线会导致问题? The compilers optimizer doesn't like pty. 编译器优化器不喜欢pty。 There is no difference in the vectors ptx, pty, and ptz in the program. 程序中的向量ptx,pty和ptz没有区别。 Everything I do to pty I do to ptx and ptz. 我做的一切都是为了ptx和ptz。 I tried swapping their positions in ppt, but pty was still the line causing a problem. 我尝试在ppt中交换他们的位置,但pty仍然是引起问题的线。

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. 真的很抱歉花时间。

If you need to make this code compilable without changing it too much consider using memcpy where you assign a value to ppt[1] . 如果你需要在不改变它的情况下使这个代码可编辑,可以考虑使用memcpyppt[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: 你也可以试试这个东西:

ppt[0]=ptx;
ppt[1]=pty; //<----- this statement causes problems
ppt[2]=ptz;

in another function. 在另一个功能。 This should also help compiler a bit to avoid the path it is taking to compile your code. 这也应该有助于编译器避免编译代码所采用的路径。

Did you try renaming pty to something else (ie pt_y)? 您是否尝试将pty重命名为其他内容(即pt_y)? I encountered a couple of times (ie with a variable "rect2") the problem that some names seem to be "reserved". 我遇到过几次(即变量“rect2”)某些名称似乎是“保留”的问题。

It sounds like a compiler bug. 这听起来像编译器错误。 Have you tried re-ordering the lines? 你试过重新订购线路吗? eg, 例如,

ppt[1]=pty; 
ppt[0]=ptx;
ppt[2]=ptz;

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), eg: 如果你嘲笑分配的值(这将在你的代码中引入错误,但可能指示它的指针或数组是否是问题),例如:

ppt[0] = pty;
ppt[1] = ptz;
ppt[2] = ptx;

(or similar). (或类似的)。

It's probably due to your declaration of ptx, pty and ptz with them being optimised out to use the same address. 这可能是由于您声明ptx,pty和ptz,并将它们优化为使用相同的地址。 Then this action is causing your compiler problems later in your code. 然后,此操作会在您的代码中导致编译器出现问题。

Try 尝试

static double *ptx;
static double *pty;
static double *ptz;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM