Thuta Learning
C
AdvancedProgrammingအခြေခံ

Memory Address

Cသင်ခန်းစာ 2525 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • & နဲ့ variable တစ်ခုရဲ့ address ကို ယူပြီး %p နဲ့ (void *) cast သုံးကာ မှန်မှန်ကန်ကန် print ထုတ်နိုင်ဖို့
  • sizeof နဲ့ object တစ်ခု memory ထဲမှာ byte ဘယ်နှစ်ခု ယူထားလဲ တွက်နိုင်ဖို့
  • Address နံပါတ်အစစ်ကို မမှီခိုဘဲ array element တွေကြား အကွာအဝေးကို တိုင်းနိုင်ဖို့
  • Object အသက်ရှင်နေသရွေ့ပဲ သူ့ address ကို သုံးလို့ရတဲ့ အကြောင်းကို ရှင်းပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Memory ကို နံပါတ်တပ်ထားတဲ့ locker တွေ အရှည်ကြီး တန်းစီထားတဲ့ လမ်းတစ်လမ်းလို့ မြင်ကြည့်ပါ၊ locker တစ်လုံးက byte တစ်ခုပါ။ int age = 43; လို့ ကြေညာလိုက်တဲ့အခါ compiler က ဘေးချင်းကပ်နေတဲ့ locker အနည်းငယ်ကို (အခု သုံးနေကြတဲ့ PC တွေမှာ လေးလုံး) ယူထားပြီး 43 ကို အဲဒီထဲ ရေးထည့်ပါတယ်။ ပထမဆုံး locker ရဲ့ နံပါတ်က အဲဒီ variable ရဲ့ memory address ပါ။

C မှာ အဲဒီ နံပါတ်ကို & operator နဲ့ တောင်းလို့ရပါတယ် — &age ဆိုတာ "age ရဲ့ address" လို့ ဆိုလိုပါတယ်။ နံပါတ်အတိအကျကို ဂရုစိုက်ရတာ ရှားပါတယ်။ အရေးကြီးတာက address ရှိရင် တခြား code က အဲဒီ variable တစ်ခုတည်းကို ပြန်ရှာတွေ့နိုင်တာပါ — scanf() က ကိုယ့် variable ထဲ value ဖြည့်ပေးနိုင်တာ၊ နောက် lesson က pointer တွေ အလုပ်လုပ်တာ အဲဒီ အချက်ကြောင့်ပါ။

Problem → Why → Solution

ပြဿနာ: scanf() က & မပါရင် ဘာလို့ ပျက်သွားတာလဲ

User Input lesson မှာ scanf("%d", &age); လို့ ရေးခဲ့ပြီး & က ဘာအတွက်လဲလို့ တွေးမိခဲ့မှာပါ။ & ကို ဖြုတ်လိုက်ရင် program က compile ဖြစ်နေတုန်းပါပဲ၊ ဒါပေမယ့် နံပါတ်တစ်ခု ရိုက်ထည့်လိုက်တာနဲ့ ပုံမှန်အားဖြင့် crash ဖြစ်ပါတယ် — ဒါမှမဟုတ် ပိုဆိုးတာက memory ရဲ့ တခြားနေရာ တစ်နေရာမှာ အသံမထွက်ဘဲ ရေးမိသွားပါတယ်။ GCC ကို -Wall နဲ့ သုံးရင်တော့ format '%d' expects argument of type 'int *', but argument 2 has type 'int' ဆိုပြီး warning ပေးပါတယ်။

read_age.c
#include <stdio.h>

int main(void) {
    int age = 0;
    printf("Enter your age: ");
    scanf("%d", age);          /* BUG: passes the value 0, not where age lives */
    printf("You are %d\n", age);
    return 0;
}

scanf() က ဘာကြောင့် address လိုတာလဲ

