使用 getchar() 和 -O3 的奇怪行为
我有这两个函数
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您还没有提供外部代码(还?),因此这是一个猜测。
one
”,然后等待一些输入。 您输入“f[enter]
”。f
”。[enter]
(换行符)。在您的第二个版本中, foo1() 不再读取任何内容。
因此:
one
'two
' 然后等待一些输入。 您输入 'f[enter]
'f
'唯一剩下的问题是为什么它会停止。 为了帮助您解决这个问题,我们必须了解
(某些条件)
实际上是什么。请注意,调用
getchar()
而不保留结果的情况相当罕见(如c = getchar();
)。 你有这样做的理由吗?一个有用的 C 习惯用法是:
强制转换为 void 是程序员的一种指示,表明他们知道正在丢弃返回值。
Since you haven't provided the external code (yet?), here's a guess.
one
' then waits for some input. You type 'f[enter]
'.f
'.[enter]
(a newline character).With your second version, foo1() doesn't read anything any more.
So:
one
'two
' then waits for some input. You type 'f[enter]
'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 inc = getchar();
). Do you have a reason for doing this?One useful C idiom is:
The cast to void is an indication from the programmer that they know they're discarding the return value.