M
masterloki
Hi there.
It's been a time since I did something in *real* C. Now I'm working in
something with pointers, but it seems that I can't get it right.
I have a int ** that I allocate with malloc, but when trying to free it
I get an error.
int initMatrix(int size, int **matrix){
int i=0;int j=0;
matrix = malloc(sizeof(int*)*size);
for (i=0;i<size+1;i++){
matrix= malloc(3 * sizeof(int));
}
for (i=0;i<size;i++)
for(j=0;j<2;j++){
matrix[j]=j;
printf("%d",matrix[j]);
}
for (i=0;i<size+1;i++){
free(matrix);
}
free(matrix);
}
I got this
*** glibc detected *** ./approach: double free or corruption (out):
0x00000000085b0030 ***
======= Backtrace: =========
/lib/libc.so.6[0x7f63d9174db6]
/lib/libc.so.6(cfree+0x6c)[0x7f63d91796fc]
../approach[0x804850d]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:06 131795
/home/masterloki/approach
08049000-0804a000 rwxp 00000000 08:06 131795
/home/masterloki/approach
085b0000-085d1000 rwxp 00000000 00:00 0
[heap]
7f63d4000000-7f63d4021000 rwxp 00000000 00:00 0
7f63d4021000-7f63d8000000 ---p 00000000 00:00 0
7f63d8eee000-7f63d8f04000 r-xp 00000000 08:06 7029
/usr/lib/libgcc_s.so.1
7f63d8f04000-7f63d9103000 ---p 00016000 08:06 7029
/usr/lib/libgcc_s.so.1
7f63d9103000-7f63d9104000 rwxp 00015000 08:06 7029
/usr/lib/libgcc_s.so.1
7f63d9104000-7f63d924d000 r-xp 00000000 08:06 700
/lib/libc-2.10.1.so
7f63d924d000-7f63d944d000 ---p 00149000 08:06 700
/lib/libc-2.10.1.so
7f63d944d000-7f63d9451000 r-xp 00149000 08:06 700
/lib/libc-2.10.1.so
7f63d9451000-7f63d9452000 rwxp 0014d000 08:06 700
/lib/libc-2.10.1.so
7f63d9452000-7f63d9457000 rwxp 00000000 00:00 0
7f63d9457000-7f63d9474000 r-xp 00000000 08:06 705
/lib/ld-2.10.1.so
7f63d964b000-7f63d964d000 rwxp 00000000 00:00 0
7f63d9671000-7f63d9673000 rwxp 00000000 00:00 0
7f63d9673000-7f63d9674000 r-xp 0001c000 08:06 705
/lib/ld-2.10.1.so
7f63d9674000-7f63d9675000 rwxp 0001d000 08:06 705
/lib/ld-2.10.1.so
7ffffc8e5000-7ffffc8fa000 rwxp 00000000 00:00 0
[stack]
7ffffc9ff000-7ffffca00000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
010101zsh: abort ./approach
I've compiled this with both gcc and tcc and I got the same error. I
debugged this with gdb and it breaks when I reach the free().
What am I doing wrong?
It's been a time since I did something in *real* C. Now I'm working in
something with pointers, but it seems that I can't get it right.
I have a int ** that I allocate with malloc, but when trying to free it
I get an error.
int initMatrix(int size, int **matrix){
int i=0;int j=0;
matrix = malloc(sizeof(int*)*size);
for (i=0;i<size+1;i++){
matrix= malloc(3 * sizeof(int));
}
for (i=0;i<size;i++)
for(j=0;j<2;j++){
matrix[j]=j;
printf("%d",matrix[j]);
}
for (i=0;i<size+1;i++){
free(matrix);
}
free(matrix);
}
I got this
*** glibc detected *** ./approach: double free or corruption (out):
0x00000000085b0030 ***
======= Backtrace: =========
/lib/libc.so.6[0x7f63d9174db6]
/lib/libc.so.6(cfree+0x6c)[0x7f63d91796fc]
../approach[0x804850d]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:06 131795
/home/masterloki/approach
08049000-0804a000 rwxp 00000000 08:06 131795
/home/masterloki/approach
085b0000-085d1000 rwxp 00000000 00:00 0
[heap]
7f63d4000000-7f63d4021000 rwxp 00000000 00:00 0
7f63d4021000-7f63d8000000 ---p 00000000 00:00 0
7f63d8eee000-7f63d8f04000 r-xp 00000000 08:06 7029
/usr/lib/libgcc_s.so.1
7f63d8f04000-7f63d9103000 ---p 00016000 08:06 7029
/usr/lib/libgcc_s.so.1
7f63d9103000-7f63d9104000 rwxp 00015000 08:06 7029
/usr/lib/libgcc_s.so.1
7f63d9104000-7f63d924d000 r-xp 00000000 08:06 700
/lib/libc-2.10.1.so
7f63d924d000-7f63d944d000 ---p 00149000 08:06 700
/lib/libc-2.10.1.so
7f63d944d000-7f63d9451000 r-xp 00149000 08:06 700
/lib/libc-2.10.1.so
7f63d9451000-7f63d9452000 rwxp 0014d000 08:06 700
/lib/libc-2.10.1.so
7f63d9452000-7f63d9457000 rwxp 00000000 00:00 0
7f63d9457000-7f63d9474000 r-xp 00000000 08:06 705
/lib/ld-2.10.1.so
7f63d964b000-7f63d964d000 rwxp 00000000 00:00 0
7f63d9671000-7f63d9673000 rwxp 00000000 00:00 0
7f63d9673000-7f63d9674000 r-xp 0001c000 08:06 705
/lib/ld-2.10.1.so
7f63d9674000-7f63d9675000 rwxp 0001d000 08:06 705
/lib/ld-2.10.1.so
7ffffc8e5000-7ffffc8fa000 rwxp 00000000 00:00 0
[stack]
7ffffc9ff000-7ffffca00000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
010101zsh: abort ./approach
I've compiled this with both gcc and tcc and I got the same error. I
debugged this with gdb and it breaks when I reach the free().
What am I doing wrong?