LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: sybaselu

知其然,必知其所以然

[复制链接]
 楼主| 发表于 2006-4-14 12:35:12 | 显示全部楼层

  1. char ch0;
  2. char *chP0 = NULL;
  3. char *chP1 = "love both you are";

  4. int main(void)
  5. {
  6.     static char ch1;
  7.     static char *chP1 = "I love you honey";

  8.     char *chP2 = NULL;
  9.     char *chP3 = (char *)malloc(sizeof(char));
  10.     chP3 = "you love me too";
  11.     char *chP4;
  12.     char  ch2;
  13.     static char ch3;

  14.     printf("sizeof char :%d\n", sizeof(char));


  15.     printf("&ch0  = 0x%x\n", &ch0);
  16.     printf("&chP0 = 0x%x\n", &chP0);
  17.     printf("&chP1 = 0x%x\n", &chP1);
  18.     printf("&ch1  = 0x%x\n", &ch1);
  19.     printf("&chP1 = 0x%x\n", &chP1);
  20.     printf("chP1  = 0x%x\n", chP1);
  21.     printf("*chP1 =   %c\n",  *chP1);
  22.     printf("&chP2 = 0x%x\n", &chP2);
  23.     printf("&chP3 = 0x%x\n", &chP3);
  24.     printf("chP3  = 0x%x\n", chP3);
  25.     printf("*chP3 =   %c\n", *chP3);
  26.     printf("&chP4 = 0x%x\n", &chP4);
  27.     printf("&ch2  = 0x%x\n", &ch2);
  28.     printf("&ch3  = 0x%x\n", &ch3);

  29.     return 0;
  30. }


  31. "address.c" 42L, 934C written
  32. [root@root GUI]# gcc address.c -o address
  33. [root@root GUI]# ./address
  34. sizeof char :1
  35. &ch0  = 0x8049814
  36. &chP0 = 0x804980c
  37. &chP1 = 0x8049804
  38. &ch1  = 0x8049810
  39. &chP1 = 0x8049804
  40. chP1  = 0x804860e
  41. *chP1 =   I
  42. &chP2 = 0xfee054b4
  43. &chP3 = 0xfee054b0
  44. chP3  = 0x804861f
  45. *chP3 =   y
  46. &chP4 = 0xfee054ac
  47. &ch2  = 0xfee054ab
  48. &ch3  = 0x8049811
  49. [root@root GUI]# size address
  50.    text    data     bss     dec     hex filename
  51.    1503     268      16    1787     6fb address
  52. [root@root GUI]#

复制代码

真的有点糊涂了,越用代码证实好像越找不出他们存放的规律,但是相信最终会完全透彻的吃掉她。
犯了一个大错误,把main() 中的变量放到非stack中考虑了。
回复 支持 反对

使用道具 举报

发表于 2006-4-14 16:56:13 | 显示全部楼层

迷途知返

As I understood, what you want to know is not the "principle of c" but "principle of cc". If you keep learning this kind of stuff, you will put yourself into deep water. If you were a student, it shoud be ok.
With the struggle of compiler developer, c and other high level languages, now, can let the programmers ONLY focus on their algorithm logic without worring about the low level details. In my opinions, You are running far away from the principle of c and any other high level languages.
That's it. I have answered some weird questions of you about bss, heap stack. BUT, Now, I don't think it's a good idea to mine deeper.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-14 17:46:34 | 显示全部楼层

  1. #include <stdlib.h>

  2. char *chP1;
  3. char *chP2 ="I love you ";

  4. static int  i1;
  5. static int  i2 = 10;

  6. static long l1;
  7. static long l2 = 10000;

  8. int main(void)
  9. {
  10.     char *chP3 = NULL;
  11.     chP3 = (char *)malloc(sizeof(char));

  12.     printf("&chP1(uninitial) = 0x%x\n", &chP1);
  13.     printf("&chP2(initial  ) = 0x%x\n", &chP2);
  14.     printf(" chP2            = 0x%x\n",  chP2);
  15.     printf("&i1(uninitial)   = 0x%x\n",   &i1);
  16.     printf("&i2(initial)     = 0x%x\n",   &i2);
  17.     printf("&l1(uninitial)   = 0x%x\n",   &l1);
  18.     printf("&l2(initial)     = 0x%x\n",   &l2);
  19.     printf(" \n");
  20.     printf("&chP3 = 0x%x\n", &chP3);
  21.     printf(" chP3 = 0x%x\n",  chP3);

  22.     return 0;
  23. }
  24. "address1.c" 30L, 669C written                                                
  25. [root@root GUI]# gcc address1.c
  26. [root@root GUI]# ./a.out
  27. &chP1(uninitial) = 0x8049784     //bss 84=80+4
  28. &chP2(initial  )   = 0x804976c     //data
  29. chP2                 = 0x8048588     //data
  30. &i1(uninitial)      = 0x804977c     //bss
  31. &i2(initial)          = 0x8049770     //data
  32. &l1(uninitial)      = 0x8049780     //bss 80=7c+4
  33. &l2(initial)          = 0x8049774     //data

  34. &chP3 = 0xfeea2d14                   //stack
  35. chP3 = 0x94be008                     //heap
  36. [root@root GUI]# size a.out
  37.    text    data     bss     dec     hex filename
  38.    1355     272      16    1643     66b a.out
  39. [root@root GUI]#

复制代码

bss=4+4+4+4(最后4个字节不知道哪里的)
data=272 ?(不知道怎么计算的,请指教)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-14 17:47:29 | 显示全部楼层
Ow, well ,great
What do you mean that "need not to know more about detail low level and focus on algorithm " , and what's different between principle of C and CC's ? answer is positive . BTW my currently job is do some works in embeded system, it's well known that there is very strick condition (include hardware and program) for this
回复 支持 反对

使用道具 举报

发表于 2006-4-14 18:24:35 | 显示全部楼层
littlepig的意思是,你现在讨论的完全是编译器的行为,跟C的关系不大。如果你想自己写一个编译器,那么这些知识才是需要的。
另外,指针就是一个整数,这个数就是一个地址。指针变量放在什么地方跟它所指向的内存在什么地方没有关系。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-14 18:42:46 | 显示全部楼层
Ok ,thanks so many people who are enthuziazm give me so advices and suggestions , however they are wrong or right, there is old saying that both listen you will be enlightened , as for how to distinguish about them, I think I have own criterial
回复 支持 反对

使用道具 举报

发表于 2006-4-15 04:33:13 | 显示全部楼层
你的基本概念可能有些混淆
你第一贴里说的“考虑到分段”,可能你是想说segment,也就是cs, ds这些段选择器所指向的段描述符所里所定义的段
但实际上,x86架构的Linux里用户层编程根本不需要考虑这个段概念,根本不分段,cs段,ds段是重叠的,0~4G。

刚才说的那个段,和text段,bss段里的段的概念,完全是两码事,text,bss段长度是不定的,不像segment
回复 支持 反对

使用道具 举报

发表于 2006-4-16 21:18:56 | 显示全部楼层
c语言学的最后,就是操作系统,硬件体系结构,编译器的知识了,否则,各种千奇百怪的错误
就没法解决。

楼主应该问一下自己,
栈式计算机懂不懂(大部分计算机,包括PC都是 stack machine)
不懂的话学习

编译器的基本知识有没有,
没有的话学习

其实楼主的问题基本都不是C语言的问题,都是在问编译器是如何实现静态变量,自动变量,以
及常量的分配的。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表