任何线索为什么视觉工作室2015爆炸这个代码在2013年工作? [英] Any clue why visual studio 2015 blows up on this code that worked in 2013?

查看:44
本文介绍了任何线索为什么视觉工作室2015爆炸这个代码在2013年工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到2015年社区版,我的程序在发布模式下爆炸,但不是调试模式。经过痛苦的调试,找到了罪魁祸首。在VS 2013社区版中,这两种模式的代码都运行良好。我忽略了什么愚蠢的事情解释了爆炸?它是一个Unicode程序,本节只将字符串更改为大写。



 模板< class T> T toUpper( const  T& str){
T temp = str;
std :: transform(temp.begin(),temp.end(),temp.begin(),_ toupper); // BLOWS UP HERE
return temp;
}





谢谢,

David



每个CHill60的问题,错误是:错误:0x0009A7FC处的访问冲突(试图写入0x0009A7FC),程序终止。最后一个CP是'RF'。



所以它不允许程序在std :: transform调用期间以释放模式重写内存位置。为什么调试模式不受影响?



我尝试过:



我通过将调用代码更改为if x == AAA或x = aaa then ...来消除问题,但我想了解为什么这个代码块不起作用。

解决方案

抛弃模板。这应该有效:



  #include   <   string  >  //或wstring 
#include < ; cctype >





......和......



  for  auto & c:str)c = std :: toupper(c); 





你可能想避免使用_toupper宏。一个定义如下:





  #define _toupper(_Char)((_ Char) - 'a'+'A') 





如果你想要一份副本,而不是就地,这样做:



 std :: string original; 
auto str = original;
for auto & c:str)c = std :: toupper(c) ;


Upgraded to the Community Edition, 2015, and my program blew up in release mode, but not debug mode. After painful debugging, found the culprit. The code worked fine in both modes in VS 2013 community edition. What stupid thing am I overlooking that explains the blowup? It is a Unicode program, and this section only changes a string to upper case.

template <class T> T toUpper(const T & str) {
   T temp = str;
   std::transform(temp.begin(), temp.end(), temp.begin(), _toupper);  //BLOWS UP HERE
   return temp;
   }



Thanks,
David

Per CHill60's question, the error is: "Error: Access violation at 0x0009A7FC (tried to write to 0x0009A7FC), program terminated. Last CP is 'RF'."

So it is not allowing the program to rewrite the memory locations in release mode during the std::transform call. Why isn't debug mode affected?

What I have tried:

I eliminated the problem by changing the calling code to "if x==AAA or x=aaa then ...", but I'd like to understand why this code block didn't work.

解决方案

Ditch the template. This should work:

#include <string> // or wstring
#include <cctype>



... and ...

for (auto & c: str) c = std::toupper(c);



You probably want to avoid the _toupper macro. One definition looks like this:


#define _toupper(_Char)    ( (_Char)-'a'+'A' )



If you want a copy, rather than in-place, do this:

std::string original;
auto str = original;
for (auto & c: str) c = std::toupper(c);


这篇关于任何线索为什么视觉工作室2015爆炸这个代码在2013年工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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