gpt4 book ai didi

c - 抛出异常 : read access violation. 无法打印头文件中的信息

转载 作者:行者123 更新时间:2023-11-30 20:32:31 25 4
gpt4 key购买 nike

我正在尝试打印位于 Objects.h 头文件中的 pID 指针的信息。无论我尝试做什么,我都会收到“抛出异常:读取访问冲突。p 为 0xCEDECEDF”。 Visual Studio 2017 中出现错误。换句话说,它无法读取内存。这是 main.cpp 文件中的代码部分。该错误来 self 试图打印信息的 For 循环。我调试了代码没有成功。

#include "stdio.h"
#include "DateTime.h"
#include "Objects.h"
#include "Headers.h"
#include "Structs.h"
#include "malloc.h"
#pragma warning ( disable : 4996 ) //Disables some errors that might occure

void PrintObjects(Object9 **ppStruct1);
//int InsertNewObject(HeaderA **ppStruct1, char *pNewID, int NewCode);
//Object9* RemoveExistingObject(HeaderA **pStruct1, char *pExistingID);

int main()
{
Object9 **pStruct1 = (Object9 **)GetStruct1(9, 35);

Object9 **p = pStruct1;
int i;
for (i = 0; i < 35; i++)
{
printf("%p\n", *(p + i));
printf("%s\n", (*(p + i))->pID); //This is the line where the error
//occures
}

if (pStruct1 == NULL)
printf("pStrcut on NULL\n");
else
printf("Ok\n");
PrintObjects(pStruct1);

return 0;
}

这是 Objects.h 文件中的部分。

typedef struct ob9
{ // formatting string for printf is "%s %lu %02d %s %04d\n", the result is
for example "Abcde 100 01 Detsember 2010"
// or "Abcde Fghij 100 01 Detsember 2010"
char *pID;
unsigned long int Code;
Date3 *pDate3; // Declaration of Date3 is in file DateTime.h
struct ob9 *pNext;
} Object9;

GetStruct1 函数来自 Structure.h 头文件。我不知道这是否有助于确定问题,但以防万一我还添加了整个 Structs.h 文件代码。我需要使用:“void **GetStruct1(int ObjectType, int nObjects);”功能。

//
// Structures: function prototypes
// -------------------------------

void **GetStruct1(int ObjectType, int nObjects);
HeaderA *GetStruct2(int ObjectType, int nObjects);
HeaderB *GetStruct3(int ObjectType, int nObjects);
HeaderC *GetStruct4(int ObjectType, int nObjects);
HeaderA **GetStruct5(int ObjectType, int nObjects);
HeaderA *GetStruct6(int ObjectType, int nObjects);
HeaderD *GetStruct7(int ObjectType, int nObjects);

/* ObjectType - 1, 2,...10 - type of objects to be inserted into the data
structure.
nTypes -number of objects to be inserted into the data structure.
ObjectType and nObjects are specified by the instructor.
Return value - pointer to the first byte of data structure.

Examples for structure from 25 objects of type 4:
Object4 **pStruct1 = (Object4 **)GetStruct1(4, 25);
HeaderA *pStruct2 = GetStruct2(4, 25);
HeaderB *pStruct3 = GetStruct3(4, 25);
HeaderC *pStruct4 = GetStruct4(4, 25);
HeaderA **pStruct5 = GetStruct5(4, 25);
*/

我可以打印出地址,但不能打印出我需要的信息。我知道我做错了什么,但不知道到底是什么。任何帮助将不胜感激。

编辑:稍微更改了代码。

最佳答案

问题在于您正在使用 malloc() 中的新指针覆盖 GetStruct1 返回的指针,并且该指针指向未初始化的数据,而不是结构数组。去掉这条线:

pStruct1 = (Object9 **)malloc(sizeof(Object9 **));

您也不需要这一行:

p = (Object9 *)malloc(sizeof(Object9));

因为您立即用以下内容覆盖p:

p = *pStruct1;

您也使用了错误的ppStruct1 是一个指向指针数组的指针。当您按照上面的方式设置 p 时,它会被设置为数组中的第一个指针。添加到 p 不会获得数组中的下一个指针,它指向 p 指向的对象之外。您需要将 p 设置为 pStruct,添加到此,然后间接通过它来获取数组中的指针。

Object9 **p = pStruct1;
for (int i = 0; i < 35; i++) }
printf("%p\n", *(p + i));
printf("%s\n", (*(p + i))->pID);
}

关于c - 抛出异常 : read access violation. 无法打印头文件中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47315733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com