当 NUM_INODES 不够大时为什么会出现分段错误?

问题描述 投票:0回答:0

我有一个像这样的 master_file_table:

xxd master_file_table

00000000: 0000 0000 0200 0000 2f00 0100 0000 0000  ......../.......
00000010: 0200 0000 0100 0000 0000 0000 0200 0000  ................
00000020: 0000 0000 0100 0000 0700 0000 6b65 726e  ............kern
00000030: 656c 0000 0120 4e00 0005 0000 0000 0000  el... N.........
00000040: 0000 0000 0001 0000 0000 0000 0002 0000  ................
00000050: 0000 0000 0003 0000 0000 0000 0004 0000  ................
00000060: 0000 0000 0002 0000 0004 0000 0065 7463  .............etc
00000070: 0001 0000 0000 0001 0000 0003 0000 0000  ................
00000080: 0000 0003 0000 0006 0000 0068 6f73 7473  ...........hosts
00000090: 0000 01c8 0000 0001 0000 0005 0000 0000  ................
000000a0: 0000 00

和:

struct inode
{
    int        id;
    char*      name;
    char       is_directory;
    char       is_readonly;
    int        filesize;
    int        num_entries;
    uintptr_t* entries;
};

目前我有这个代码:

struct inode* load_inodes()  /* this one works on the first inode loadexample1 */
{
    printf("Start of load_inodes()");
    
    FILE* f = fopen("master_file_table", "rb");
    if (f == NULL) {
        fprintf(stderr, "Failed to open master_file_table for reading\n");
        perror("reason:");
        return NULL;
    }

    printf("NUM_INODES * sizeof: %ld\n\n", NUM_INODES * sizeof(struct inode));
    // Allocate memory for inodes array
    struct inode* inodes = (struct inode*) malloc(NUM_INODES * sizeof(struct inode));
    printf("ja0"); 

但是我在倒数第二行遇到了分段错误。我不知道为什么。我注意到如果我将 NUM_INODES 更改为高于 900,它会起作用,但不会低于。为什么?

输出:

Start av load_inodes()NUM_INODES * sizeof: 36000

make: *** [makefile:21: run_load1] Segmentation fault (core dumped)
c memory binary malloc
© www.soinside.com 2019 - 2024. All rights reserved.