我使用malloc来分配8192个字节的内存; malloc成功返回,但由于某种原因,我无法访问超过4205字节的内存块的内存.
我也试过分配更大的内存块(即8192*2),但仍然没有运气,只有第一个4205的内存可以访问:(
这是部分代码:
int num_ino = 256;
struct inode * ino_table = malloc(8192);
assert(ino_table);
for(int i = 0; i < num_ino; i ++){
printf("pre core dump %d\n", i);
memcpy(ino_table + i * sizeof(struct inode), &inotable[i], sizeof(struct inode));
}
Run Code Online (Sandbox Code Playgroud)
这是gdb中发生的事情:
Breakpoint 1, unixfilesystem_init (dfd=3) at unixfilesystem.c:54
54 assert(ino_table);
(gdb) p *(ino_table)
$1 = {i_mode = 0, i_nlink = 0 '\000', i_uid = 0 '\000', i_gid = 0 '\000', i_size0 = 0 '\000', i_size1 = 0, i_addr = {0, 0, 0, 0, 0, 0, 0, 0},
i_atime = {0, 0}, i_mtime = {0, 0}}
(gdb) p *(ino_table + 4205)
$2 = {i_mode = 0, i_nlink = 0 '\000', i_uid = 0 '\000', i_gid = 0 '\000', i_size0 = 0 '\000', i_size1 = 0, i_addr = {0, 0, 0, 0, 0, 0, 0, 0},
i_atime = {0, 0}, i_mtime = {0, 0}}
(gdb) p *(ino_table + 8000)
Cannot access memory at address 0x643a30
(gdb) p *(ino_table + 4206)
Cannot access memory at address 0x625ff0
Run Code Online (Sandbox Code Playgroud)
当您执行指针运算时ino_table,单位是sizeof(struct inode),而不是字节.
就这样
ino_table + i * sizeof(struct inode)
Run Code Online (Sandbox Code Playgroud)
应该成为
ino_table + i
Run Code Online (Sandbox Code Playgroud)
最后,我改变了malloc()这样:
struct inode * ino_table = malloc(num_ino * sizeof(struct inode));
Run Code Online (Sandbox Code Playgroud)