奇怪:printf()的影响变量值 [英] Weird: printf() affects variable value

查看:121
本文介绍了奇怪:printf()的影响变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code块:

int somefunc() {
    /* ... */
    while ((pos = KMP_index(array, size, pattern, plen)) > -1) {
        count++;
    }
    return count;
}

somefunc()被称为在多个子进程,每个叉一个电话。

somefunc() is called in multiple child processes, one call per fork.

我的code编译和Linux上的x86_64和i386按预期工作。但是,当我对上网本的Atom(Arch Linux的i686的),运行计数变量不超过2!

My code compiles and works as expected on Linux x86_64 and i386. But when I run it on Atom netbook (Arch Linux i686), count variable never exceeds 2!

while (...) {
    count++;  //succesfully increments 
}
return count; //it's maximum 2!

不过,如果我加的printf():

However, if I add printf() :

while (...) {
    count++;  //succesfully increments
    printf("%d", anything);
}
return count; //value as expected

印刷空字符或 fflush ING标准输出不会在这里工作。我必须打印至少有一个字符,只有变量是罚款。它驱使我疯了。

Printing empty character or fflushing the stdout don't work here. I have to print at least one character, only then the variable is fine. And it drives me nuts.

有人能告诉我,为什么我会甚至不得不使用这样的变通方法?
难道是我的Linux环境的问题吗? (没什么特别的,GCC 4.8,股票的内核)
谢谢你。

Can someone please tell me, why would I even have to use such "workaround"? Could it be an issue with my linux environment? (Nothing special, GCC 4.8, stock kernel) Thank you.

P.S整个源在这里 http://pastebin.com/4eEHMbKn
是的,它是一门功课:)我需要建立一个类似grep的工具处理在独立的进程中每个文件。

P.S the whole source is here http://pastebin.com/4eEHMbKn . Yes, it's a homework :) I need to create a grep-like utility processing every file in separate process.

推荐答案

KMP_index()通话 KMP_failure()初始化失败[] 阵列。不幸的是, KMP_failure()未能初始化数组的第一个元素。

Your KMP_index() calls KMP_failure() to initialize the failure[] array. Unfortunately, KMP_failure() fails to initialize the first element of the array.

KMP_index()不检查的第一个元素失败[] 。因为该值没有设置,从的结果KMP_index()是半随机的,这取决于previous函数调用如何使用的堆栈(本地变量)。这就解释了为什么从该函数的结果取决于你碰巧之前的 KMP_index()调用叫什么。

KMP_index() does examine the first element of failure[]. Because that value is unset, the results from KMP_index() are semi-random, depending on how previous function calls used stack (local variables). This explains why the result from that function depends on what you happen to call prior to the KMP_index() call.

(我这样做,你的code编译没有在GCC 4.6.3任何警告,所以编译器并没有注意到,要么另行通知。)

(I did notice that your code compiles without any warnings on GCC 4.6.3, so the compiler did not notice that either.)

最有可能您成功的测试是一个64位的平台,在这里偶然放东西的数组的第一个元素不同,比32位Atom架构上。尤其是,的printf()改变的情况下,因为它使用相当多的堆栈:的下一次调用KMP_index()将获得失败[] 阵列与垃圾与调用留下遗留下来的初始元素的printf()

Most likely your successful tests are on a 64-bit platform, where happenstance puts something different to the first element of that array, than on that 32-bit Atom architecture. In particular, the printf() changes the situation, because it uses quite a bit of stack: the next invocation of KMP_index() will get a failure[] array with the initial element left over from garbage left from calling printf().

在事实上, -m32 -m64 和不同的优化设置(编译-Os -O3 )产生失败[]

In fact, compiling with -m32 and -m64 and different optimization settings (-Os, -O3) produce different semi-random values in the first element in failure[].

希望这有助于。

这篇关于奇怪:printf()的影响变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