C က argument တွေကို value အနေနဲ့ ပို့ပါတယ်၊ ဒါကြောင့် scanf("%d", age) က age ထဲမှာ လောလောဆယ် ရှိနေတဲ့ နံပါတ်ရဲ့ copy (ဒီမှာ 0) ကိုပဲ scanf ဆီ ပေးလိုက်တာပါ၊ variable ကိုယ်တိုင်ကို မဟုတ်ပါဘူး။ Copy တစ်ခုထဲ ရလဒ်ကို ပြန်ထည့်ပေးဖို့ scanf မှာ နည်းလမ်း မရှိပါဘူး။ ပြောင်းထားတဲ့ နံပါတ်ကို age ရဲ့ byte တွေထဲ တိုက်ရိုက် ရေးနိုင်ဖို့ age ဘယ်မှာ နေလဲ သိဖို့ လိုပါတယ်။ 0 ဆိုတဲ့ value ကို ပို့လိုက်တော့ scanf က 0 ကို address လို့ ယူဆပြီး အဲဒီမှာ ရေးပါတယ် — ဒါက undefined behaviour ပါ။

Core concept: address, sizeof နဲ့ layout

Run နေတဲ့ C program ထဲက object တိုင်း — variable ဖြစ်ဖြစ်၊ array ဖြစ်ဖြစ်၊ struct ဖြစ်ဖြစ် — memory ထဲမှာ byte တွေ ဆက်တိုက် နေရာယူထားပြီး သူ့ address က အဲဒီ byte တွေထဲက ပထမ byte ရဲ့ တည်နေရာပါ။ Unary & operator က အဲဒီ address ကို ထုတ်ပေးပါတယ်။ x ရဲ့ type က int ဆိုရင် &x ရဲ့ type က int * ("pointer to int" လို့ ဖတ်ပါ) ဖြစ်ပါတယ်။ ဒါကြောင့် address က တည်နေရာနဲ့အတူ type အချက်အလက်ကိုပါ သယ်လာပါတယ် — အဲဒီကနေ စတဲ့ byte လေးခုထဲမှာ int တစ်ခု ရှိတယ်ဆိုတာ compiler သိပါတယ်။

sizeof က & ရဲ့ အဖော်ပါ။ Object သို့မဟုတ် type တစ်ခု byte ဘယ်နှစ်ခု ယူလဲဆိုတာကို size_t type value အနေနဲ့ ပြောပြပြီး printf မှာ %zu နဲ့ print ထုတ်ပါတယ်။ ဒီနေ့ 64-bit Windows နဲ့ Linux PC တွေမှာ char က 1 byte၊ int က 4၊ double က 8 ဖြစ်ပြီး pointer တိုင်းက 8 byte ပါ။ Standard က 1 လို့ တိတိကျကျ သတ်မှတ်ထားတာ char တစ်ခုပဲ ရှိပါတယ်၊ ကျန်တာတွေက platform အလိုက် compiler ရွေးထားတာမို့ ခန့်မှန်းမနေဘဲ sizeof ကို print ထုတ်ကြည့်ပါ။

Address ကို print ထုတ်ချင်ရင် %p conversion ကို သုံးပြီး pointer ကို (void *) လို့ cast လုပ်ပါ၊ %p က void * အတွက်ပဲ သတ်မှတ်ထားလို့ပါ။ Output က Linux မှာ 0x7ffe5367e044 လိုမျိုး၊ Windows ပေါ်က GCC မှာ 0x မပါတဲ့ hex digit ၁၆ လုံးလိုမျိုး ပေါ်ပါတယ် — ပုံစံရော နံပါတ်ကိုယ်တိုင်ပါ implementation ပေါ် မူတည်ပါတယ်။ ခေတ်သစ် operating system တွေက stack နဲ့ heap စတဲ့နေရာကို random ပြောင်းထားတာ (address space layout randomization) ကြောင့် program တစ်ခုတည်းကို run တိုင်း address မတူတာ များပါတယ်။ Print ထုတ်ထားတဲ့ address ကို debugging အတွက် သဲလွန်စ တစ်ခုလို့ပဲ သဘောထားပါ၊ program က မှီခိုရမယ့် data လို့ ဘယ်တော့မှ မသုံးပါနဲ့။

