文章程式碼顯示

2019年9月1日 星期日

《筆記》C語言 - 補充_9: calloc 與普通的變數在內部儲存的方式 (堆(heap) & 棧 (stack) )

1、棧區(stack): 放置為由高地址到低地址
由編譯器(系統)自動分配釋放 ,存放函數的參數值,局部變量的值等。其操作方式類似於數據結構中的棧。

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;
}




參考連結
堆和棧的區別
堆栈,堆栈,堆和栈的区别
程序的运行时 数据结构



我的 Youtube 頻道,一定要訂閱
我將定期推出程式語言的新手教學影片


↓↓↓ 連結到部落格方針與索引 ↓↓↓

Blog 使用方針與索引