Strange behavior using getchar() and -O3

Posted by Eduardo on Stack Overflow See other posts from Stack Overflow or by Eduardo
Published on 2009-03-22T21:27:29Z Indexed on 2010/05/12 18:54 UTC
Read the original article Hit count: 319

Filed under:
|
|

I have these two functions

    void set_dram_channel_width(int channel_width){
      printf("one\n");
          getchar();
    }


    void set_dram_transaction_granularity(int cacheline_size){
      printf("two\n");
          getchar();
    }
    //output:
    one
    f //my keyboard input
    two
    one
    f  //keyboard input
    two
    one
    f  //keyboard input
    //No more calls

Then I change the functions to:

    void set_dram_channel_width(int channel_width){
      printf("one\n");
    }


    void set_dram_transaction_granularity(int cacheline_size){
      printf("two\n");
      getchar();
    }
    //output
    one
    two 
    f //keyboard input
    //No more calls

Both functions are called by an external code, the code for both programs is the same, just changing the getchar() I get those two different outputs. Is this possible or there is something that is really wrong in my code?

Thanks

This is the output I get with GDB**

For the first code

(gdb) break mem-dram.c:374
Breakpoint 1 at 0x71c810: file build/ALPHA_FS/mem/dramsim/mem-dram.c, line 374.
(gdb) break mem-dram.c:381
Breakpoint 2 at 0x71c7b0: file build/ALPHA_FS/mem/dramsim/mem-dram.c, line 381.
(gdb) run -d ./tmp/MyBench2/ 
one
f
[Switching to Thread 47368811512112 (LWP 17507)]

Breakpoint 1, set_dram_channel_width (channel_width=64)
(gdb) c
Continuing.
two
one
f

Breakpoint 2, set_dram_transaction_granularity (cacheline_size=64)
(gdb) c
Continuing.

Breakpoint 1, set_dram_channel_width (channel_width=8)
374     void set_dram_channel_width(int channel_width){
(gdb) c
Continuing.
two
one
f

For the second code

(gdb) break mem-dram.c:374
Breakpoint 1 at 0x71c7b6: file build/ALPHA_FS/mem/dramsim/mem-dram.c, line 374.
(gdb) break mem-dram.c:380
Breakpoint 2 at 0x71c7f0: file build/ALPHA_FS/mem/dramsim/mem-dram.c, line 380.
(gdb) run
one
two
f
[Switching to Thread 46985688772912 (LWP 17801)]

Breakpoint 1, set_dram_channel_width (channel_width=64)
(gdb) c
Continuing.

Breakpoint 2, set_dram_transaction_granularity (cacheline_size=64)
(gdb) c
Continuing.

Breakpoint 1, set_dram_channel_width (channel_width=8)
(gdb) c
Continuing.

© Stack Overflow or respective owner

Related posts about getchar

Related posts about c