【发布时间】:2017-03-22 09:14:01
【问题描述】:
我是一个自我狂热的 CS 学习者。 (请把我的 cmets 作为一个极客的查询,以获得他需要的进一步澄清) 我正在编写一个简单的 c 脚本来更好地理解动态分配的内存(除了队列和堆)。为此,我创建了一个简单的“malloced”指针,指向“n int 类型的堆内存”。
int *array;
int size = 10;
//point to a chunk of 10 (m)allocated memory in heap (?) am I right ?
array = (int *) malloc(sizeof(int) * size);
现在我对它们做了一些操作。例如
//passing array as reference (?)
void handy_array(int *pointer, int size) {
/*operation on the (m)allocated array of given size created in heap*/
for(int i = 0; i < size; i++) {
pointer[i] = i-10 ;
}
return;
}
到目前为止一切顺利,现在我们可以像使用 char string[n] = "a string" 一样使用这个数组 [size]。 但是当使用这种方法释放内存时
void free_array(int *array, int size) {
for(int i= 0; i < size; i++){
//free the ith assigned address and its value in heap memory
free(&array[i]);
}
return;
}
现在,我收到了这条有趣的错误消息-
*** Error in `./main': free(): invalid pointer: 0x0973d00c ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb75f6377]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb75fc2f7]
/lib/i386-linux-gnu/libc.so.6(+0x6dbb1)[0xb75fcbb1]
./main[0x8048590]
./main[0x80484c8]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb75a7637]
./main[0x8048391]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 22937845 /directory/to/the/script
08049000-0804a000 r--p 00000000 08:01 22937845 /directory/to/the/script
0804a000-0804b000 rw-p 00001000 08:01 22937845 /directory/to/the/script
0973d000-0975e000 rw-p 00000000 00:00 0 [heap]
b7400000-b7421000 rw-p 00000000 00:00 0
b7421000-b7500000 ---p 00000000 00:00 0
b7559000-b7575000 r-xp 00000000 08:01 13239646 /lib/i386-linux-gnu/libgcc_s.so.1
b7575000-b7576000 rw-p 0001b000 08:01 13239646 /lib/i386-linux-gnu/libgcc_s.so.1
b758f000-b773e000 r-xp 00000000 08:01 13239459 /lib/i386-linux-gnu/libc-2.23.so
b773e000-b773f000 ---p 001af000 08:01 13239459 /lib/i386-linux-gnu/libc-2.23.so
b773f000-b7741000 r--p 001af000 08:01 13239459 /lib/i386-linux-gnu/libc-2.23.so
b7741000-b7742000 rw-p 001b1000 08:01 13239459 /lib/i386-linux-gnu/libc-2.23.so
b7742000-b7745000 rw-p 00000000 00:00 0
b775d000-b7760000 rw-p 00000000 00:00 0
b7760000-b7762000 r--p 00000000 00:00 0 [vvar]
b7762000-b7763000 r-xp 00000000 00:00 0 [vdso]
b7763000-b7785000 r-xp 00000000 08:01 13239455 /lib/i386-linux-gnu/ld-2.23.so
b7785000-b7786000 rw-p 00000000 00:00 0
b7786000-b7787000 r--p 00022000 08:01 13239455 /lib/i386-linux-gnu/ld-2.23.so
b7787000-b7788000 rw-p 00023000 08:01 13239455 /lib/i386-linux-gnu/ld-2.23.so
bf7eb000-bf80c000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
我的确切问题是:
0. 这是从全局或本地范围分配内存块的实际做法,或者从主方法启动它们并使用我在这里做的本地方法。
1. 上述场景中释放内存的具体方法是什么?
和
(可选;如果您认为不相关,请忽略)
2. 我想要一些提示来理解有趣的错误信息,比如回溯和内存映射
提前致谢
【问题讨论】:
-
C 不是脚本语言。您应该阅读How to Ask 并提供minimal reproducible example。旁注:一般不要投射
malloc& friends 或void *的结果。 -
一个 malloc 后面跟着一个 free,而不是 10 个 free。
标签: c pointers memory memory-management