作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我仍在研究我的 Field 类,并试图提高我糟糕的插入/删除性能。
但是,新功能只工作一次,当我第二次使用它时就会发生灾难性的故障。
这是代码:
template <class T>
T *Field<T>::insert(const T *pPos, const T& data)
{
// Special case: field is empty. insert should still succeed.
// Special case: Pointing to one past the end. insert should still succeed
if( empty() || pPos == last() )
{
this->push_back(data);
return (this->last() - 1);
}
/* Explanation: Find cell before which to insert new value. Push_back new
new value, then keep swapping cells until reaching *pPos and swapping it
with data. The while fails, we exit, insert successful. */
T *p = ( std::find( this->first(), this->last(), *pPos ));
if( p != last() )
{
this->push_back(data);
T *right = (this->last() - 1);
T *left = (this->last() - 2);
while( *pPos != data )
std::iter_swap( left--, right-- );
// pPos *has* to be the destination of new value, so we simply return param.
return const_cast<T*>(pPos);
}
else
throw std::range_error("Not found");
}
从main
调用代码
// Field already has push_back()ed values 10, 20, 30.
field->insert( &(*field)[2], 25 ); // field is a ptr (no reason, just bad choice)
在控制台上打印时产生此输出。
Field: 10 20 30 // Original Field
25 // Function return value
Field: 10 20 25 30 // Correct insertion.
来自 main
的新调用代码
// Field already has push_back()ed values 10, 20, 30
field->insert( &(*field)[2], 25 );
field->insert( &(*field)[3], 35 );
在控制台上打印时产生此输出。
Field: 10 20 30
25
35
-4.2201...e+37, 10, 15, 20, 30
Windows has triggered a breakpoint in Pg_1.exe.
This may be due to a corruption in the heap (oh shit).
No symbols are loaded for any call stack frame.
The source code cannot be displayed.
然后控制台将不再关闭,直到我关闭 VSC++08 本身。
什么?为什么?如何?我的代码在做什么!?
附加信息
字段在推送前大小为 3,容量为 4。两次插入后,Field 的容量正确增加到 8(翻倍),并存储了五个元素。
使用 insert() 将第二个元素插入何处并不重要,它会以完全相同的方式失败。相同的输出,甚至相同的数字(我认为)在第一个单元格。
附加代码
向后推()
注意:此代码在我的重构过程中没有更改。此功能一直有效,所以我非常怀疑这将是问题的原因。
/* FieldImpl takes care of memory management. it stores the values v_, vused_,
and vsize_. Cells in the Field are only constructed when needed through a
placement new that is done through a helper function. */
template <class T>
void Field<T>::push_back(const T& data)
{
if( impl_.vsize_ == impl_.vused_ )
{
Field temp( (impl_.vsize_ == 0) ? 1
: (impl_.vsize_ * 2) );
while( temp.impl_.vused_ != this->impl_.vused_ )
temp.push_back( this->impl_.v_[temp.size()] );
temp.push_back(data);
impl_.Swap(temp.impl_);
}
else
{
// T *last() const { return &impl_.v_[impl_.vused_]; }
// Returns pointer to one past the last constructed block.
// variant: T *last() const { return impl_.v_; }
Helpers::construct( last(), data );
++impl_.vused_;
}
}
最佳答案
// ...
if( p != last() )
{
this->push_back(data);
在这一行之后 pPos
可能不再是一个有效的指针。
The console then proceeds to never shutdown again until I close VSC++08 itself.
尝试过单击调试器中的“停止”按钮?
关于c++ - 功能代码在使用两次时中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3912672/
安装并修复我的 VS2015 实例后,我仍然无法让智能感知(服务器端)在我的 MVC View 中工作。当我在 session 中第一次打开 .cshtml 文件并找到 Activitylog 文件时
我是一名优秀的程序员,十分优秀!