အားကိုးလို့ရတာကတော့ array တစ်ခုတည်းရဲ့ အတွင်း layout ပါ။ Element တွေကို ကြားမှာ အပေါက်မရှိဘဲ ဘေးချင်းကပ် သိမ်းထားလို့ &scores[1] က &scores[0] ထက် sizeof scores[0] byte အတိအကျ နောက်မှာ ရှိပြီး element နှစ်ခုရဲ့ address ကို နုတ်လိုက်ရင် ကြားက element အရေအတွက် ရပါတယ်။ သီးခြား variable တွေကတော့ မတူပါဘူး — compiler က ကြိုက်တဲ့ အစီအစဉ်၊ ကြိုက်တဲ့ အကွာအဝေးနဲ့ ထားနိုင်ပါတယ်၊ ဆက်စပ်မှုမရှိတဲ့ object နှစ်ခုကို < လို အစီအစဉ် နှိုင်းယှဉ်တာကိုလည်း standard က မသတ်မှတ်ထားပါဘူး။

text
int scores[3] = {70, 85, 92};  (int is 4 bytes)
-----------------------------------------------
byte offset:  0       4       8       12
              +-------+-------+-------+
              |  70   |  85   |  92   |
              +-------+-------+-------+
              ^       ^       ^
        &scores[0] &scores[1] &scores[2]

(char *)&scores[1] - (char *)&scores[0]  ->  4   (bytes)
&scores[2] - &scores[0]                  ->  2   (elements)
မှတ်ထားရမယ့် ပုံစံ: လမ်းပေါ်က locker များ
  1. ကြေညာတာက byte အတန်းလိုက် ယူထားတာ
  2. &name က ပထမ byte ရဲ့ address ပေး
  3. sizeof က byte ဘယ်နှစ်ခု ယူလဲ ပြော
  4. Object အသက်ရှင်စဉ်မှာပဲ address သုံးရ

Array တစ်ခုအတွင်းက အကွာအဝေးကို အားကိုးပါ၊ %p ထုတ်ပေးတဲ့ နံပါတ်အတိအကျကို ဘယ်တော့မှ မအားကိုးပါနဲ့။

အထွေထွေ ပုံစံ

c
type name = value;
type *where = &name;               /* address of name; its type is type * */

printf("%p\n", (void *)&name);     /* print an address (varies per run) */
printf("%zu\n", sizeof name);      /* bytes the object occupies */

ptrdiff_t gap = &arr[j] - &arr[i]; /* elements between two items, use %td */

& က variable ရဲ့ type ကို ညွှန်တဲ့ pointer type ရှိတဲ့ address ကို ပေးပါတယ်။ %p က (void *) cast ပြီးမှ address ကို print ထုတ်ပြီး %zu က sizeof ရလဒ်ကို print ထုတ်ပါတယ်။ Array တစ်ခုတည်းရဲ့ element address နှစ်ခုကို နုတ်ရင် ptrdiff_t ရပြီး %td နဲ့ print ထုတ်ပါတယ်။

Example: address နံပါတ် မပြဘဲ memory ကို စူးစမ်းခြင်း

ဒီ program က address နံပါတ်အစစ်ကို တစ်ခါမှ print မထုတ်ပါဘူး၊ ဒါကြောင့် ဘယ်နှစ်ကြိမ် run run၊ ဘယ် 64-bit PC မှာ run run output တူတူပဲ ထွက်ပါတယ်။ အဲဒီအစား အားကိုးလို့ရတဲ့ အချက်တွေကို ပြပါတယ် — object တစ်ခုစီ ဘယ်လောက် ကြီးလဲ၊ &age က age ကို တကယ် ညွှန်ပြလား၊ array element တွေ ဘယ်လောက် အကွာမှာ ရှိလဲ။

int က 2 byte ပဲ ရှိတဲ့ platform (microcontroller တချို့) မှာ run ရင် byte အရေအတွက်တွေ ပြောင်းသွားပေမယ့် element အကွာအဝေး 2 ကတော့ မပြောင်းပါဘူး။

memory_address.c
#include <stdio.h>

