分类:
2008-11-18 15:46:09
习题
7.2 When is the output from the printfs in actually output?
如下是figure 7.3的代码:
#include "apue.h"
static void my_exit1(void);
static void my_exit2(void);
int
main(void)
{
if (atexit(my_exit2) != 0)
err_sys("can't register my_exit2");
if (atexit(my_exit1) != 0)
err_sys("can't register my_exit1");
if (atexit(my_exit1) != 0)
err_sys("can't register my_exit1");
printf("main is done\n");
return(0);
}
static void
my_exit1(void)
{
printf("first exit handler\n");
}
static void
my_exit2(void)
{
printf("second exit handler\n");
}
答案:
When the program is run interactively, standard output is usually line buffered, so the actual output occurs when each newline is output. If standard output were directed to a file, however, it would probably be fully buffered, and the actual output wouldn't occur until the standard I/O cleanup is performed.
如果一个流于一个terminal对应,比如键盘或显示器,那么就是默认的line buffered。所以每次调用printf时如有换行符都会输出。如果他们被重定向到一个普通文件,那么就成了full buffered。那么就会在main执行完毕,返回到exit函数,而exit函数调用完所有的被atext注册了的函数之后,最后对standard library进行cleanup工作时关闭所有的打开的流的时候才会将缓冲中的数据写出去。
7.3 Is there any way for a function that is called by main to examine the command-line arguments without (a) passing argc and argv as arguments from main to the function or (b) having main copy argc and argv into global variables?
问题:有没有一种方法,我们不在main函数内部直接使用argc和argv, 就能够访问argv?
答案:
On most UNIX systems, there is no way to do this. Copies of argc and argv are not kept in global variables like environ is.
就是说,不行,虽然,这个command line arguments也是保存在全局变量空间,跟environments一样,但是我们没有一个像environ那样的全局指针可以使用,而只有main函数内部的形参argv,它并不是全局变量,因此,除了argv, argc,我们没有别的办法访问他们。
|
In the output from the size command at the end of , why aren't any sizes given for the heap and the stack? |
7.6的代码:
$ size /usr/bin/cc /bin/sh
text data bss dec hex filename
79606 1536 916 82058 1408a /usr/bin/cc
619234 21120 18260 658614 a 0cb6 /bin/sh
答案:heap和stack是在程序运新起来之后动态分配的,其大小不可能通过分析该可执行文件得到。
|
In , the two file sizes (475570 and 11410) don't equal the sums of their
respective text and data sizes. Why? |
7.7的代码:
$ cc -static hello1.c prevent gcc from using shared libraries
$ ls -l a.out
-rwxrwxr-x 1 sar 475570 Feb 18 23:17 a .out
$ size a.out
text data bss dec hex filename
375657 3780 3220 382657 5d6c 1 a .out
$ cc hello1.c gcc defaults to use shared libraries
$ ls -l a.out
-rwxrwxr-x 1 sar 11410 Feb 18 23:19 a .out
$ size a.out
text data bss dec hex filename
872 256 4 1132 46c a.out
答案:可执行文件是包含符号表的,它有助于我们利用它对core文件进行调试,可以用strip命令将其去掉。但是去掉后,其实还是不一样。