Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3903351
  • 博文数量: 534
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4800
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(534)

文章存档

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(253)

2006年(73)

分类: C/C++

2011-04-24 10:39:06

[oracle@asm test]$ uname -a
Linux asm 2.6.9-22.EL #1 Mon Sep 19 18:20:28 EDT 2005 i686 i686 i386 GNU/Linux
[oracle@asm test]$ gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.4/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)
[oracle@asm test]$ cat a.c
#include

typedef struct
{

} ST_A;


int main(int argc, char **argv)
{
  int   c1;
  int   c2;
  ST_A  a;
  int   c3;

  printf("sizeof(ST_A) = %d\n", sizeof(ST_A));
  printf("sizeof(a)    = %d\n", sizeof(a));
  printf("address c1:%p, c2:%p a:%p, c3:%p\n", &c1, &c2, &a, &c3);

  return (0);
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++
[oracle@asm test]$ gcc -Wall -o a a.c
[oracle@asm test]$ ./a
sizeof(ST_A) = 0
sizeof(a)    = 0
address c1:0xbff5ea1c, c2:0xbff5ea18 a:0xbff5ea00, c3:0xbff5e9fc

来分析一下各个地址直接的关系:
c1地址 - 0x4  = c2地址    (0xbff5ea1c - 0x4  = 0xbff5ea18)
c2地址 - 0x18 = a地址    (0xbff5ea18 - 0x18 = 0xbff5ea00)
a地址  - 0x4  = c3地址    (0xbff5ea00 - 0x4  = 0xbff5e9fc)

--------   0xbff5ea1c  = A1
c1地址
--------   A1 - 0x4    = A2    
c2地址
--------   A2 - 0x18   = A3
a地址
--------   A3 - 0x4
c3地址
--------

但为什么是0x18呢?不是sizeof计算的为0吗?

++++++++++++++++++++++++++++++++++++++++++++++++++++++
[oracle@asm test]$ gcc  -o a a.c        
[oracle@asm test]$ ./a
sizeof(ST_A) = 0
sizeof(a)    = 0
address c1:0xbff0120c, c2:0xbff01208 a:0xbff011f0, c3:0xbff011ec
这个应该和添加了-Wall结果一样了。

++++++++++++++++++++++++++++++++++++++++++++++++++++++
做优化编译处理看看:
[oracle@asm test]$ gcc -Wall -O2 -o a a.c
[oracle@asm test]$ ./a
sizeof(ST_A) = 0
sizeof(a)    = 0
address c1:0xbfe0e874, c2:0xbfe0e878 a:0xbfe0e880, c3:0xbfe0e87c

来分析看看:
c1地址 + 0x4 = c2地址
c2地址 + 0x8 = a地址
c2地址 + 0x4 = c3地址
c3地址 + 0x4 = a地址

--------   0x0xbfe0e880  = A1
a地址
--------   A1 - 0x4      = A2    
c3地址
--------   A2 - 0x4      = A3
c2地址
--------   A3 - 0x4
c1地址
--------

++++++++++++++++++++++++++++++++++++++++++++++++++++++
[oracle@asm test]$ gcc -Wall -O -o a a.c
[oracle@asm test]$ ./a
sizeof(ST_A) = 0
sizeof(a)    = 0
address c1:0xbff52db4, c2:0xbff52db8 a:0xbff52dc0, c3:0xbff52dbc
这个应该和-O2结果一样。


这样的解释不知道对不对:(ref:http://blogold.chinaunix.net/u/19782/showart_2533791.html)
堆与栈都是C语言中的动态内存,在内存管理方向,栈内存由编译器管理,而堆内存由程序调用C语言的基本库函数管理。

栈内存的使用在很大程度上依赖于处理器硬件机制,在处理器中,一般有一个寄存器来表示当前栈指针的位置,通常在内存中分配一块区域,这块内存的上界与下界之间是可用的栈内存区域。

栈内存的两种增长方式:一种是向上增长,即从低地址向高址增长,另一种向下增长,即从高地址向低地址增长。[咱们看到gcc没有优化编译的是从高地址到低地址分配,优化后结果反过来了。]
为什么是0x18呢?感觉这个问题应该了解gcc了。
阅读(1359) | 评论(0) | 转发(0) |
0

上一篇:堆与栈

下一篇:SQL joins用法

给主人留下些什么吧!~~