Exception when comparing an (int)double and (int)i

2019-03-04 06:11发布

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

3条回答
乱世女痞
2楼-- · 2019-03-04 06:36

I think you're mistaken about what's throwing the exception. Where is ss defined?

NB if you reformat your code to a more canonical style, like

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 {

with one statement per line, the exception will tell you much more closely which statement caused the problem.

查看更多
走好不送
3楼-- · 2019-03-04 06:38

This is a memory system assertion, it usually is triggered by allocating memory when the heap has already been corrupted. The trigger was probably this:

time_s.insert(0, min); 

However, the problem is somewhere else--somewhere where you are overwriting memory that you shouldn't.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-04 06:42

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());} 
查看更多
登录 后发表回答