int main(void) {
    int age = 43;
    int scores[3] = {70, 85, 92};

    printf("age = %d, stored in %zu bytes\n", age, sizeof age);
    printf("scores uses %zu bytes for 3 ints\n", sizeof scores);

    /* &age means "the address of age"; its type is int * */
    int *where = &age;
    printf("where holds the address of age: %s\n", where == &age ? "yes" : "no");

    /* Array elements sit side by side, so distances are predictable */
    char *first = (char *)&scores[0];
    char *second = (char *)&scores[1];
    printf("bytes from scores[0] to scores[1]: %td\n", second - first);
    printf("elements from scores[0] to scores[2]: %td\n", &scores[2] - &scores[0]);
    return 0;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 7sizeof age

    64-bit Windows နဲ့ Linux မှာ 4 ရပါတယ်။ sizeof ရလဒ်က size_t ဖြစ်လို့ printf မှာ %zu နဲ့ တွဲပါတယ်။

  2. Line 8sizeof scores

    Array အပေါ် sizeof သုံးရင် array တစ်ခုလုံးကို တိုင်းပါတယ် — element 3 ခု × 4 byte = 12။ Array ကိုယ်တိုင် မြင်ရတဲ့ scope ထဲမှာပဲ ဒီလို အလုပ်လုပ်ပြီး pointer ပေါ်မှာ ဒီအဖြေ မရပါဘူး။

  3. Line 11int *where = &age;

    &age က age ရဲ့ address ကို ထုတ်ပေးပြီး အဲဒါကို သိမ်းဖို့ int * type variable လိုပါတယ်။ ဒီလို variable တွေအကြောင်းကို နောက် lesson မှာ အသေးစိတ် လေ့လာပါမယ်။

  4. Line 12where == &age

    Address နှစ်ခုကို == နဲ့ နှိုင်းယှဉ်တာ အမြဲ ခွင့်ပြုပြီး object တစ်ခုတည်းကို ညွှန်လား ပြောပြပါတယ်။ နံပါတ်ကို မပြဘဲ yes ထွက်ပါတယ်။

  5. Lines 15-17(char *)&scores[1]

    char * ကို cast လုပ်လိုက်တော့ နုတ်တဲ့အခါ byte နဲ့ ရေတွက်ပါတယ်၊ char က 1 byte မို့ပါ။ ရလဒ်က int တစ်ခုရဲ့ အရွယ်အစား 4 ပါ။

  6. Line 18&scores[2] - &scores[0]

    int address နှစ်ခုကို နုတ်ရင် byte မဟုတ်ဘဲ element အရေအတွက်ကို ရေတွက်လို့ အဖြေက 2 ပါ။ ရလဒ် type က ptrdiff_t ဖြစ်ပြီး %td နဲ့ print ထုတ်ပါတယ်။

You should see
age = 43, stored in 4 bytes
scores uses 12 bytes for 3 ints
where holds the address of age: yes
bytes from scores[0] to scores[1]: 4
elements from scores[0] to scores[2]: 2

%p နဲ့ address အစစ်ကို ကြည့်ခြင်း

Debugging လုပ်နေတုန်း address အစစ်ကို တကယ် မြင်ချင်ရင် %p ကို သုံးပါ။ အောက်က program ကို နှစ်ကြိမ် run ကြည့်ပြီး output ကို နှိုင်းယှဉ်ပါ။ နောက်က sample output က Windows ပေါ်က GCC နဲ့ run ထားတာပါ — ကိုယ့်စက်မှာ နံပါတ်တွေ မတူတာ ပုံမှန်ပါပဲ။

show_address.c
#include <stdio.h>

int main(void) {
    int age = 43;
    double price = 9.99;

    printf("age   lives at %p\n", (void *)&age);
    printf("price lives at %p\n", (void *)&price);
    return 0;
}
sample output (two runs)
age   lives at 000000f02a9ffa6c
price lives at 000000f02a9ffa60

age   lives at 000000b7f37ff7ac
price lives at 000000b7f37ff7a0

Run နှစ်ကြိမ်လုံးမှာ price က age ထက် 12 byte အောက်မှာ ရှိနေတာကို သတိထားမိပါလိမ့်မယ်။ ဒါက ဒီ compiler နဲ့ ဒီ option တွေနဲ့ ရွေးချယ်ထားတဲ့ layout တစ်ခုပဲ ဖြစ်ပါတယ်၊ ကတိ မဟုတ်ပါဘူး — -O2 နဲ့ compile လုပ်တာ၊ compiler ပြောင်းတာနဲ့ အစီအစဉ်ပါ ပြောင်းသွားနိုင်ပါတယ်။

Lifetime: address တစ်ခု ဘယ်အချိန်မှာ မမှန်တော့လဲ

Address တစ်ခုက သူ ညွှန်ထားတဲ့ object အသက်ရှင်နေသရွေ့ပဲ အသုံးဝင်ပါတယ်။ C မှာ object တစ်ခု ဘယ်လောက်ကြာကြာ နေလဲဆိုတာ storage duration သုံးမျိုးက ဆုံးဖြတ်ပါတယ်။

Object အမျိုးအစားဘယ်အချိန်ထိ နေလဲ
Function ထဲက local variable (automatic)သူ့ကို ကြေညာထားတဲ့ block { } ပြီးသွားတဲ့အထိ
Global variable သို့မဟုတ် static local (static)Program တစ်ခုလုံး run နေသရွေ့
malloc() နဲ့ ယူထားတဲ့ memory (allocated)free() ခေါ်တဲ့အထိ (Dynamic Memory lesson)

Function ပြီးသွားတာနဲ့ သူ့ local variable တွေရဲ့ memory ကို နောက် function call တွေက ပြန်သုံးပါတယ်။ ဒါကြောင့် local variable ရဲ့ address ကို return ပြန်လိုက်ရင် caller လက်ထဲမှာ ပိုင်ရှင်မရှိတဲ့ locker နံပါတ်တစ်ခုပဲ ကျန်ပါတယ်။ GCC က ဒီလို code ကို function returns address of local variable [-Wreturn-local-addr] ဆိုပြီး သတိပေးပါတယ်။ ကြာကြာ နေရမယ့် data လိုရင် value ကို return ပြန်ပါ၊ caller ပိုင်တဲ့ variable ရဲ့ address ကို လက်ခံပါ၊ ဒါမှမဟုတ် malloc() ကို သုံးပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Pointer များတဲ့ code မရေးခင်ကတည်းက address တွေနဲ့ တွေ့နေရပါပြီ။ scanf() call တိုင်း၊ parameter ကနေ ရလဒ်ကို ဖြည့်ပေးတဲ့ function တိုင်း (ဥပမာ end pointer ပါတဲ့ standard strtol()) နဲ့ function ဆီ ပို့လိုက်တဲ့ array တိုင်းဟာ နောက်ကွယ်မှာ address တွေနဲ့ အလုပ်လုပ်ပါတယ်။ gdb နဲ့ Visual Studio debugger လို debugger တွေက variable ဘေးမှာ address ကို ပြပေးလို့ နာမည်နှစ်ခုက object တစ်ခုတည်းကို ညွှန်နေလား ခွဲခြားနိုင်ပါတယ်။

Address ကို print ထုတ်တာက diagnostic tool တစ်ခုပါ။ Program crash ဖြစ်တဲ့အခါ AddressSanitizer လို tool တွေက (ဥပမာ Linux ပေါ်က GCC နဲ့ Clang မှာ -fsanitize=address၊ MSVC ရဲ့ recent version တွေမှာလည်း ရှိပါတယ်) မှားနေတဲ့ address ကို အဲဒီ memory ကို ဘယ်မှာ allocate လုပ်ခဲ့လဲ၊ ဘယ်မှာ free လုပ်ခဲ့လဲဆိုတာနဲ့အတူ report လုပ်ပေးပါတယ်။ %p value တွေကို log ထုတ်ကြည့်ရင် struct နှစ်ခုက buffer တစ်ခုတည်းကို မျှသုံးနေတာမျိုး တွေ့နိုင်ပေမယ့် production code ကတော့ print ထုတ်ထားတဲ့ address string ကို သိမ်းတာ၊ နှိုင်းယှဉ်တာ ဘယ်တော့မှ မလုပ်သင့်ပါဘူး၊ run တိုင်း ပြောင်းလို့ပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Address ကို %d သို့မဟုတ် %x နဲ့ print ထုတ်တာ

64-bit PC မှာ address က 8 byte ရှိပြီး int က 4 byte ပဲ ရှိပါတယ်။ printf("%d", &n) က undefined behaviour ဖြစ်ပြီး လက်တွေ့မှာ ဖြတ်တောက်ထားတဲ့ အဓိပ္ပာယ်မဲ့ နံပါတ်တစ်ခု ထွက်ပါတယ်။ GCC က format '%d' expects argument of type 'int', but argument 2 has type 'int *' လို့ သတိပေးပါတယ်။ %p နဲ့ (void *) cast ကို သုံးပါ။

2. Variable တွေရဲ့ address အစီအစဉ်ကို အဓိပ္ပာယ်ရှိတယ် ထင်တာ

Build တစ်ခုမှာ price က age ထက် 12 byte အောက်မှာ ရှိနေလို့ တခြား compiler၊ optimization level ဒါမှမဟုတ် platform မှာလည်း အဲလို ဖြစ်မယ်လို့ မဆိုနိုင်ပါဘူး။ Array (သို့မဟုတ် struct) တစ်ခုတည်းအတွင်းက address တွေမှာပဲ အာမခံထားတဲ့ ဆက်စပ်မှု ရှိပါတယ်။

3. Function return ပြီးမှ local variable ရဲ့ address ကို ဆက်သုံးတာ

int *make_counter(void) { int count = 0; return &count; } လို function က function ပြီးတာနဲ့ ပြန်လွှတ်ပေးလိုက်တဲ့ memory ရဲ့ address ကို ပေးပါတယ်။ အဲဒီ address ကို သုံးတာက undefined behaviour ပါ — အစပိုင်းမှာ အလုပ်လုပ်သလို ထင်ရပြီး မဆိုင်တဲ့ ပြောင်းလဲမှုတစ်ခု လုပ်ပြီးမှ ပျက်သွားတတ်ပါတယ်။ -Wreturn-local-addr warning ကို error တစ်ခုလို သဘောထားပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • -Wall -Wextra နဲ့ compile လုပ်ပါ၊ address နဲ့ ဆိုင်တဲ့ format/argument မကိုက်ညီမှုတွေကို compiler က ပြပေးပါတယ်။
  • 4 ဒါမှမဟုတ် 8 လို အရွယ်အစားတွေကို code ထဲ hard-code မလုပ်ဘဲ sizeof ကို သုံးပါ။
  • Address ကို object တစ်ခုတည်းလား စစ်ဖို့ (==) နဲ့ array တစ်ခုအတွင်း အကွာအဝေး တိုင်းဖို့ပဲ သုံးပါ၊ မဆိုင်တဲ့ variable တွေ အစီအစဉ်အတွက် မသုံးပါနဲ့။

Mini challenge

Mini challenge: array သုံးခုရဲ့ memory map

Array သေးသေး သုံးခု memory ထဲမှာ ဘယ်လို စီထားလဲဆိုတာကို address နံပါတ်အစစ် မသုံးဘဲ အရွယ်အစားနဲ့ အကွာအဝေးတွေနဲ့ report လုပ်တဲ့ program တစ်ခု ရေးပါ။

  • char[4], int[4], double[4] array တစ်ခုစီ ကြေညာရမယ်
  • label၊ element အရွယ်အစား၊ array တစ်ခုလုံး အရွယ်အစားနဲ့ element 0 ကနေ 3 ထိ byte အကွာအဝေးကို print ထုတ်တဲ့ report() helper ရေးရမယ်
  • Element address တွေကို char * သို့ cast လုပ်ပြီး နုတ်ကာ byte အကွာအဝေးကို တွက်ရမယ်
  • နောက်ဆုံးမှာ int array အတွက် &counts[3] - &counts[0] element အကွာအဝေးကို print ထုတ်ရမယ်
memory_map.c
#include <stddef.h>
#include <stdio.h>

static void report(const char *label, size_t element, size_t total, ptrdiff_t span) {
    // TODO: print one aligned line, e.g. "int   : element 4, array 16, ..."
}

int main(void) {
    // TODO: declare char letters[4], int counts[4] and double prices[4]

    // TODO: call report() once per array: sizes come from sizeof,
    //       the span from (char *)&arr[3] - (char *)&arr[0]

    // TODO: print &counts[3] - &counts[0] with %td
    return 0;
}

ရလဒ်: 64-bit Windows သို့မဟုတ် Linux PC မှာ ဒီလို ထွက်ရပါမယ်: char : element 1, array 4, [0] to [3] = 3 bytes int : element 4, array 16, [0] to [3] = 12 bytes double: element 8, array 32, [0] to [3] = 24 bytes &counts[3] - &counts[0] = 3 elements

အရိပ်အမြွက် ကြည့်ရန်

sizeof letters[0] က element တစ်ခုရဲ့ အရွယ်အစားဖြစ်ပြီး sizeof letters က array တစ်ခုလုံးပါ။ Span အတွက် (char *)&letters[3] - (char *)&letters[0] က char 1 byte ဖြစ်လို့ byte နဲ့ ရေတွက်ပါတယ်။ ptrdiff_t ကို %td နဲ့၊ size_t ကို %zu နဲ့ print ထုတ်ပါ။ Label ကို %-6s နဲ့ ညှိနိုင်ပါတယ်။

အဖြေ ကြည့်ရန်
memory_map.c
#include <stddef.h>
#include <stdio.h>

static void report(const char *label, size_t element, size_t total, ptrdiff_t span) {
    printf("%-6s: element %zu, array %zu, [0] to [3] = %td bytes\n",
           label, element, total, span);
}

int main(void) {
    char letters[4] = {'a', 'b', 'c', 'd'};
    int counts[4] = {1, 2, 3, 4};
    double prices[4] = {1.5, 2.5, 3.5, 4.5};

    report("char", sizeof letters[0], sizeof letters,
           (char *)&letters[3] - (char *)&letters[0]);
    report("int", sizeof counts[0], sizeof counts,
           (char *)&counts[3] - (char *)&counts[0]);
    report("double", sizeof prices[0], sizeof prices,
           (char *)&prices[3] - (char *)&prices[0]);

    printf("&counts[3] - &counts[0] = %td elements\n", &counts[3] - &counts[0]);
    return 0;
}

Quick Quiz 1 / 3

int n; ရဲ့ address ကို ဘယ် printf call က မှန်မှန်ကန်ကန် print ထုတ်လဲ။

Quick Quiz 2 / 3

int a[5]; ရှိပြီး int က 4 byte ဖြစ်တဲ့ PC မှာ (char *)&a[3] - (char *)&a[1] ရဲ့ တန်ဖိုး ဘာလဲ။

Quick Quiz 3 / 3

Function တစ်ခုက သူ့ local variable count ရဲ့ &count ကို return ပြန်ပါတယ်။ ဘယ်ဟာ မှန်လဲ။

မှတ်ထားရမယ့် အချက်များ

  • &x က x ရဲ့ address ကို ပေးပြီး သူ့ type က x ရဲ့ type ကို ညွှန်တဲ့ pointer ပါ (int အတွက် int *)။
  • sizeof က object တစ်ခု byte ဘယ်နှစ်ခု ယူလဲ ပြောပါတယ် — အရွယ်အစားကို ခန့်မှန်းမနေဘဲ %zu နဲ့ print ထုတ်ပါ။
  • Address ကို debugging အတွက်ပဲ %p နဲ့ (void *) cast သုံးပြီး print ထုတ်ပါ — နံပါတ်က run နဲ့ platform အလိုက် ပြောင်းပါတယ်။
  • Array တစ်ခုအတွင်း address တွေက ညီညီညာညာ ခြားထားလို့ element address တွေကြား ကွာခြားချက်ကို အားကိုးလို့ ရပါတယ်။
  • Object အသက်ရှင်နေသရွေ့ပဲ address ကို သုံးလို့ရပါတယ် — function return ပြီးရင် local ရဲ့ address ကို မသိမ်းထားပါနဲ့။

SEI CERT C Coding Standard — Carnegie Mellon SEI

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Memory Address | Thuta Learning