1

My application is built with Eclipse CDT Neon (4.6.2RC3) and gcc (Debian 6.3.0-18) 6.3.0 20170516

#include <stdio.h>
#include <stdlib.h>

int
main()
{
    for (int i = 0;;i++) {  /* Loop through characters. */
        int ch;
        ch = getc(stdin);   /* Read the next character. */
        if (i < 100 || i > 67730) fprintf(stderr, "%i(%i) ", i, ch);
        else if (i == 100) fprintf(stderr, " ----- ");
        if (ch == EOF)
            break;      /* Exit loop on end-of-file. */ 
    }
    fprintf(stderr, "\n\n*** finished successfully ***\n");
    return 0;
}

Here are my compilation options: -O0 -pedantic -Wall -Wextra -c -ggdb -fmessage-length=0 -Wno-implicit-function-declaration. I run it with help of an input file piped to stdin.

When I run it from the console, everything is fine. Here is the end of the normal output when runned from the console:

./myprogram < inputfile.pdf

67825(32) 67826(54) 67827(10) 67828(10) 67829(12) 67830(-1) 

*** finished successfully ***

But when I run it in debug mode from Eclipse CDT, it waits indefinitely at the last character (67829th) before EOF (67830th) and the program never exits. If I suspend execution, I can check it is stucked inside the ch = getc(stdin); line. Here is the stack at this point :

Thread #1 [ArithmeticCoder] 29598 [core: 5] (Running : User Request)    

Thread #1 [myprogram] 29598 [core: 6] (Suspended : Signal : SIGINT:Interrupt)   
    __read_nocancel() at /build/glibc-p3Km7c/glibc-2.24/io/../sysdeps/unix/syscall-template.S:84 0x7ffff7811700 
    _IO_new_file_underflow() at /build/glibc-p3Km7c/glibc-2.24/libio/fileops.c:600 0x7ffff77a9a00   
    __GI__IO_default_uflow() at /build/glibc-p3Km7c/glibc-2.24/libio/genops.c:413 0x7ffff77aab02    
    _IO_getc() at /build/glibc-p3Km7c/glibc-2.24/libio/getc.c:38 0x7ffff77a54f0 
    main() at /.../WorkSpace/myprogram/src/myprogram.c:20 0x555555554f08    

I have tried:

  • with fgetc which is very similar, but the result is the same.
  • in Eclipse CDT RUN mode, and it does not reach the end of the file. I assume the problem is the same and due to running this application under the Eclipse CDT environment.
  • Replacing ch = getc(stdin); if (ch == EOF) by int s = fread (&ch, 1, 1, stdin ); if (ch == EOF || s == 0), the result is the same ! I expected not since their behaviour is really different.

What kind of mistake may I have in my settings?

Any idea please ?

lalebarde
  • 1,684
  • 1
  • 21
  • 36
  • 1
    Why are you disabling warnings? – melpomene Apr 27 '18 at 14:45
  • The original code is an old C program that uses implicit function declarations. I have added headers. I agree I should have remove it. Thanks. – lalebarde Apr 27 '18 at 14:52
  • I don't see anything in this sample program that could cause this problem; if this is really the exact program that gets stuck, it has to be an issue with Eclipse somehow. – zwol Apr 27 '18 at 14:53
  • 3
    I would like to know exactly how you are providing the file on stdin when running under Eclipse. – zwol Apr 27 '18 at 14:54
  • @zwol : yes, exact program, everything else is commented. The input file is provided like explained in the solution [here](https://stackoverflow.com/questions/50061178/how-to-allocate-an-input-file-instead-of-the-stdin-in-an-eclipse-cdt-application/50063579#50063579). – lalebarde Apr 27 '18 at 15:00
  • Is it perhaps that your debugger is broken? Because it looks to me that the debugger sees if (ch == EOF) belongs to the else statement one line above. – Dippo Apr 27 '18 at 15:13
  • @Dippo in RUN mode, the debugger is not used – lalebarde Apr 27 '18 at 16:06
  • 1
    *The original code is an old C program that uses implicit function declarations. I have added headers. I agree I should have remove it* Did you remove the header declarations for your function calls? Adding declarations to functions defined in the K&R style will break things **BADLY**. K&R-style functions will expect that all the arguments passed will have undergone [default argument promotions](https://stackoverflow.com/questions/1255775/default-argument-promotions-in-c-function-calls). But if you provide a header declaration to the caller, the caller won't do those promotions. – Andrew Henle Apr 28 '18 at 17:15
  • @Andrew Thank you very much, I was not aware of that. I am going to update the code and come back for a status. – lalebarde May 05 '18 at 05:53
  • Why is the test for EOF not the first thing in the loop? Also note that after `fread (&ch, 1, 1, stdin );` it's not meaningful to test `ch` for EOF since EOF is an int (usually -1) and the fread has read a char. – Jens Jun 13 '18 at 17:07
  • @Jens : what is before the test for EOF is only traces (printf). I agree fread shall be used with feof – lalebarde Jun 16 '18 at 08:30

0 Answers0