Collin
Collin

Reputation: 1875

Valgrind issue - displaying below main instead of error location

Working though learn c the hard way.. have this bit of code:

#include <stdio.h>

/* Warning: This program is wrong on purpose. */

int main()
{
    int age = 10;
    int height;

    printf("I am %d years old.\n");
    printf("I am %d inches tall.\n", height);

    return 0;
}

now when I go to run valgrind I'll do

valgrind --track-origins=yes ./ex4

and I'll get this:

collin@ubuntu:~/learning$ valgrind --track-origins=yes ./ex4
==14332== Memcheck, a memory error detector
==14332== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==14332== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==14332== Command: ./ex4
==14332== 
==14332== Use of uninitialised value of size 4
==14332==    at 0x408C2AB: _itoa_word (_itoa.c:179)
==14332==    by 0x408FB21: vfprintf (vfprintf.c:1654)
==14332==    by 0x409681E: printf (printf.c:34)
==14332==    by 0x4061934: (below main) (libc-start.c:260)
==14332==  Uninitialised value was created by a stack allocation
==14332==    at 0x8048422: main (ex4.c:6)
==14332== 
==14332== Conditional jump or move depends on uninitialised value(s)
==14332==    at 0x408C2B3: _itoa_word (_itoa.c:179)
==14332==    by 0x408FB21: vfprintf (vfprintf.c:1654)
==14332==    by 0x409681E: printf (printf.c:34)
==14332==    by 0x4061934: (below main) (libc-start.c:260)
==14332==  Uninitialised value was created by a stack allocation
==14332==    at 0x8048422: main (ex4.c:6)
==14332== 
==14332== Conditional jump or move depends on uninitialised value(s)
==14332==    at 0x408CFE5: vfprintf (vfprintf.c:1654)
==14332==    by 0x409681E: printf (printf.c:34)
==14332==    by 0x4061934: (below main) (libc-start.c:260)
==14332==  Uninitialised value was created by a stack allocation
==14332==    at 0x8048422: main (ex4.c:6)
==14332== 
==14332== Conditional jump or move depends on uninitialised value(s)
==14332==    at 0x408D061: vfprintf (vfprintf.c:1654)
==14332==    by 0x409681E: printf (printf.c:34)
==14332==    by 0x4061934: (below main) (libc-start.c:260)
==14332==  Uninitialised value was created by a stack allocation
==14332==    at 0x8048422: main (ex4.c:6)
==14332== 
==14332== Conditional jump or move depends on uninitialised value(s)
==14332==    at 0x4091328: vfprintf (vfprintf.c:1654)
==14332==    by 0x409681E: printf (printf.c:34)
==14332==    by 0x4061934: (below main) (libc-start.c:260)
==14332==  Uninitialised value was created by a stack allocation
==14332==    at 0x8048422: main (ex4.c:6)
==14332== 
==14332== Conditional jump or move depends on uninitialised value(s)
==14332==    at 0x408D0B8: vfprintf (vfprintf.c:1654)
==14332==    by 0x409681E: printf (printf.c:34)
==14332==    by 0x4061934: (below main) (libc-start.c:260)
==14332==  Uninitialised value was created by a stack allocation
==14332==    at 0x8048422: main (ex4.c:6)
==14332== 
==14332== Conditional jump or move depends on uninitialised value(s)
==14332==    at 0x408D0F1: vfprintf (vfprintf.c:1654)
==14332==    by 0x409681E: printf (printf.c:34)
==14332==    by 0x4061934: (below main) (libc-start.c:260)
==14332==  Uninitialised value was created by a stack allocation
==14332==    at 0x8048422: main (ex4.c:6)
==14332== 
I am -1097264588 years old. 
I am 69169152 inches tall.
==14332== 
==14332== HEAP SUMMARY:
==14332==     in use at exit: 0 bytes in 0 blocks
==14332==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==14332== 
==14332== All heap blocks were freed -- no leaks are possible
==14332== 
==14332== For counts of detected and suppressed errors, rerun with: -v
==14332== ERROR SUMMARY: 46 errors from 7 contexts (suppressed: 0 from 0)

