c++ - 错误 0xC0000005 : Access Violation when returning from a function C++

标签 c++

我是这门语言的初学者,遇到过这个问题。在网上搜索后,它似乎与内存分配、指针等有关。我不太明白……在这里,我试图通过类将数据添加到二进制文件中…… 所以想请教一下是什么问题-->

void addques()
{
    question abc;
    ofstream fout;
    fout.open("question.txt",ios::app|ios::binary);
    cout<<"Enter Question!\n";
    gets(abc.ques);
    cout<<"Enter Options!\n";
    gets(abc.option1);gets(abc.option2);gets(abc.option3);gets(abc.option4);
    cout<<"Enter correct option number\n";
    cin>>abc.k;
    cout<<"Enter question nummber"; cin>>abc.quesno;
    fout.write((char*)&abc,sizeof(abc));
    fout.close();
    cout<<"File closed";
}

似乎所有具有 ifstream/ofstream 类对象的函数都显示此错误。最后一行“文件关闭”也正在执行,之后出现错误。 难道是这一行-- fout.write((char*)&abc,sizeof(abc)); ??? 请帮忙

这是相关的类-->

class question
{   public:
    char ques[80];
    char option1[50], option2[50], option3[50], option4[50];
    char k;
    char quesno;
};

对于整个程序,我在这里粘贴了我的代码 http://pastebin.com/S7KNby0E 请看到它...因为我无法在这里这样做

最佳答案

您输入的问题或答案是否对您的缓冲区之一来说太长了?我敢打赌你是,它超出了类的范围并破坏了堆栈。另外,混合使用 cin 和 cout 以及像 gets 这样的 C 风格 IO 函数是自找麻烦,所以不要这样做。

由于您使用的是 C++,因此不必像字符数组那样进行字符串操作。有一个 STL 类可以为您处理所有内存垃圾。我将按以下方式重写您的类(class):

class Question
{   public:
    string ques;
    string option1, option2, option3, option4;
    char k;
    char quesno;

    void write(fstream& f)
    {
        f << ques.length() << " " << ques << endl
          << option1.length() << " " << option1 << endl
          << option2.length() << " " << option2 << endl
          << option3.length() << " " << option3 << endl
          << option4.length() << " " << option4 << endl
          << k << " " << quesno << endl;
    }
};

和你的功能如下:

void addques()
{
    Question abc;
    ofstream fout;
    fout.open("question.txt", ios::app);

    cout << "Enter Question!" << endl;
    getline (cin, abc.ques);

    cout << "Enter Options!\n";
    getline(cin, abc.option1);
    getline(cin, abc.option2);
    getline(cin, abc.option3);
    getline(cin, abc.option4);

    cout << "Enter correct option number: ";
    cin >> abc.k;

    cout << "Enter question number: ";
    cin >> abc.quesno;

    // you will have to change your writing method a bit because you can't just write the string object straight to disk like you were before

    abc.write(fout);
    fout.close();
}

然后您应该能够使用提取运算符以与写入工作大致相同的方式将一个读入流中。

Ascii 转二进制

由于必须使用二进制,因此可以通过以下方式将整数值存储为二进制值:

int i = ques.length();
fout.write((const char *) &i, sizeof(i));

这会将 32 位整数值直接写入流,而无需先将其转换为字符串。您的字符串将采用以下格式:

+     0x0     0x1     0x2     0x3     0x4     0x5     0x6     0x7     
0x0  [0x00    0x00    0x00    0xC0  ][H       E       L       L
0x8   O       <space> W       O       R       L       D       <null> ]

长度是前 4 个字节,此处显示为 0x0000000C(整数值 12)。字符串紧随其后,其值为“HELLO WORLD\0”。\0 是空终止符。在我的示例中,此长度包括空终止符。

尺寸

Sizeof 是一个运算符,它在编译器可以确定的范围内生成指定类型的内存大小。对于整数类型,如 int、short、char 等,它将返回该类型使用的字节数。对于数组,您可能会遇到令人困惑的行为。如果在静态声明为固定大小的数组上调用,sizeof 将返回数组的长度 * 一个元素的大小。

int derp[1000];
sizeof(derp); // sizeof(int) * 1000

如果编译器不知道数组有多大,您将得到的是指向第一个元素的指针的大小。所以要小心。您不能对指针使用 sizeof 来确定数组大小。

int derp2[];
sizeof(derp2); // sizeof(int *), probably 4 or 8
int * derp3 = derp;
sizeof(derp3); // sizeof(int *), probably 4 or 8

要获取 std::string(STL 字符串类)的长度,请使用长度成员:

string hurr = "hello world";
hurr.length(); // does NOT include the null terminator
               // length of string AND terminator is hurr.length() + 1

关于c++ - 错误 0xC0000005 : Access Violation when returning from a function C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801321/

相关文章:

c++ - 在 C++ 中的 SQL Server 上运行选择查询的最快方法是什么?

c++ - 是否有计算 vector 中所有正数的函数?

c++ - 如何计算以下函数的时间复杂度?

c++ - 强制 GCC 不要优化掉一个未使用的变量?

c++ - 我应该使用 operator+= 而不是 operator+ 来连接 std::string 吗?

c++ - 从 map-C++ 中检索字符串时出现逻辑错误

c++ - 等待条件变量加载 CPU 内核吗?

带有sql连接器的C++无需请求太多数据即可获取元数据

c++ - Qt Gui 应用程序中的 2 个独立标准线程

c++ - 声明变量时逗号分隔的作用