1

I cannot understand why this simple program works fine, but when I check it with Valgrind it gives me too many errors.

#include<stdio.h>

int main()
{
    char input[10]  = {'a','b','c','d','e','f','g','\0'};
    while (fgets(input, 9, stdin))
    {
        printf("\nComplete line: %s\n", input);
    }
    return 0;
}

This is what I get from valgrind

14 times this one (with different addresses)
==2079== Conditional jump or move depends on uninitialised value(s)
==2079==    at 0x425DF0: malloc_hook_ini (in /home/utente/Desktop/program)
==2079==    by 0x48204A: _dl_get_origin (in /home/utente/Desktop/program)
==2079==    by 0x455844: _dl_non_dynamic_init (in /home/utente/Desktop/program)
==2079==    by 0x457485: __libc_init_first (in /home/utente/Desktop/program)
==2079==    by 0x402538: (below main) (in /home/utente/Desktop/program)

==2079== Conditional jump or move depends on uninitialised value(s)
==2079==    at 0x410428: __cxa_atexit (in /home/utente/Desktop/program)
==2079==    by 0x402549: (below main) (in /home/utente/Desktop/program)

**This should be useful**
==2079== Conditional jump or move depends on uninitialised value(s)
==2079==    at 0x4185C8: fgets (in /home/utente/Desktop/program)
==2079==    by 0x401D95: main (program.c:6)

==2079== Conditional jump or move depends on uninitialised value(s)
==2079==    at 0x4263BD: malloc (in /home/utente/Desktop/program)
==2079==    by 0x4738F3: _IO_file_doallocate (in /home/utente/Desktop/program)
==2079==    by 0x41ECBF: _IO_doallocbuf (in /home/utente/Desktop/program)
==2079==    by 0x41B5AB: _IO_file_underflow (in /home/utente/Desktop/program)
==2079==    by 0x41ED75: _IO_default_uflow (in /home/utente/Desktop/program)
==2079==    by 0x4189FB: _IO_getline_info (in /home/utente/Desktop/program)
==2079==    by 0x418609: fgets (in /home/utente/Desktop/program)
==2079==    by 0x401D95: main (api.c:6)
and many others similar to the latter

I give the file using program <input.txt. The file contains rows with max length = 5 and the program properly prints them

I am using Kubuntu and to compile I type:

gcc -DEVAL -std=gnu11 -g -static -o program program.c

Unfortunately I can't use different compiling options because of a school project

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Daniele
  • 43
  • 5
  • Please post the *full* code, including `#include`s and other things that you have hidden. – Eugene Sh. Aug 21 '20 at 16:49
  • Also post the OS/version and compiler/version. – dbush Aug 21 '20 at 16:53
  • Just added, there's nothing more – Daniele Aug 21 '20 at 16:53
  • Why would you initialize your input array, only to use it immediately after that in an fgets? – Robert Harvey Aug 21 '20 at 16:54
  • @RobertHarvey My guess is it was an attempt to silence the valgrind messages. I suspect a library issue. – dbush Aug 21 '20 at 16:56
  • I suspect that the file analyzed is not the one compiled from this code. How do you compile it? – Eugene Sh. Aug 21 '20 at 16:57
  • @RobertHarvey since it says fgets is reading from not initialized I wanted to try that too – Daniele Aug 21 '20 at 16:58
  • @EugeneSh. I deleted everything from the folder that contains it because I thought I could make that mistake – Daniele Aug 21 '20 at 16:59
  • 1
    The problem might be with the static linkage: https://stackoverflow.com/questions/7506134/valgrind-errors-when-linked-with-static-why – Eugene Sh. Aug 21 '20 at 17:03
  • @EugeneSh.That could be the answer. I am not the only person working with those compiling options and nobody has had that issue though. Removing static from compiling options it works. I don't know how to overcome that – Daniele Aug 21 '20 at 17:07

0 Answers0