使用 getchar() 和 -O3 的奇怪行为

发布于 2024-07-15 01:09:15 字数 2078 浏览 9 评论 0原文

我有这两个函数

    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

然后我将函数更改为:

    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 

两个函数均由外部代码调用,两个程序的代码相同,只需更改 getchar() 我得到这两个不同的输出。 这是可能的还是我的代码中确实有问题?

谢谢

这是我用 GDB 得到的输出**

对于第一个代码

(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

对于第二个代码

(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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2024-07-22 01:09:15

由于您还没有提供外部代码(还?),因此这是一个猜测。

while(some condition) {
    foo1();
    foo2();
}
  • foo1 打印“one”,然后等待一些输入。 您输入“f[enter]”。
  • foo1 使用“f”。
  • foo2 打印 'two' 然后使用 [enter] (换行符)。
  • 然后你又回到起点,这一切又会发生。

在您的第二个版本中, foo1() 不再读取任何内容。

因此:

  • foo1 打印 'one'
  • foo2 打印 'two' 然后等待一些输入。 您输入 'f[enter]'
  • foo2 消耗了 'f'

唯一剩下的问题是为什么它会停止。 为了帮助您解决这个问题,我们必须了解(某些条件)实际上是什么。

请注意,调用 getchar() 而不保留结果的情况相当罕见(如 c = getchar();)。 你有这样做的理由吗?

一个有用的 C 习惯用法是:

(void) getchar(); 

强制转换为 void 是程序员的一种指示,表明他们知道正在丢弃返回值。

Since you haven't provided the external code (yet?), here's a guess.

while(some condition) {
    foo1();
    foo2();
}
  • foo1 prints 'one' then waits for some input. You type 'f[enter]'.
  • foo1 consumes the 'f'.
  • foo2 prints 'two' then consumes the [enter] (a newline character).
  • Then you go back to the start, and it all happens again.

With your second version, foo1() doesn't read anything any more.

So:

  • foo1 prints 'one'
  • foo2 prints 'two' then waits for some input. You type 'f[enter]'
  • foo2 consumes the 'f'

The only remaining question is why it stops when it does. To help you with that, we'd have to see what (some condition) actually is.

Note that it's fairly unusual to call getchar() without keeping the result (as in c = getchar();). Do you have a reason for doing this?

One useful C idiom is:

(void) getchar(); 

The cast to void is an indication from the programmer that they know they're discarding the return value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文