Visual Studio 2008 中的 System.AccessViolationException
// diskbin.cpp : main project file.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <sys/stat.h>
using namespace std;
int main( int argc, char *argv[] )
{
//code
if(stat("key.pc.db", &filek) ==0 )
sizek=filek.st_size;
if(stat("seek.pc.db", &files) ==0 )
sizes=files.st_size;
sizek=sizek/sizeof(int);
sizes=sizes/sizeof(int);
int i,min,max,mid;
int *s=new int[sizes];
int *hit=new int[sizes];
//code
}
当我在 Visual Studio 2008 中运行这个程序时,我没有收到任何错误,但是当我运行 cmd 时,它会打开然后关闭,然后会弹出一个窗口,其中显示: “diskbin.exe 中发生了类型为‘System.AccessViolationException’的未处理异常 附加信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”可能是什么问题?我是否没有分配 s 并正确命中?
谢谢!
// diskbin.cpp : main project file.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <sys/stat.h>
using namespace std;
int main( int argc, char *argv[] )
{
//code
if(stat("key.pc.db", &filek) ==0 )
sizek=filek.st_size;
if(stat("seek.pc.db", &files) ==0 )
sizes=files.st_size;
sizek=sizek/sizeof(int);
sizes=sizes/sizeof(int);
int i,min,max,mid;
int *s=new int[sizes];
int *hit=new int[sizes];
//code
}
When I run this program in Visual Studio 2008, I am not getting any error but when I run the cmd opens and then closes followed by a pop up window which says:
"An unhandled exception of type 'System.AccessViolationException' occurred in diskbin.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." What could be the issue? Have I not allocated s and hit properly?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它崩溃是因为您使用了未初始化的变量:
如果 stat() 失败,您将使用未初始化的 sizek。
根据未初始化的内存,您的下一条语句将崩溃:
因为
size
可能为负数或非常大的数字,并且新的语句将失败。检查 stat() 返回的错误,尽管可能没有找到文件
key.pc.db
,导致函数失败。It's crashing because you're using uninitialized variables:
if stat() fails, you use an uninitialized sizek.
Depending on the uninitialized memory, your next statement will crash:
because
sizes
can be negative or a very large number and the new will fail.Check the error returned by stat(), although it's possible the file
key.pc.db
is not found, causing the function to fail.