Skip to content

Instantly share code, notes, and snippets.

@sun4lower
Last active March 17, 2018 11:20
Show Gist options
  • Save sun4lower/bcee7e8e834f50a344ae958fc9e72b74 to your computer and use it in GitHub Desktop.
Save sun4lower/bcee7e8e834f50a344ae958fc9e72b74 to your computer and use it in GitHub Desktop.
The memory in computer
#include <stdio.h>
int main()
{
int a = 3;
int b = 2;
int array[3];
array[0] = 1;
array[1] = 10;
array[2] = 100;
int *pa = &a;
int i;
for(i = 0; i < 6; i++) {
printf("*pa = %d\n", *pa);
pa++;
}
printf("------------------------------------\n");
pa = &a;
for(i = 0; i<6; i++) {
printf("p[%d] = %d\n", i, pa[i]);
}
}
// 0xffffffffffffffff 00000000 | 系统内核 |
// ... ... | |
// ----------------------------------------------------------
// 0x7fffffffffffffff 00000000 | |
// 0x7ffffffffffffffe 00000000 | 栈 |
// 0x7ffffffffffffffd 00000000 ------------------------
// 0x7ffffffffffffffc 00000000 | |
// 0x7ffffffffffffffb 00000000 | 自由可分配内存 |
// 0x7ffffffffffffffa 00000000 | |
// 0x7ffffffffffffff9 00000000 ------------------------
// 0x7ffffffffffffff8 00000000 | 堆 |
// 0x7ffffffffffffff7 00000000 ------------------------
// ... ... | | 全局变量
// 0x2 00000000 | 数据段 | 常量、静态变量
// 0x1 00000000 ------------------------
// 0x0 00000000 | 代码段 | p &函数名
// ----------------------------------------------------------
//同一类型变量的声明放在一起(编译器的优化)
//为什么这样会让程序运行更快呢?
//静态变量、局部变量、return
#include <stdio.h>
int global = 0;
int rect(int a, int b)
{
static int count = 0;
count++;
global++;
int s = a * b;
return s;
}
int quadrate(int a)
{
static int count = 0;
count++;
global++;
int s = rect(a, a);
return s;
}
int main()
{
int a = 3;
int b = 4;
int *pa = &a;
int *pb = &b;
int *pglobal = &global;
int (*pquadrate)(int a) = &quadrate;
int s = quadrate(a);
printf("%d\n", s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment