【发布时间】:2016-07-14 09:27:24
【问题描述】:
在 tStack.exe 中的 0x003165F0 处引发异常:0xC0000005:访问冲突读取位置 0x9BFF07EF.?
我似乎无法确定这个程序的问题。我不断在不同的地方收到这样的读/写错误。这是 .cpp 和 .h
.cpp:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include <string>
#include <iostream>
//using namespace std;
tStack::tStack()
{
}
tStack::~tStack()
{
}
tStack::tStack(const tStack &)
{
}
void tStack::Pop()
{
snode *tmp_ptr = NULL;
if (front)
{
tmp_ptr->next = front;
front = tmp_ptr;
free(tmp_ptr);
}
else
std::cout << "\nStack is Empty";
}
void tStack::Push(std::string op)
{
snode *tmp_ptr = front;
tmp_ptr->data = op;
if (front)
{
tmp_ptr->next = front;
front = tmp_ptr;
}
else
{
front = tmp_ptr;
front->next = NULL;
}
}
void tStack::Print()
{
snode *cur_ptr = front;
if (cur_ptr)
{
std::cout << "\nElements in Stack:\n";
while (cur_ptr)
{
std::cout << cur_ptr->data;
cur_ptr = cur_ptr->next;
}
std::cout << "\n";
}
else
std::cout << "\nStack is Empty";
}
void tStack::cStack()
{
free(front);
}
void tStack::convert(std::string postfix, tStack a)
{
int count = 0;
bool lastOper;
std::string pusher, val1, val2;
for (int i = 0; i < postfix.size(); i++)
{
if (isalpha(postfix[i]))
{
pusher = postfix[i];
a.Push(pusher);
count++;
}
else
{
if (count < 2)
{
std::cout << "There are not enough values to perform an operation.";
}
else
{
pusher = postfix[i];
val1 = front->data;
a.Pop();
val2 = front->data;
a.Pop();
a.Push(")");
a.Push(val1);
a.Push(pusher);
a.Push(val2);
a.Push("(");
}
lastOper = true;
}
}
}
.h:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include <string.h>
#include <iostream>
//using namespace std;
class snode
{
public:
std::string data;
snode *next;
};
class tStack
{
public:
tStack();
~tStack();
tStack(const tStack &);
void Pop();
void Push(std::string);
void Print();
void cStack();
void convert(std::string, tStack);
private:
snode *front;
};
我发现一些帖子建议不要使用 using namespace std,但这似乎没有帮助。我只是完全误解了链表的工作原理吗?
【问题讨论】:
-
您的指针数学在某处不正确。您需要调试您的程序以找出位置。
-
谢谢,你能帮我清理一下吗?当我说 front->next = temp_ptr;我的想法是否正确,这意味着 front 的 next 指针值现在指向 temp_ptr 指向的位置?
-
看来其他人已经发现了问题。但是,如果您遇到访问冲突错误,这通常意味着您计算的地址错误,或者您的内存管理不善。还有很抱歉,我已经很久没有处理指针了,我不想让你误入歧途。