#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char * f()
{
char *ptr = malloc(30);
ptr = "abcdefg";
printf("&ptr=%p,%s,size=%d/n",ptr,ptr,strlen(ptr));
free(ptr);
return (char *)0;
}
int main()
{
// char *p = f();//it seems that a memory freed is unuseable
f();
return 0;
}
[root@bogon cTest]# ./a.out
&ptr=0x8048550,abcdefg,size=7
*** glibc detected *** ./a.out: free(): invalid pointer: 0x08048550 ***
======= Backtrace: =========
/lib/libc.so.6[0xa265a5]
/lib/libc.so.6(cfree+0x59)[0xa269e9]
./a.out[0x8048447]
./a.out[0x8048468]
/lib/libc.so.6(__libc_start_main+0xdc)[0x9d2e9c]
./a.out[0x8048331]
======= Memory map: ========
00545000-00550000 r-xp 00000000 03:01 4224575 /lib/libgcc_s-4.1.2-20080825.so.1
00550000-00551000 rwxp 0000a000 03:01 4224575 /lib/libgcc_s-4.1.2-20080825.so.1
00782000-00783000 r-xp 00782000 00:00 0 [vdso]
0099a000-009b4000 r-xp 00000000 03:01 4224500 /lib/ld-2.5.so
009b4000-009b5000 r-xp 00019000 03:01 4224500 /lib/ld-2.5.so
009b5000-009b6000 rwxp 0001a000 03:01 4224500 /lib/ld-2.5.so
009bd000-00afc000 r-xp 00000000 03:01 4224560 /lib/libc-2.5.so
00afc000-00afd000 --xp 0013f000 03:01 4224560 /lib/libc-2.5.so
00afd000-00aff000 r-xp 0013f000 03:01 4224560 /lib/libc-2.5.so
00aff000-00b00000 rwxp 00141000 03:01 4224560 /lib/libc-2.5.so
00b00000-00b03000 rwxp 00b00000 00:00 0
08048000-08049000 r-xp 00000000 03:01 98389 /root/Desktop/cTest/a.out
08049000-0804a000 rw-p 00000000 03:01 98389 /root/Desktop/cTest/a.out
09fce000-09fef000 rw-p 09fce000 00:00 0 [heap]
b7fbf000-b7fc1000 rw-p b7fbf000 00:00 0
b7fd8000-b7fd9000 rw-p b7fd8000 00:00 0
bfd2c000-bfd41000 rw-p bffea000 00:00 0 [stack]
已放弃
结果不理解。