由編譯器(系統)自動分配釋放 ,存放函數的參數值,局部變量的值等。其操作方式類似於數據結構中的棧。
2、堆區(heap): 放置為由低地址到高地址 *往上堆*
一般由程序設計師分配釋放, 若程序設計師不釋放,程序結束時可能由 OS 回收 。
注意它與數據結構中的堆是兩回事,分配方式類似於鍊結。
3、全局區(靜態區)(static)
全局變量和靜態變量的存儲是放在一塊的,初始化的全局變量和靜態變量在一塊區域, 未初始化的全局變量和未初始化的靜態變量在相鄰的另一塊區域。 程序結束後有系統釋放
#include "stdio.h"
#include "stdlib.h"
int numer_0_1;
int numer_0_2;
int numer_1_1 = 123;
int numer_1_2 = 456;
static int numer_2_1;
static int numer_2_2;
static int numer_3_1 = 213;
static int numer_3_2 = 555;
typedef struct _Student{
int No;
int phone_number;
}Student, *PTR_Student;
int main()
{
PTR_Student Amy = NULL;
Amy = calloc(sizeof(Student) ,1);
printf("[calloc]\r\n");
Amy->No=1;
Amy->phone_number=123;
printf("%d, %d \r\n", Amy->No, Amy->phone_number);
printf("The Address of Amy = 0x%x\r\n", Amy);
printf("The Address of Amy->No = 0x%x\r\n", &(Amy->No) );
printf("The Address of Amy->phone_number = 0x%x \r\n\n", &(Amy->phone_number) );
printf("[variables]\r\n");
printf("Global variables\r\n");
printf("The address of numer_0_1 0x%x \r\n", &numer_0_1);
printf("The address of numer_0_2 0x%x \r\n", &numer_0_2);
printf("The address of numer_1_1 0x%x \r\n", &numer_1_1);
printf("The address of numer_1_2 0x%x \r\n", &numer_1_2);
printf("Global static variables\r\n");
printf("The address of numer_2_1 0x%x \r\n", &numer_2_1);
printf("The address of numer_2_2 0x%x \r\n", &numer_2_2);
printf("The address of numer_3_1 0x%x \r\n", &numer_3_1);
printf("The address of numer_3_2 0x%x \r\n", &numer_3_2);
return 0;
}
#include "stdio.h"
#include "stdlib.h"
typedef struct _Student{
int No;
int phone_number;
}Student, *PTR_Student;
int main()
{
PTR_Student Amy = NULL;
Amy = calloc(sizeof(Student) ,1);
printf("[calloc]\r\n");
Amy->No=1;
Amy->phone_number=123;
printf("%d, %d \r\n", Amy->No, Amy->phone_number);
printf("The Address of Amy = 0x%x\r\n", Amy);
printf("The Address of Amy->No = 0x%x\r\n", &(Amy->No) );
printf("The Address of Amy->phone_number = 0x%x \r\n\n", &(Amy->phone_number) );
int numer_0_1;
int numer_0_2;
int numer_1_1 = 123;
int numer_1_2 = 456;
static int numer_2_1;
static int numer_2_2;
static int numer_3_1 = 213;
static int numer_3_2 = 555;
printf("[variables]\r\n");
printf("Local variables\r\n");
printf("The address of numer_0_1 0x%x \r\n", &numer_0_1);
printf("The address of numer_0_2 0x%x \r\n", &numer_0_2);
printf("The address of numer_1_1 0x%x \r\n", &numer_1_1);
printf("The address of numer_1_2 0x%x \r\n", &numer_1_2);
printf("Local static variables\r\n");
printf("The address of numer_2_1 0x%x \r\n", &numer_2_1);
printf("The address of numer_2_2 0x%x \r\n", &numer_2_2);
printf("The address of numer_3_1 0x%x \r\n", &numer_3_1);
printf("The address of numer_3_2 0x%x \r\n", &numer_3_2);
return 0;
}
參考連結
堆和棧的區別
堆栈,堆栈,堆和栈的区别
程序的运行时 数据结构
我將定期推出程式語言的新手教學影片