内存分配模式 - Linux

时间:2014-07-28 11:27:05

标签: linux

我对在linux系统中分配内存的模式非常困惑。要了解我制作了一个简单的程序,我已在下面列出:

main()
{  
 int a=5;
 char b='B';
 int c=10;
 int *p=NULL;
 char *q=NULL;
 int *r=NULL;
 p = new int;
 q = new char;
 r = new int;
 printf("\n The address of a : %p ",&a);
 printf("\n The address of b : %p ",&b);
 printf("\n The address of c : %p ",&c);
 int *p=NULL;
 int *q=NULL;
 p = new int;
 q = new int;
 printf("\n The address of a is %p ",&a);
 printf("\n The address of b is %p ",&b);
 printf("\n The address of p : %p ",&p);
 printf("\n The address of q : %p",&q);
 printf("\n The address at which new1 memory is allocated :  %p ",p);
 printf("\n The address at which new2 memory is allocated :  %p ",q);
 }

Red Hat Linux 7.3 X86 PC上的输出:

The address of a : 0xbffffa74
The address of b : 0xbffffa73
The address of c : 0xbffffa6c
The address of p : 0xbffffa68
The address of q : 0xbffffa64
The address of r : 0xbffffa60
The address at which new1 memory is allocated :  0x8050638
The address at which new2 memory is allocated :  0x8050648
The address at which new3 memory is allocated :  0x8050658

现在,下面是我的一些问题。请帮助我理解..

1.为什么堆栈上c和b之间有7个字节的差异?

2.为什么堆上分配的每个变量都有10个字节的差异?

3.无论什么时候我运行这个可执行文件,我都会得到相同的地址。这可能吗?

请帮助我理解这种行为..

0 个答案:

没有答案