比较 (int)double 和 (int)int 时出现异常
嘿,我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里还没有足够的内容,但我的猜测是断言来自
time_s.insert(0, min)
调用,该调用与if((int) 一起位于行中time >= 600)
- 字符串正在执行一些重新分配,并且堆已损坏(可能是由于函数顶部循环中发生的任何事情,但也可能完全是在其他地方)。如果您在调试器中运行它并让它捕获断言,那么调用堆栈会是什么样子?
发生了什么:
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 theif((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:
这是一个内存系统断言,通常是在堆已损坏时通过分配内存来触发的。触发因素可能是这样的:
然而,问题出在其他地方——你不应该覆盖内存的地方。
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:
However, the problem is somewhere else--somewhere where you are overwriting memory that you shouldn't.
我认为您对抛出异常的原因是错误的。
ss
在哪里定义的?注意,如果您将代码重新格式化为更规范的样式(例如
每行一个语句),异常将更准确地告诉您哪个语句导致了问题。
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
with one statement per line, the exception will tell you much more closely which statement caused the problem.