当我的堆栈加倍时,它会给我临界区错误!_ctrlvalidHeappointer
when doubling my stack it give me critical section error ! _ctrlvalidHeappointer
我写了一个类Stack
,它的两边都被使用,即两个堆栈在一起。一个从[0]
到[someplace - 1]
,一个从[capacity-1]
到[someplace +1]
。
一切都很好,但我有一些问题,加倍内存当我的数组是满的。我的代码一开始可以翻倍,但是当它需要再翻倍时,它给了我一些奇怪的错误。
_ctrlvalidHeappointer
临界区错误
这是我的代码。代码中也有一些解释。当我在堆栈中压入太多元素时,它会失败。
string firstname = "asasasasasaasasasasassasasasaasas";
string secondname= "asasdasfregeasasasasasgergergererg";
for (int i = 0; i < firstname.length(); i++)
{
a.push_at_first(firstname.at(i));
}
for (int i = 0; i < secondname.length(); i++)
{
a.push_from_end(secondname.at(i));
}
这是我的班
using namespace std;
template <class T>
class Stack{
public:
Stack();
~Stack();
Stack(const Stack<T>& ob);
void double_size();
void push_at_first(T mydata);
void push_from_end(T mydata);
T & operator = (Stack<T> ob);
private:
int top;
int top2;
T * stack;
int capacity;
};
template <class T>
T& Stack<T>::operator = (Stack<T> ob)
{
if(capacity == ob.capacity){
top = ob.top;
top2 = ob.top2;
for (int i = 0; i < capacity; i++)
{
stack[i] = ob.stack[i];
}
return *this;}
else
{
capacity = ob.capacity;
stack = new T[capacity];
for (int i = 0; i < capacity; i++)
{
stack[i] = ob.stack[i];
}
}
}
template <class T>
Stack<T>::Stack (const Stack<T>& ob) :capacity(ob.capacity)
{
stack = new T[capacity];
top = ob.top;
top2=ob.top2;
for (int i = 0; i < capacity; i++)
{
stack[i] = ob.stack[i];
}
}
template <class T>
Stack<T>::~Stack()
{
delete [] stack;
}
template <class T>
Stack<T>::Stack()
{
capacity = 17;
top = 0;
top2 = capacity-1;
stack = new T[capacity];
}
template <class T>
void Stack<T>::push_at_first(T mydata)
{
if ( (top + 1) == (top2 -1) ) // 1 : because I want to Be a Empty Space between Two Stack so i can tell the difference
double_size();
stack[++top] = mydata;
}
template <class T>
void Stack<T>::push_from_end(T mydata)
{
if( (top + 1) == (top2 -1) ) // 1 : because I want to Be a Empty Space between Two Stack so i can tell the difference
double_size();
stack[--top2] = mydata;
}
template <class T>
void Stack<T>::double_size()
{
Stack<T> temp(*this);
capacity *= 2;
stack = new T[capacity];
top = temp.top;
top2 = capacity - (temp.capacity - temp.top2);// capacity - number of data in stack of temp ;
// if we have something in first stack then copy 0 to top elements of temp.stack to stack
if(top > 0)
{
for (int i = 0; i <= top ; i++)
{
stack[i] = temp.stack[i];
}
}
// There is Something Wrong Down here ! i can't figure out what !
if(top2 < capacity - 1)
{
for (int i = capacity-1; i >= top2; i--)
{
stack[i] = temp.stack[i-(temp.capacity)];
}
}
}
我发现了几个问题。
-
你缺少一个复制构造函数和赋值操作符
-
你显式地调用了元素的析构函数。
当delete
再次执行此操作时,将导致麻烦,因为它会自动执行。 -
您没有在
double_size
中为temp.stack
分配适当的内存量。即使您可以访问temp
的私有成员,您也应该让它管理自己(参见上面的复制构造函数)。(BTW:该变量是不必要的-只需从旧内存块复制到新内存块,然后删除旧块并将新块指针分配给stack
。) -
double_size
的索引也可能有一些问题,但你应该先解决这三个问题。
相关文章:
- 在同步问题中,弱指针可以代替互斥锁或临界区吗?
- 多线程环境下,其他线程永远无法访问临界区
- 如果一个线程在临界区上调用Acquire(),如果另一个线程调用Release(),该锁会被释放吗?
- 是否需要临界区?
- 当我的堆栈加倍时,它会给我临界区错误!_ctrlvalidHeappointer
- 确定Windows线程是否处于临界区或类似的位置
- 这段代码是一个工作的临界区包装器类吗?
- 临界区通过constexpr
- 临界区队列
- 出乎意料的性能提升与OpenMP临界区
- 如何使用Boost创建临界区?
- 临界区最简单的实现
- 由两个线程访问的共享队列的临界区代码是什么?
- c++ 11有临界区吗?
- 如何避免临界区和SendMessage之间的死锁
- Windows临界区公平性
- 线程同步-临界区或互斥锁
- 是否寄存器读/写需要临界区保护
- 如何在两个类之间共享单个临界区
- 什么是临界区