Why am I getting this 'below main' stuff rather than valgrinding telling me the problem locations? I thought doing --track-origins=yes was supposed to fix this.

Appreciate the help.

This is my Makefile I used

CFLAGS=-Wall -g

clean:
    rm -f ex4

Upvotes: 0

Views: 2785

Answers (2)

caf
caf

Reputation: 239331

I was able to fix this issue by adding -fno-omit-frame-pointer to my CFLAGS.

Upvotes: 0

Ravichandra Sutrave
Ravichandra Sutrave

Reputation: 179

It is because you didnt compile your program using debugging flag (-g)

I tried your code with -g option and it shows the line number

bash-3.2$ cat test.c
#include <stdio.h>

/* Warning: This program is wrong on purpose. */

int main()
{
    int age = 10;
    int height;

    printf("I am %d years old.\n");
    printf("I am %d inches tall.\n", height);

    return 0;
}
bash-3.2$ gcc -g test.c
bash-3.2$ 
bash-3.2$ valgrind --track-origins=yes ./a.out
==18523== Memcheck, a memory error detector
==18523== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==18523== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==18523== Command: ./a.out
==18523==
I am -16775864 years old.
==18523== Use of uninitialised value of size 8
==18523==    at 0x3FF8441E2D: _itoa_word (in /lib64/libc-2.5.so)
==18523==    by 0x3FF8445212: vfprintf (in /lib64/libc-2.5.so)
==18523==    by 0x3FF844D009: printf (in /lib64/libc-2.5.so)
==18523==    by 0x4004C7: main (test.c:11)
==18523==  Uninitialised value was created by a stack allocation
==18523==    at 0x400498: main (test.c:6)
==18523==
==18523== Conditional jump or move depends on uninitialised value(s)
==18523==    at 0x3FF8441E37: _itoa_word (in /lib64/libc-2.5.so)
==18523==    by 0x3FF8445212: vfprintf (in /lib64/libc-2.5.so)
==18523==    by 0x3FF844D009: printf (in /lib64/libc-2.5.so)
==18523==    by 0x4004C7: main (test.c:11)
==18523==  Uninitialised value was created by a stack allocation
==18523==    at 0x400498: main (test.c:6)
==18523==
==18523== Conditional jump or move depends on uninitialised value(s)
==18523==    at 0x3FF8445298: vfprintf (in /lib64/libc-2.5.so)
==18523==    by 0x3FF844D009: printf (in /lib64/libc-2.5.so)
==18523==    by 0x4004C7: main (test.c:11)
==18523==  Uninitialised value was created by a stack allocation
==18523==    at 0x400498: main (test.c:6)
==18523==
==18523== Conditional jump or move depends on uninitialised value(s)
==18523==    at 0x3FF8445AA6: vfprintf (in /lib64/libc-2.5.so)
==18523==    by 0x3FF844D009: printf (in /lib64/libc-2.5.so)
==18523==    by 0x4004C7: main (test.c:11)
==18523==  Uninitialised value was created by a stack allocation
==18523==    at 0x400498: main (test.c:6)
==18523==
==18523== Conditional jump or move depends on uninitialised value(s)
==18523==    at 0x3FF8443949: vfprintf (in /lib64/libc-2.5.so)
==18523==    by 0x3FF844D009: printf (in /lib64/libc-2.5.so)
==18523==    by 0x4004C7: main (test.c:11)
==18523==  Uninitialised value was created by a stack allocation
==18523==    at 0x400498: main (test.c:6)
==18523==
I am 0 inches tall.
==18523==
==18523== HEAP SUMMARY:
==18523==     in use at exit: 0 bytes in 0 blocks
==18523==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==18523==
==18523== All heap blocks were freed -- no leaks are possible
==18523==
==18523== For counts of detected and suppressed errors, rerun with: -v
==18523== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 7 from 7)

Edit: Using -Wall will not compile this program as it has warnings. So that means your CFLAGS are not used while compiling.

Try changing yor makefile as below:

CFLAGS= -g
TARGET = ex4
ex4: $(TARGET)
$(TARGET): $(TARGET).c
    $(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c
clean:
    rm -rf ex4

It worked for me using above makefile. Google about makefiles to learn more.

Upvotes: 2

Related Questions