Program received signal SIGSEGV, Segmentation fault.
0x40093343 in _int_malloc () from /lib/tls/libc.so.6
(gdb) bt
#0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6
#1 0x40094c54 in malloc () from /lib/tls/libc.so.6
It's really strange; I just call malloc() like "tmp=malloc(size);"
the system gives me Segmentation fault
I want to write a code to do like a dynamic array, and the code is as
follow:
char *t=space->ptr;
int size=0;
char *tmp=NULL;
printf("pointer:%p\tsize:%d\n" , space->ptr ,
space->capacity*space->unit_size);
space->capacity+=100;
//printf("%s\n" , (char *)space->ptr);
//space->ptr=realloc(space->ptr , space->capacity*space->unit_size);
size=space->capacity*space->unit_size;
tmp=malloc(size);
printf("---pointer:%p\tnew size:%d\n" , tmp ,
space->capacity*space->unit_size);
space->ptr=tmp;
memcpy(space->ptr , t , (space->capacity-100)*space->unit_size);
free(t);
if(space->ptr == NULL)
err_quit("there is not enough space\n");
At first I use realloc() to realize the dynamic array, but there is
also Segmentation fault, so I change the code.
The following is what the program print when it ran.
The first time it goes well:
pointer:(nil) size:0
---pointer:0x8051230 new size:100
pointer:0x8051230 size:100
---pointer:0x8051b20 new size:200
pointer:0x8051b20 size:200
---pointer:0x8051cd8 new size:300
pointer:0x8051cd8 size:300
---pointer:0x8051e08 new size:400
pointer:0x8051e08 size:400
---pointer:0x8051fa0 new size:500
pointer:0x8051fa0 size:500
---pointer:0x8051cd8 new size:600
pointer:0x8051cd8 size:600
---pointer:0x8051f38 new size:700
free pointer:0x8051f38
For the second time, there is something wrong, glibc says I have double
free 0x08051230, but I'm sure I don't
pointer:(nil) size:0
---pointer:0x8051230 new size:100
pointer:0x8051230 size:100
---pointer:0x8051b20 new size:200
*** glibc detected *** double free or corruption: 0x08051230 ***
pointer:0x8051b20 size:200
---pointer:0x8052cf8 new size:300
pointer:0x8052cf8 size:300
---pointer:0x8052e28 new size:400
pointer:0x8052e28 size:400
---pointer:0x8052fc0 new size:500
pointer:0x8052fc0 size:500
---pointer:0x8052cf8 new size:600
pointer:0x8052cf8 size:600
---pointer:0x8052f58 new size:700
pointer:0x8052f58 size:700
---pointer:0x8053218 new size:800
pointer:0x8053218 size:800
---pointer:0x8052cf8 new size:900
pointer:0x8052cf8 size:900
---pointer:0x8053080 new size:1000
pointer:0x8053080 size:1000
---pointer:0x8053470 new size:1100
pointer:0x8053470 size:1100
---pointer:0x8052cf8 new size:1200
pointer:0x8052cf8 size:1200
---pointer:0x80531b0 new size:1300
pointer:0x80531b0 size:1300
---pointer:0x80536c8 new size:1400
pointer:0x80536c8 size:1400
---pointer:0x8052cf8 new size:1500
pointer:0x8052cf8 size:1500
---pointer:0x80532d8 new size:1600
pointer:0x80532d8 size:1600
---pointer:0x8053920 new size:1700
free pointer:0x8053920
The third time, I get Segmentation fault
---pointer:0x8051b20 new size:100
pointer:0x8051b20 size:100
Program received signal SIGSEGV, Segmentation fault.
0x40093343 in _int_malloc () from /lib/tls/libc.so.6
Why?
It troubles me too much.
Please help me!
Thank you!