简体   繁体   English

优化使函数立即返回而不是执行

[英]Optimization makes function return immediately instead of executing

I'm working on VS 2010 express and trying to do some file reading and parsing work 我正在开发VS 2010 Express,并尝试进行一些文件读取和解析工作

my function goes something like this... (I dropped the boring parts) 我的功能是这样的...(我删除了无聊的部分)

void SomeClass::SomeFunc(char *ALName, std::map<...> *chromList, std::map<...> *chromLine)
{
    ifstream file;
    char tmpBuff[500];
    char tmpBuff2[500];
    char fileName[350];
    char tmp[350];
    char *pch;
    char *pch2;

    .....

    file.open(fileName);

    while ( file.getline( tmpBuff, 500 ) ) 
    {
        ....
        if ( some_condition == 0 )
        {
          pch2 = strtok( NULL, "," );
          pch = strtok( NULL, "," );
          (*chromList)[pch2] = do_some_work( atoi(pch), tmpBuff2 );
          strcpy( tmp, get_chrom_line( tmpBuff2 ) );
          (*chromLine)[pch2] = tmp;
        }
    }

    file.close();

 }

When I change to Release with Optimization set to Maximum speed this function is skipped. 当我将“发布”的“优化”设置为“最大速度”时,将跳过此功能。 The debugger enters the function and immediately returns. 调试器进入函数并立即返回。

When i run with Debug setting or Release with the Optimization flag set to disabled, the function runs just fine. 当我在“调试”设置下运行或“优化”标志设置为“禁用”的“释放”时,该功能运行良好。

What can be the possible reason for that? 可能是什么原因呢? Can I put a preprocessor definition to force "No optimization" on this function while the rest of the code get optimized 我可以在此代码的其余部分得到优化的同时,在该函数上添加预处理器定义以强制“无优化”吗?

Thanks!! 谢谢!! Idan 伊丹

You should never try to debug optimized code. 您永远不要尝试调试优化的代码。 The line numbers it shows you will rarely match up with what is actually being executed, and forget about reading local variables. 它显示的行号很少会与实际执行的内容匹配,而会忘记读取局部变量。 That is why there is a "Debug" and "Release" mode. 这就是为什么有“调试”和“发布”模式的原因。

However, if you really want to, try this to make Visual Studio not optimize that function. 但是,如果您确实想这样做,请尝试执行此操作以使Visual Studio不优化该功能。 You could also put that function in a separate source file and compile it in debug mode. 您也可以将该函数放在单独的源文件中,并在调试模式下进行编译。

Are you sure that the function is actually being skipped and that the debugger isn't simply making it look skipped? 您确定该函数实际上已被跳过,并且调试器不是简单地使其看起来已被跳过吗?

If it really isn't being executed then it's almost certain you have undefined behavior that happens to work how you want it when not optimized and the compiler (probably rightfully) optimizes it into non-working code in optimized mode. 如果确实没有执行它,那么几乎可以肯定的是,您有未定义的行为,可以在未优化时按您希望的方式工作,并且编译器(可能是正确的)以优化模式将其优化为非工作代码。

That said I see some questionable items: You call strtok with NULL (aside - prefer 0 or nullptr in C++/C++11) without calling it with a valid pointer. 那就是说,我看到一些可疑的项目:您用NULL调用strtok (除了-在C ++ / C ++ 11中更喜欢0nullptr )而没有使用有效的指针调用它。 Are you aware that strtok is destructive? 您是否知道strtok具有破坏性?

Then you use a character pointer to index an array, or presumably (hopefully?) call an overloaded operator[] function with a char* argument. 然后,您可以使用字符指针为数组建立索引,或者大概(希望如此)调用带有char*参数的重载operator[]函数。

Since this is C++, your code will be cleaner, safer, and probably easier to debug if you remove all the C-style parsing and do it with string and find / find_first_of /etc depending on your needs. 由于这是C ++,因此,如果删除所有C样式的解析,并根据需要使用string进行find ,并find / find_first_of / etc,则代码将更加简洁,安全,并且可能更易于调试。

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

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