Learner
Learner

Reputation: 79

What is backtrace and memory map after executing the C program

I encountered following after running program 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

Upvotes: 5

Views: 6780

Answers (3)

another.anon.coward
another.anon.coward

Reputation: 11395

Most likely the problem is in the allocation of multiply pointer. This code has 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 and not int * for multiply.
To avoid these kinds of mistake, it may be better to use the pointer which is being allocated in the call to malloc, something like malloc (m * sizeof *multiply)
Also, as you can see that you have malloced for m elements for multiply but allocate for q. Try changing q to m in both allocation and freeing.
Something on the lines of:

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

Side note:
Signature of main should be int main(void), note that in C, main() is not the same as main(void).

Hope this helps!

Upvotes: 0

CCoder
CCoder

Reputation: 2335

It is a crash. To solve this u can follow one of the following approaches.

Use a debugger like GDB to run your program and use backtrace functionality to figure out the crashing function.

Or

Review your code for double free. (May be the code where you have written to free resources. Because you mentioned that you are getting the accurate results.)

Or

Use -Xlinker -Map=output.map option with gcc while compiling your program. This will generate a map file for the executable which has all function addresses. You can map the faulting instruction address or the stack trace to find the function which is causing the crash.

Upvotes: 3

Karthik T
Karthik T

Reputation: 31952

It is a crash that happened in your program. The reason is indicated in the first line double free or corruption

Since your expected output is fine, my guess is that when you release resources towards the end, you are calling free twice on the same memory location somewhere.

If it is not easy to find where this happens, use GDB (as CCoder suggests) or any other such debugger to track it down. They should break when this error happens.

Upvotes: 1

Related Questions