What is backtrace and memory card after executing C program

I found the following after the program was working correctly and getting accurate results, but I don't understand them.

glibc detected *** ./programa: double free or corruption (out): 0x089300a0 ***

======= Backtrace: =========
/lib/libc.so.6[0x8f8c65]
/lib/libc.so.6(cfree+0x59)[0x8fcc59]
./programa[0x804880a]
/lib/libc.so.6(__libc_start_main+0xdc)[0x8a4ebc]
./programa[0x80483e1]
======= Memory map: ========
00870000-0088b000 r-xp 00000000 08:04 1384259    /lib/ld-2.5.so
0088b000-0088c000 r-xp 0001a000 08:04 1384259    /lib/ld-2.5.so
0088c000-0088d000 rwxp 0001b000 08:04 1384259    /lib/ld-2.5.so
0088f000-009e6000 r-xp 00000000 08:04 1384270    /lib/libc-2.5.so
009e6000-009e8000 r-xp 00156000 08:04 1384270    /lib/libc-2.5.so
009e8000-009e9000 rwxp 00158000 08:04 1384270    /lib/libc-2.5.so
009e9000-009ec000 rwxp 009e9000 00:00 0
00ab3000-00abe000 r-xp 00000000 08:04 1384276    /lib/libgcc_s-4.1.2-20080825.so.1
00abe000-00abf000 rwxp 0000a000 08:04 1384276    /lib/libgcc_s-4.1.2-20080825.so.1
00ddb000-00ddc000 r-xp 00ddb000 00:00 0          [vdso]
08048000-08049000 r-xp 00000000 00:17 8620696    /users/c//programa
08049000-0804a000 rw-p 00000000 00:17 8620696    /users/c/programa
08930000-08951000 rw-p 08930000 00:00 0          [heap]
b7fcd000-b7fcf000 rw-p b7fcd000 00:00 0
b7fd8000-b7fda000 rw-p b7fd8000 00:00 0
bfe6f000-bfe84000 rw-p bffe9000 00:00 0          [stack]

Aborted

      

+3


source to share


3 answers


This is a wreck. To fix this problem, you can follow one of the following approaches.

Use a debugger like GDB to start your program and use the backtrace function to detect the failure function.

Or



Review your code for double free. (Maybe code that you wrote for free resources. Because you mentioned that you are getting accurate results.)

Or



Use the -Xlinker -Map=output.map

c option gcc

when compiling your program. This will create a map file for the executable that has all functional addresses. You can match the address of the fault instruction or the stack trace to find the function that is causing the failure.

+3


source


This is an error that occurred in your program. The reason is in the first linedouble free or corruption

Since your expected result is fine, my guess is that when you free the resources to the end, you are calling the free ones twice in the same memory location.



If you have a hard time finding where this is happening, use GDB (as CCoder suggests) or any other such debugger to keep track of it. They should break when this error occurs.

+1


source


Most likely the problem is with the pointer allocation multiply

. This code has a problem:

   multiply = malloc(m * sizeof(int)); /* Should be "int*" and not "int" */
   for (i = 0; i < q; i++)
                   ^ - Allocation was for m in statement above
      multiply[i] = malloc(q * sizeof(int));

      

You have allocated memory for int

, not int *

for multiply

.
To avoid such errors, it might be better to use a pointer that is allocated on call malloc

, something like malloc (m * sizeof *multiply)


Also, as you can see, you have malloc

ed for m

elements for multiply

, but allocate for q

. Try to change q

to m

both distribution and release.
Something along the lines:

multiply = malloc(m * sizeof *multiply);
for (i = 0; i < m; i++)
  multiply[i] = malloc(q * sizeof *multiply[i]);

      

Side note:
Signature main

must be int main(void)

, note that in C

, main()

does not match main(void)

.

Hope this helps!

0


source







All Articles