比较 (int)double 和 (int)int 时出现异常

发布于 2024-11-07 18:18:35 字数 1906 浏览 1 评论 0原文

嘿,我正在使用 pdCurses lib 和 stringStream 来计算并制作一个代表时钟的 5 个字符长的字符串。它显示为 00:00、0:00、00.00 或 0.000。然而,当运行我的函数时,我在这部分抛出一个异常:

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

异常也指出存在访问冲突:

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

为什么像 if 语句这样简单的事情会发生这种情况?我该如何解决它? 感谢您的帮助! =) 这也是我的功能:

////////////////////////////////////////////    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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2024-11-14 18:18:35

这里还没有足够的内容,但我的猜测是断言来自 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());} 

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());} 
扎心 2024-11-14 18:18:35

这是一个内存系统断言,通常是在堆已损坏时通过分配内存来触发的。触发因素可能是这样的:

time_s.insert(0, min); 

然而,问题出在其他地方——你不应该覆盖内存的地方。

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.

夜访吸血鬼 2024-11-14 18:18:35

我认为您对抛出异常的原因是错误的。 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 {

每行一个语句),异常将更准确地告诉您哪个语句导致了问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文