现在的位置: 首页 > 综合 > 正文

堆中分配的变量地址虽有一定规律,但整体上是无规律的

2018年04月03日 ⁄ 综合 ⁄ 共 367字 ⁄ 字号 评论关闭

C code:

 

#include<stdio.h>
#include<stdlib.h>

typedef struct MyNode
{
 int v;
 struct MyNode * next;
}Node;

void main()
{
 Node * h1, *h2, *h3;
 h1 = malloc(sizeof(Node));
 h2 = malloc(sizeof(Node));
 h3 = malloc(sizeof(Node));
 printf("%X/t%X/t%X/n", h1, h2, h3);
 free(h1);
 free(h2);
 free(h3);
 h1 = h2 = h3 = NULL;
}

 

 

Running result:

 

430070  430030  431E90

 

看到了吧,地址由大变小,又由小变大,而且地址间的差距无法预料。如果你输出十几个,也许你会找到一点点的规律吧!

抱歉!评论已关闭.