如何解决使用 realloc 分配一个 INT_MAX-1 元素数组
在程序中,我创建了一个结构数组。数组的长度不会超过 C 中
int new_size(int size,int single_el) {
if (size < INT_MAX/2) {
return 2*single_el + 2*size
} else {
return INT_MAX - 1;
}
}
int current_size(int single_el,int ind) {
return ind*single_el;
}
void reall(struct **instr,int* size,int ind,int single_el) {
int cur_s = current_size(single_el,ind);
if ( cur_s >= (*size)) {
int sisi = new_size(*size,single_el);
*size = sisi;
printf("%d\n",sisi);
struct* t = realloc(*instr,(size_t)*size);
if (t == NULL)
printf("well done\n");
else
*instr = t;
}
}
当我到达 size = INT_MAX - 1;
时,realloc 返回 null。如果我假设 INT_MAX 是正确的大小,我该如何处理?我明白,如果内存不足,realloc 会返回 null,但是如果我的大小正确,该怎么办?因此:如何动态分配INT_MAX大小的数组?
这里是 valgrind 错误:
==31211== Invalid write of size 1
==31211== at 0x10CAE1: machine (obejsc10.c:362)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211== Address 0xa9c8a018 is 0 bytes after a block of size 1,342,177,240 alloc'd
==31211== at 0x4837D7B: realloc (vg_replace_malloc.c:826)
==31211== by 0x10BE14: reall (obejsc10.c:234)
==31211== by 0x10CB37: machine (obejsc10.c:358)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211==
==31211== Invalid write of size 4
==31211== at 0x10CAE4: machine (obejsc10.c:362)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512) ==31211== Address 0xa9c8a01c is 4 bytes after a block of size 1,240 alloc'd
==31211== at 0x4837D7B: realloc (vg_replace_malloc.c:826)
==31211== by 0x10BE14: reall (obejsc10.c:234)
==31211== by 0x10CB37: machine (obejsc10.c:358)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211==
==31211== Invalid write of size 4
==31211== at 0x10CAE7: machine (obejsc10.c:362)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211== Address 0xa9c8a020 is 8 bytes after a block of size 1,240 alloc'd
==31211== at 0x4837D7B: realloc (vg_replace_malloc.c:826)
==31211== by 0x10BE14: reall (obejsc10.c:234)
==31211== by 0x10CB37: machine (obejsc10.c:358)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211==
==31211== Invalid write of size 4
==31211== at 0x10CAEE: machine(obejsc10.c:362)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211== Address 0xa9c8a024 is 12 bytes after a block of size 1,240 alloc'd
==31211== at 0x4837D7B: realloc (vg_replace_malloc.c:826)
==31211== by 0x10BE14: reall (obejsc10.c:234)
==31211== by 0x10CB37: machine (obejsc10.c:358)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211==
==31211== Invalid write of size 4
==31211== at 0x10CAF5: machine (obejsc10.c:362)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211== Address 0xa9c8a028 is 16 bytes after a block of size 1,240 alloc'd
==31211== at 0x4837D7B: realloc (vg_replace_malloc.c:826)
==31211== by 0x10BE14: reall (obejsc10.c:234)
==31211== by 0x10CB37: machine (obejsc10.c:358)
==31211== by 0x10D441: game (obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211==
==31211==
==31211== Process terminating with default action of signal 11 (SIGSEGV)
==31211== Access not within mapped region at address 0xA9C8B000
==31211== at 0x10CAEE: machine (obejsc10.c:362)
==31211== by 0x10D441: game(obejsc10.c:481)
==31211== by 0x10D615: main (obejsc10.c:512)
==31211== If you believe this happened as a result of a stack
==31211== overflow in your program's main thread (unlikely but
==31211== possible),you can try to increase the size of the
==31211== main thread stack using the --main-stacksize= flag.
==31211== The main thread stack size used in this run was 8388608. Segmentation fault
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。