Visual Studio 2008 中的 System.AccessViolationException

发布于 2024-12-03 12:37:24 字数 802 浏览 4 评论 0原文

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2024-12-10 12:37:24

它崩溃是因为您使用了未初始化的变量:

  int       sizes, sizek;
  struct stat files, filek;
  ofstream ofs;

  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);

如果 stat() 失败,您将使用未初始化的 sizek。
根据未初始化的内存,您的下一条语句将崩溃:

int *s=new int[sizes];

因为 size 可能为负数或非常大的数字,并且新的语句将失败。

检查 stat() 返回的错误,尽管可能没有找到文件 key.pc.db ,导致函数失败。

It's crashing because you're using uninitialized variables:

  int       sizes, sizek;
  struct stat files, filek;
  ofstream ofs;

  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);

if stat() fails, you use an uninitialized sizek.
Depending on the uninitialized memory, your next statement will crash:

int *s=new int[sizes];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文