0
#include <bits/stdc++.h>
int main(){
    int a, b = 10, r;
    printf("%d\n", a);
}

==13235== Memcheck, a memory error detector
==13235== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13235== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==13235== Command: ./assign
==13235==
==13235== error calling PR_SET_PTRACER, vgdb might block
==13235== Conditional jump or move depends on uninitialised value(s)
==13235==    at 0x4AAEAD8: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235==
==13235== Use of uninitialised value of size 8
==13235==    at 0x4A9281B: _itoa_word (_itoa.c:179)
==13235==    by 0x4AAE6F4: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235==
==13235== Conditional jump or move depends on uninitialised value(s)
==13235==    at 0x4A9282D: _itoa_word (_itoa.c:179)
==13235==    by 0x4AAE6F4: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235==
==13235== Conditional jump or move depends on uninitialised value(s)
==13235==    at 0x4AAF3A8: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235==
==13235== Conditional jump or move depends on uninitialised value(s)
==13235==    at 0x4AAE86E: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235== 0
==13235==
==13235== HEAP SUMMARY:
==13235==     in use at exit: 0 bytes in 0 blocks
==13235==   total heap usage: 2 allocs, 2 frees, 73,216 bytes allocated
==13235==
==13235== All heap blocks were freed -- no leaks are possible
==13235==
==13235== Use --track-origins=yes to see where uninitialised values come from
==13235== For lists of detected and suppressed errors, rerun with: -s
==13235== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)

I am not able errors from this input, what is heap summary showing and, why size=8 for the uninitialized variable? Also, "total heap usage: 2 allocs, 2 frees, 73,216 bytes allocated" what is this representing?"ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)"?

dbush
  • 205,898
  • 23
  • 218
  • 273
ronaldo_jr
  • 169
  • 9
  • 1
    First of all: [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl May 23 '20 at 15:26
  • 1
    Second: you never initialized `a`, so reading it (while printing) is [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub) and your entire program is now invalid/meaningless. – Jesper Juhl May 23 '20 at 15:28

2 Answers2

1

The uninitialized variable error is coming from this line:

printf("%d\n", a);

since you are using a without giving it an initial value. This invokes undefined behavior, and should never be done.

The size of the variable being 8 bytes suggests that you are compiling on a 64bit architecture, where an int is 8 bytes, or 64 bits.

The total heap usage is simply a summary of all the memory that was allocated, and freed during the execution of the program. Since you have 0 bytes in use at the end, this means your program is not leaking any memory.

cigien
  • 57,834
  • 11
  • 73
  • 112
1

The first message from valgrind states "Conditional jump or move depends on uninitialised value" and references this line in your code:

printf("%d\n", a);

This line uses the variable a. If you then look at the preceding line:

int a, b = 10, r;

You'll see that a is never assigned a value. That is, it is uninitialized. This is what valgrind is complaining about. You can fix this by giving a a value:

int a = 1, b = 10, r;

The rest of the valgrind messages reference the same line of code from your file, so the rest of the errors followed from the first, and fixing one should fix the rest.

dbush
  • 205,898
  • 23
  • 218
  • 273