C 结构:分段错误

发布于 2024-11-07 05:18:53 字数 447 浏览 0 评论 0原文

关于结构的简单问题:

struct xint {
     int number;
     char string[12];
};

int main(int argc, char *argv[])
{
  struct xint offsets, *poffsets;
  poffsets=&offsets;
  FILE * pFile = fopen("file","rb");
  fread(poffsets,1,16,pFile);
  printf("Number %d\nString %s\n",offsets.number,offsets.string);
}

我得到这个输出

Number 12345
Segmentation fault

,我知道我可能在结构、指针和内存分配方面做错了一些事情。提前致谢 :)

Quick question about structs:

struct xint {
     int number;
     char string[12];
};

int main(int argc, char *argv[])
{
  struct xint offsets, *poffsets;
  poffsets=&offsets;
  FILE * pFile = fopen("file","rb");
  fread(poffsets,1,16,pFile);
  printf("Number %d\nString %s\n",offsets.number,offsets.string);
}

I get this output

Number 12345
Segmentation fault

I know I've probably done something wrong with structures and pointers and memory allocation. Thanks in advance :)

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

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

发布评论

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

评论(4

不语却知心 2024-11-14 05:18:53

您的问题是您直接从文件中读入结构,而不检查结构对齐。改为这样做:

  fread(&offset.number,1,sizeof(offsets.number),pFile);
  fread(&offset.string,1,sizeof(offsets.string),pFile);

Your problem is you're directly reading into a struct from the file, without checking struct alignment. Do this instead:

  fread(&offset.number,1,sizeof(offsets.number),pFile);
  fread(&offset.string,1,sizeof(offsets.string),pFile);
不寐倦长更 2024-11-14 05:18:53

我怀疑您正在读取的文件数据没有以 NUL ('\0') 字符终止字符串。根据 C 标准库的 printf() 所遵循的字符串的 C 定义,字符串必须以 NUL 字符终止。

您可能会始终(通过代码)确保 .string[11] = '\0'

或者,声明 string[13] 并确保 string[12] = '\0'

另外,另一位发帖者提到了结构成员对齐问题。这是您也必须解决的一个合理的担忧。

I suspect that the file data you are reading does not terminate the string with a NUL ('\0') character. By the C definition of strings, which printf() of the C standard library abides, a string must be terminated with a NUL character.

You might be well-off to always (via code) ensure that .string[11] = '\0'.

OR, declare string[13] and ensure that string[12] = '\0'

Also, another poster mentioned struct member alignment concerns. That is a valid concern you must also address.

帝王念 2024-11-14 05:18:53

我猜测该字符串在文件中不是以空值终止的,并且您的代码也没有执行任何以空值终止的操作。

fread(poffsets, 1, 16, pFile);
offsets.string[11] = '\0';
printf("Number %d\nString %s\n", offsets.number, offsets.string);

或者修改文件,使字符串以空字节结尾。

I'm guessing the string is not null-terminated in the file, and your code does nothing to null-terminate the string either.

fread(poffsets, 1, 16, pFile);
offsets.string[11] = '\0';
printf("Number %d\nString %s\n", offsets.number, offsets.string);

Or modify the file so the string ends with a null byte.

初见你 2024-11-14 05:18:53

你会得到缓冲区溢出。您的字符串包含 12 个字符,但没有空间用于终止 '\0'

如果您这样做:

struct xint {
     int number;
     char string[16]; // Make sure you have enough space for the string + '\0'.
};

int main(int argc, char *argv[])
{
    struct xint offsets, *poffsets;

    // Initialize your memory to 0. This will ensure your string is 
    // '\0'-terminated.
    // FYI, sizeof(xint) here is 20.
    memset(&offsets, 0, sizeof(xint)); 

    poffsets=&offsets;
    FILE * pFile = fopen("file","rb");
    fread(poffsets,1,16,pFile);
    fclose(pFile);
    printf("Number %d\nString %s\n",offsets.number,offsets.string);
}

那就可以解决问题。

You get buffer overflow. Your string is made to contain 12 chars, but you don't have space for a terminating '\0'.

If you did:

struct xint {
     int number;
     char string[16]; // Make sure you have enough space for the string + '\0'.
};

int main(int argc, char *argv[])
{
    struct xint offsets, *poffsets;

    // Initialize your memory to 0. This will ensure your string is 
    // '\0'-terminated.
    // FYI, sizeof(xint) here is 20.
    memset(&offsets, 0, sizeof(xint)); 

    poffsets=&offsets;
    FILE * pFile = fopen("file","rb");
    fread(poffsets,1,16,pFile);
    fclose(pFile);
    printf("Number %d\nString %s\n",offsets.number,offsets.string);
}

That would fix the issue.

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