比较一个(int)double和(int)int时的异常 [英] Exception when comparing an (int)double and (int)int

查看:149
本文介绍了比较一个(int)double和(int)int时的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯所以我使用pdCurses的lib和stringStream来计算一个5字符长的字符串,代表一个时钟。它显示为00:00,0:00,00.00或0.000。但是,当我运行我的函数时,我得到一个exeption抛出在这一部分:

  if((int)time> = 10)
{
if((int)time> = 60)
{
if((int)time> = 600)
{

异常指向这也说有访问冲突:

  / *验证块类型* / 
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse));

为什么会发生像if语句一样简单的事情?我该如何解决?感谢任何帮助! =)此处还有我的功能:

  /////////////// ///////////////////////////刷新//////////////////// /////// 
void Refresh()
{
for(int r = 0; r< nrows; r ++)
{
move(r, 0);
instr((char *)_ contents [r] .c_str());
} //确保以后工作

//插入当前时间;
enum {time_loc_y = 24,time_loc_x = 10};
long float time = myStopwatch.ElapsedTime();
string time_s =;
string min; ss < (int)的时间%60; ss>>分钟;
string sec; ss < (INT)时间/ 60; ss>>秒;

if((int)time> = 10)
{
if((int)time> = 60)
{
if (int)time> = 600)
{
time_s.insert(0,min); // 00:00
time_s.insert(time_s.begin()+ 2,':');
time_s.insert(4,sec);
}
else
{
time_s.insert(1,min); // 0:00
time_s.insert(time_s.begin()+ 2,':');
time_s.insert(4,sec);
}
}
else
{
ss.precision(2); ss <时间; // 00.00
ss>> time_s;
}
}
else
{
ss.precision(3);
ss<<时间; // 0.000
ss>> time_s;
}
mvinstr(time_loc_y,time_loc_x,(char *)time_s.c_str());
refresh();
}; //功能结束


解决方案

还不够去在这里,但我猜测的是,断言来自 time_s.insert(0,min)调用,以及 if( (int)time> = 600) - 字符串正在执行一些重新分配,并且堆已被破坏(可能是通过在函数顶部的循环中发生的任何事情,但也可能在其他地方完全)



如果你在一个调试器中运行这个命令,并且抓住这个断言,调用堆栈是什么样的?



发生了什么:

  for(int r = 0; r< nrows; r ++){move(r, 0); 
instr((char *)_ contents [r] .c_str());}


Hey so i'm using the pdCurses lib and stringStream to calculate and make a 5 character long string that represents a clock. It shows like 00:00, 0:00, 00.00, or 0.000. However when running my function i get an exeption thrown at this part:

if((int)time >= 10)
{
    if((int)time >= 60)
    {
        if((int)time >= 600)
        {

The exception points to this also Saying there is an access violation:

/* verify block type */
            _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

Why is this happening with something as simple as an if statement? and how can i fix it? Thanks for any help! =) Also here's my function:

////////////////////////////////////////////    Refresh     ///////////////////////////
void Refresh()
{
      for(int r = 0;r<nrows;r++)
      {
          move(r,0);
          instr((char*)_contents[r].c_str());
      }   // make sure this works later

      // Insert the current time;
      enum{ time_loc_y= 24, time_loc_x= 10 };
      long float time = myStopwatch.ElapsedTime();
      string time_s= "     "; 
      string min;  ss << (int)time%60; ss >> min;
      string sec;  ss << (int)time/60; ss >> sec;

      if((int)time >= 10)
      {
          if((int)time >= 60)
          {
              if((int)time >= 600)
              {
                  time_s.insert(0, min); // 00:00
                  time_s.insert(time_s.begin()+2, ':');
                  time_s.insert(4, sec);
              }
              else
              {
                  time_s.insert(1, min); //  0:00
                  time_s.insert(time_s.begin()+2, ':');
                  time_s.insert(4, sec);
              }
          }
          else
          {
              ss.precision(2); ss << time; //  00.00
              ss >> time_s;
          }
      }
      else
      {
          ss.precision(3);
          ss << time; //  0.000
          ss >> time_s;
      }
      mvinstr(time_loc_y, time_loc_x, (char*)time_s.c_str());
      refresh();
 }; // end of function

解决方案

There's not really enough to go on here, but my guess is that the assertion is from the time_s.insert(0, min) call that's in the line along with the if((int)time >= 600) - the string is performing some reallocation and the heap has been corrupted (maybe by whatever is happening in the loop at the top of the function, but maybe somewhere else entirely).

If you run this in a debugger and have it catch the assertion, what does the call stack look like?

What's happening in:

for(int r = 0;r<nrows;r++){ move(r,0); 
                                instr((char*)_contents[r].c_str());} 

这篇关于比较一个(int)double和(int)int时的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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