- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
每次我运行该程序时,都会弹出这个神秘的错误,提示我遇到了某种类型的状态访问冲突。我试过用谷歌搜索它,结果是我可能正在尝试访问一些不允许的内存。非常感谢您的帮助!
这是我运行程序时的错误:
2 [main] a 5772 exception::handle: Exception: STATUS_ACCESS_VIOLATION
1532 [main] a 5772 open_stackdumpfile: Dumping stack trace to a.exe.stackdump
这是 .cpp 代码:
int main()
{
Stack s; //variable declarations
StackItem *newItem;
char token, nextChar, prevChar, response, check;
int lineCount, apostCount;
char filename[50];
bool insideComment = false, insideString = false, error, isMatch, delimError = false;
fstream sourceFile;
do //do while response is 'y'
{
do //do while opening the source file fails
{
cout << "Enter filename of source file: ";
cin.getline (filename,51);
sourceFile.open(filename); //opens the file with given filename
if (sourceFile.fail())
cout << "File could not be opened" << endl; //error if can't open
sourceFile.clear();
}
while (sourceFile.fail()); //exits if source file doesn't fail
sourceFile.clear();
lineCount = 0; //initializes line count to zero
while (!sourceFile.eof()) //exits if end of source file is reached
{
sourceFile.get(nextChar); //gets the next character in file
if ((int)nextChar == '\n') //if next character is an end line
{
lineCount++; //increments line count
cout << "Line count: " << lineCount << endl; //echoes line count
}
else if (nextChar == '{' || nextChar == '[' || nextChar == '(')
{ //continues if next char is an opening token
newItem->token = nextChar; //sets newItem's token
newItem->lineNumber = lineCount; //sets newItem's line count
if (!s.isFull()) //continues if stack isn't full
s.push(newItem); //pushes newItem onto the stack
s.displayStack(); //displays the stack
}
else if (nextChar == '}' || nextChar == ']' || nextChar == ')')
{ //continues if next char is a closing token
if (!s.isEmpty()) //continues if stack isn't empty
{
isMatch = s.matchStack(nextChar); //checks if token matches top
if (isMatch == true) //if true, pops the top of the stack
s.pop();
else //continues if match is false
{
newItem->token = nextChar; //sets newItem's token
newItem->lineNumber = lineCount; //sets newITem's line count
s.trailingItem(newItem); //calls the trailingItem function
cout << endl << endl << "Process another file? (y/n): ";
cin >> response; //asks user whether to continue
while (response != 'y' && response != 'n') //checks for input error
{
cout << "Error! Must enter either y for yes or n for no." << endl;
cout << endl << endl << "Process another file? (y/n): ";
cin >> response;
}
break; //leaves the while loop
}
}
else if (s.isEmpty()) //continues if stack is empty
{
newItem->token = nextChar; //sets newItem's token
newItem->lineNumber = lineCount; //sets newItem's line count
s.push(newItem); //pushes newItem onto the top of the stack
s.processStack(); //processes the error
cout << endl << endl << "Process another file? (y/n): ";
cin >> response; //asks user whether to continue
while (response != 'y' && response != 'n') //checks for input error
{
cout << "Error! Must enter either y for yes or n for no." << endl;
cout << endl << endl << "Process another file? (y/n): ";
cin >> response; //gets user input
}
break; //leaves the while loop
}
s.displayStack(); //displays the items on the stack
}
else if (nextChar == '/') //continues if next char is a '/'
{
sourceFile.get(nextChar); //gets the next character in file
if (nextChar == '*') //continues if next char is a '*'
{ //at this point a comment has started because of '/' then '*'
insideComment = true; //sets bool to true
newItem->token = 'c'; //sets newItem's token to c for "comment"
newItem->lineNumber = lineCount; //sets newItem's line count
s.push(newItem); //pushes the item onto the stack
while (insideComment == true && !sourceFile.eof())
{ //continues while inside a comment and end of file isn't reached
sourceFile.get(nextChar); //gets the next character in file
if (nextChar == '*') //continues if next char is *
{
sourceFile.get(nextChar); //gets the next character in file
if (nextChar == '/') //continues if next char is '/'
{ //at this point the comment has ended because of '*' then '/'
insideComment = false; //sets bool to false
s.pop(); //pops off the c in the stack
}
}
}
}
else if (nextChar == '/') //continues if next char is a '/'
{ //at this point a comment has started because of / then another '/'
insideComment = true; //sets bool to true
while (insideComment == true && !sourceFile.eof())
{ //continues while inside a comment and not at end of file
sourceFile.get(nextChar); //gets the next character in file
if (nextChar == '\n') //leaves comment if next line is entered
insideComment = false; //sets bool to false
}
}
}
else if (nextChar == '\n') //continues if next char is an end line
{
lineCount++; //increments line count
cout << "Line count: " << lineCount << endl; //echoes line count
}
else if (nextChar == '\"' && insideComment == false)
{ //continues if next char is a " and is not currently inside a comment
insideString = true; //sets bool to true b/c " starts a string
while (insideString == true && !sourceFile.eof())
{
sourceFile.get(nextChar); //gets the next character in file
if (nextChar == '\"')
insideString = false;
else if (nextChar == '\n')
{
lineCount++; //increments line count
cout << "Line count: " << lineCount << endl; //echoes line count
}
}
}
else if (nextChar == '\'' && insideComment == false && insideString == false)
{ //continues if next char is a ' and not in a comment or string
apostCount = 1; //initializes apostrophe count (chars after first apostrophe)
while (nextChar != '\'' && !sourceFile.eof())
{ //continues while next char isn't another ' and not end of file
sourceFile.get(nextChar); //gets the next character in file
prevChar = nextChar;
apostCount++; //increments apostrophe count
if (nextChar == '\'' && prevChar == '\\' && apostCount <= 3)
nextChar = ' '; //sets next char to space if 3 or more apostrophes
else if (nextChar == '\n') //continues if next char is an end line
{
lineCount++; //increments line count
cout << "Line count: " << lineCount << endl; //echoes line count
}
}
if (apostCount >= 4) //if more than 3 chars come after apostrophe
{ //displays delimeter error
cout << "Character delimeter error on line " << lineCount << endl;
delimError = true;
}
if (delimError == true) //continues if delimiter error occured
break; //leaves 2nd while loop
}
} //end of while loop
if (s.isEmpty() == true) //continues if no errors within stack
{ //displays successful nesting structure to user
cout << "Source file of " << lineCount << " lines has proper nesting structure";
cout << endl << endl << "Process another file? (y/n): "; //asks to continue
cin >> response; //gets user's response
while (response != 'y' && response != 'n') //checks for input error
{
cout << "Error! Must enter either y for yes or n for no." << endl;
cout << endl << endl << "Process another file? (y/n): ";
cin >> response; //gets user input
}
}
else //continues if errors are present
{
s.processStack(); //processes and displays the delimiter errors
cout << endl << endl << "Process another file? (y/n): "; //asks to continue
cin >> response; //gets user's response
while (response != 'y' && response != 'n') //checks for input error
{
cout << "Error! Must enter either y for yes or n for no." << endl;
cout << endl << endl << "Process another file? (y/n): ";
cin >> response; //gets user input
}
}
}
while (response == 'y'); //opens another file if user entered y for yes
return 0; //quits program
}
这是 .h 代码:
struct StackItem //defines StackItem structure
{
char token; //holds delimeter
int lineNumber; //holds the line number of the delimeter in the source file
StackItem *next; //points to the next structure
};
typedef StackItem* nodePtr;
const int MAX_ITEMS = 99;
class Stack //Stack class definition
{
private:
nodePtr top; //points to the top of the stack
int numberItems; //number of items in the stack
public:
Stack() //initializes private variables
{ top = NULL; numberItems = 0; }
~Stack()
{ while (numberItems > 0)
{ pop();
}
}
bool isEmpty() //returns whether the stack is empty or not
{ return top == NULL; }
bool isFull() //returns whether the stack is full or not
{ return MAX_ITEMS == numberItems;}
void push(nodePtr newItem) //pushes a new item onto the top of the stack
{
newItem->next = top;
top = newItem;
numberItems++;
}
void pop() //removes the item on top of the stack
{
nodePtr p = top; //sets pointer p to what top points to
top = top->next; //top is set to the next struct in stack
delete p; //deletes the top item in the stack
numberItems--;
}
void displayStack() //displays all of the items on the stack
{
nodePtr p = top; //sets pointer p to what top points to
cout << "Stack Contents: " << endl;
while(p != NULL) //while loop to display the stack contents
{
cout << p->token << " on line " << p->lineNumber << "." << endl;
p = p->next;
}
}
void processStack() //displays the error for each delimeter in stack
{
nodePtr p = top; //sets pointer p to what top points to
while (p != NULL) //continues until p points to NULL
{
cout << "\" " << p->token << " \" on line " << setw(5) << right;
cout << p->lineNumber << " has no matching element" << endl;
p = p->next;
}
}
//determines whether the stack delimeter matches the current character
bool matchStack(char character)
{
bool match = false;
if (top->token == '{')
{
if (character == '}')
match = true; //sets bool to true if we have { and }
}
else if (top->token == '[')
{
if (character == ']')
match = true; //sets bool to true if we have [ and ]
}
else if (top->token == '(')
{
if (character == ')')
match = true; //sets bool to true if we have ( and )
}
return match; //returns the bool
}
//called if we have a trailing item that doesn't match stack delimeter
void trailingItem(nodePtr newItem) //displays what doesn't match
{
cout << "\" " << top->token << " \" on line " << setw(5) << right;
cout << top->lineNumber << " doesn't match \" " << newItem->token;
cout << " \" on line " << setw(5) << right << newItem->lineNumber;
}
};
这是 a.exe.stackdump 文件:
Exception: STATUS_ACCESS_VIOLATION at eip=00401701
eax=61100049 ebx=0028CD40 ecx=00A01998 edx=00000000
esi=0028CD40 edi=00000000 ebp=0028CD18 esp=0028CAF0
program=C:\Users\Tyler\Desktop\Program 2\a.exe, pid 5772, thread main
cs=0023 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame Function Args
0028CD18 00401701 (00000001, 009E8628, 009E8510, 00000001)
0028CD68 61007038 (00000000, 0028CDA4, 61006980, 7EFDE000)
End of stack trace
最佳答案
您声明 StackItem *newItem;
这是一个指针,但您从未将其初始化为任何内容,因此其内容未定义。然后,一旦您执行 newItem->...
,您的程序就会崩溃,因为它没有指向任何有效内存。
解决方案是在各处添加一些 newItem = new StackItem
以创建实际对象。当然,StackItem
类的一个(或多个)构造函数将是最受欢迎的。
关于程序运行时出现 C++ STATUS_ACCESS_VIOLATION 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9796200/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!