2 icquu icquu 于 2017.09.14 14:30 提问

请问我在这段代码中动态申请的内存,为什么不能使用free释放呢.

编译环境:
CentOS7.2, gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
代码如下:
#include
#include
#include
#include
#include

typedef struct _stack
{
int a;
char str[32];
int elem_lenth;
}stack;
static stack* stackpush(stack s)
{
stack *p;
s = realloc(s, s->elem_lenth + 1);
p = s + s->elem_lenth;
p->a = 0;
memcpy(p->str, s->str, sizeof(s->str));
return s;
}
static stack
stacknew()
{
stack s;
if ((s = (stack
)calloc(1, sizeof(stack))) != NULL)
{
s->a = 1;
s->elem_lenth = 1;
strcpy(s->str, "stacknew");
return s;
}
return NULL;
}
static void stackdestroy(stack *s)
{
free(s);
}
int main()
{
stack *val;
val = stacknew();
assert(val!=NULL);
val = stackpush(val);
printf("%d\t%s\n", val->a, val->str);
printf("%d\t%s\n", (val + 1)->a, (val + 1)->str);

// stackdestroy(val); //如果这里不注释掉,系统会提示出现问题,问题描述请见下方

return 0;

}


出错信息:
*** Error in `./bin/x64/Debug/stack.out': free(): invalid next size (fast): 0x00000000024b6010 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7cfe1)[0x7f0d9aa00fe1]
./bin/x64/Debug/stack.out[0x4007b7]
./bin/x64/Debug/stack.out[0x400851]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f0d9a9a5b15]
./bin/x64/Debug/stack.out[0x4005f9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 fd:00 9729 /root/projects/stack/bin/x64/Debug/stack.out
00600000-00601000 r--p 00000000 fd:00 9729 /root/projects/stack/bin/x64/Debug/stack.out
00601000-00602000 rw-p 00001000 fd:00 9729 /root/projects/stack/bin/x64/Debug/stack.out
024b6000-024d7000 rw-p 00000000 00:00 0 [heap]
7f0d94000000-7f0d94021000 rw-p 00000000 00:00 0
7f0d94021000-7f0d98000000 ---p 00000000 00:00 0
7f0d9a76e000-7f0d9a783000 r-xp 00000000 fd:00 201326729 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f0d9a783000-7f0d9a982000 ---p 00015000 fd:00 201326729 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f0d9a982000-7f0d9a983000 r--p 00014000 fd:00 201326729 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f0d9a983000-7f0d9a984000 rw-p 00015000 fd:00 201326729 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f0d9a984000-7f0d9ab3a000 r-xp 00000000 fd:00 201329923 /usr/lib64/libc-2.17.so
7f0d9ab3a000-7f0d9ad3a000 ---p 001b6000 fd:00 201329923 /usr/lib64/libc-2.17.so
7f0d9ad3a000-7f0d9ad3e000 r--p 001b6000 fd:00 201329923 /usr/lib64/libc-2.17.so
7f0d9ad3e000-7f0d9ad40000 rw-p 001ba000 fd:00 201329923 /usr/lib64/libc-2.17.so
7f0d9ad40000-7f0d9ad45000 rw-p 00000000 00:00 0
7f0d9ad45000-7f0d9ad66000 r-xp 00000000 fd:00 201329916 /usr/lib64/ld-2.17.so
7f0d9af3d000-7f0d9af40000 rw-p 00000000 00:00 0
7f0d9af63000-7f0d9af66000 rw-p 00000000 00:00 0
7f0d9af66000-7f0d9af67000 r--p 00021000 fd:00 201329916 /usr/lib64/ld-2.17.so
7f0d9af67000-7f0d9af68000 rw-p 00022000 fd:00 201329916 /usr/lib64/ld-2.17.so
7f0d9af68000-7f0d9af69000 rw-p 00000000 00:00 0
7ffd8cc5b000-7ffd8cc7c000 rw-p 00000000 00:00 0 [stack]
7ffd8cdfc000-7ffd8cdfe000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

Aborted (core dumped)

疑问:
这里为什么不能使用free释放堆中申请到的内存呢.

1个回答

caozhy
caozhy   Ds   Rxr 2017.09.14 16:27
Csdn user default icon
上传中...
上传图片
插入图片