【发布时间】:2014-02-05 18:24:30
【问题描述】:
测试平台为 32 位 x64 Linux,coreutils 8.5。
base64的源码中,fwrite会使用stdout 输出base64编码的字符串
-
当我使用 ltrace 打印所有 libc 调用时,我们可以看到 stdout 等于 0xb772da20
__libc_start_main(0x8048eb0, 2, 0xbf892f74, 0x804cb50, 0x804cbc0 <unfinished ...> strrchr("base64", '/') = NULL setlocale(6, "") = "en_US.UTF-8" bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale" textdomain("coreutils") = "coreutils" __cxa_atexit(0x804a3b0, 0, 0, 0xbf892f74, 2) = 0 getopt_long(2, 0xbf892f74, "diw:", 0x0804d1a0, NULL) = -1 fopen64("testbase64", "rb") = 0x8591878 fileno(0x8591878) = 3 posix_fadvise64(3, 0, 0, 0, 0) = 0 fread_unlocked(0xbf89225c, 1, 3072, 0x8591878) = 900 fwrite_unlocked("Ly8gcXVpY2tTb3J0LmMKI2luY2x1ZGUg"..., 1, 76, 0xb772da20) = 76 -
当我这样修改base64的代码时:
int main (int argc, char **argv) { printf("%p \n", stdout); int opt; FILE *input_fh; const char *infile; .....
输出仍然是 0xb772da20 ,这对我来说很奇怪,因为这是 base64.c 的第一行。
我在coreutils的lib文件夹中grep
grep stdout *.h
我没有看到任何标准输出的预定义。
谁能帮我解释一下为什么 stdout 会被定义为 "0xb772da20" ,而不是 1,而不是 0?
【问题讨论】:
-
要打印指针值,请将其转换为
void*。通常没关系,但void*和其他指针类型不保证具有相同的表示或以与参数相同的方式传递。printf("%p\n", (void*)stdout);
标签: c stdout libc gnu-coreutils