为什么我会收到访问冲突运行时错误?

发布于 2024-12-28 06:12:14 字数 873 浏览 1 评论 0原文

我有这个结构

typedef struct fpinfo
{
    unsigned long chunk_offset;
    unsigned long chunk_length;
    unsigned char *fing_print;
}fpinfo;

typedef struct Hash_Entry {
    struct Hash_Entry *next;  /* Link entries within same bucket. */
    unsigned namehash;        /* hash value of key */
    struct fpinfo fp;
} Hash_Entry;

和下面的代码行从 fing_print 数组中提取 10 msb

unsigned int h;
h = (he.fp.fing_print[0] << 2 | (he.fp.fing_print[1] & 0xC0) >> 6) & 0x3FF;

以下是我如何通过从文件

while(fscanf(rd,"%ul,%ul,%X",&test_st.fp.chunk_offset,&test_st.fp.chunk_length,&test_st.fp.fing_print) !=EOF)
{   
    ....
}

vc 2010 中读取内容来初始化 he 数据成员,给出错误:

htable 中 0x013217f8 处出现未处理的异常。 exe: 0xC0000005: 读取位置 0xcccccccc 时发生访问冲突。

有什么问题吗?

I have this structure

typedef struct fpinfo
{
    unsigned long chunk_offset;
    unsigned long chunk_length;
    unsigned char *fing_print;
}fpinfo;

typedef struct Hash_Entry {
    struct Hash_Entry *next;  /* Link entries within same bucket. */
    unsigned namehash;        /* hash value of key */
    struct fpinfo fp;
} Hash_Entry;

and the following line of code to extract the 10 msb from the fing_print array

unsigned int h;
h = (he.fp.fing_print[0] << 2 | (he.fp.fing_print[1] & 0xC0) >> 6) & 0x3FF;

Here is how I initialized the he data member by reading contents from a file

while(fscanf(rd,"%ul,%ul,%X",&test_st.fp.chunk_offset,&test_st.fp.chunk_length,&test_st.fp.fing_print) !=EOF)
{   
    ....
}

vc 2010 gives the error:

Unhandled exception at 0x013217f8 in htable.exe: 0xC0000005: Access violation reading location 0xcccccccc.

what is wrong with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

失退 2025-01-04 06:12:14

最可能的原因是 he.fp.fing_print 尚未初始化,因此您的进程在尝试访问其元素时崩溃。要进行验证,请打印出指针的值,或在调试器中检查它。

编辑 fscanf() 代码有两个问题:

  1. 不完全清楚其意图是什么,但 %X一起使用&test_st.fp.fing_print 覆盖指针
  2. 您似乎没有为 test_st.fp.fing_print 分配内存。

The most likely reason is that he.fp.fing_print has not been initialized, so your process crashes when trying to access its elements. To verify, print out the value of the pointer, or examine it in the debugger.

edit There are two problems with the fscanf() code:

  1. It's not totally clear what the intent is, but %X together with &test_st.fp.fing_print overwrites the pointer;
  2. You don't appear to allocate memory for test_st.fp.fing_print.
∞觅青森が 2025-01-04 06:12:14

可能的原因可能是 test_st 未初始化,而您正在使用它来初始化 fp。

possible reason could be that test_st is not initialized and you are using it to initialize fp.

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