ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Array ကို ဆေးဗူး တစ်ပတ်စာ ဗူးလို မြင်ကြည့်ပါ — အကန့်ခုနစ်ကန့်က တစ်တန်းတည်း ကပ်လျက် ရှိပြီး အကန့်တိုင်းမှာ အမျိုးအစား တူတဲ့ ပစ္စည်း တစ်ခုစီ ထည့်ပါတယ်။ ဗူးတစ်ဗူးလုံးကို နာမည်တစ်ခုတည်းနဲ့ ခေါ်ပြီး အကန့်တစ်ခုကိုတော့ နံပါတ်နဲ့ ညွှန်းပါတယ်။ C က နံပါတ်ကို 0 ကနေ စတပ်လို့ ပထမ အကန့်က [0] ဖြစ်ပါတယ်။
စာမေးပွဲ ရမှတ် ငါးခု၊ ဈေးနှုန်း list တစ်ခု၊ နေ့စဉ် အပူချိန်တွေလို အရေအတွက် သိပြီး type တူတဲ့ data တွေအတွက် array ကို သုံးပါတယ်။ ဗူးရဲ့ အရွယ်အစားကို ဖန်တီးတဲ့အချိန်မှာ သတ်မှတ်ပြီး နောက်မှ ချဲ့လို့ မရပါဘူး။
Problem → Why → Solution
ပြဿနာ: C က array ရဲ့ အဆုံးကို မစောင့်ကြပ်ပေး
Score လေးခုကို ပေါင်းဖို့ loop ရေးတဲ့အခါ i <= 4 လို့ ရေးမိလိုက်တယ် ဆိုပါစို့။ ငါးကြိမ်မြောက် pass မှာ scores[4] ကို ဖတ်ပါတယ်၊ ဒါပေမယ့် အဲဒီ element မရှိပါဘူး။ C က ရပ်မပေးပါဘူး — program က အဓိပ္ပာယ်မရှိတဲ့ total တစ်ခု print ထုတ်တာ၊ crash ဖြစ်တာ ဒါမှမဟုတ် ကံကောင်းလို့ မှန်သလို ပေါ်နေတာ ဖြစ်နိုင်ပါတယ်။ Array အပြင်ကို ရေးမိရင် ပိုဆိုးပါတယ်၊ ဘေးက variable တစ်ခုကို တိတ်တဆိတ် ဖျက်ဆီးပစ်နိုင်ပါတယ်။
#include <stdio.h>
int main(void) {
int scores[4] = {80, 75, 90, 60};
int total = 0;
for (int i = 0; i <= 4; i++) { /* i == 4 is one past the end */
total += scores[i]; /* reads memory that is not scores */
}
printf("Total: %d\n", total); /* anything can happen */
return 0;
}ဘာကြောင့် compiler က မတားတာလဲ
C array တွေက run time မှာ သူတို့ရဲ့ length ကို သယ်မထားသလို C ကလည်း index ကို မစစ်ပါဘူး — scores[i] ဆိုတာ "စတဲ့ address + i * sizeof(int)" ပဲ ဖြစ်ပါတယ်။ i == 4 အတွက် အဲဒီ address က array အပြင်ဘက်မှာ ရှိပြီး C standard က အဲဒီကို ဖတ်တာကို undefined behaviour လို့ သတ်မှတ်ပါတယ်။ Compiler က ဒါ ဘယ်တော့မှ မဖြစ်ဘူးလို့ ယူဆခွင့်ရှိလို့ ဘာရလဒ်မဆို ထွက်နိုင်ပါတယ်။ GCC က -O0 နဲ့ -Wall -Wextra မှာ ဒီ loop အတွက် ဘာမှ မပြောပါဘူး၊ -O2 မှာမှ "iteration 4 invokes undefined behavior" လို့ သတိပေးနိုင်ပါတယ်။ Warning ကို အားမကိုးဘဲ bound ကို array ကိုယ်တိုင်ကနေ တွက်ယူပါ။
Core concept: ကပ်လျက် အကွက်တွေ၊ length က type ထဲမှာပဲ ရှိ
Array ဆိုတာ type တစ်မျိုးတည်းရှိတဲ့ element အရေအတွက် သတ်မှတ်ထားတာတွေကို memory ထဲမှာ ကွက်လပ်မရှိဘဲ တစ်ခုနောက်တစ်ခု သိမ်းထားတာပါ။ int scores[4] က int လေးခုစာ နေရာကို အတိအကျ ယူထားပြီး int က 4 byte ဖြစ်တဲ့ system မှာ ကပ်လျက် byte 16 ခု ဖြစ်ပါတယ်။ အရွယ်အစားက array ရဲ့ type ရဲ့ အစိတ်အပိုင်း ဖြစ်လို့ ဖန်တီးပြီးနောက် ပြောင်းလို့ မရပါဘူး။ Element i က စတဲ့ address ကနေ i ဆ element size ကွာတဲ့ နေရာမှာ ရှိလို့ indexing က မြန်ပြီး ပထမ element က 0 နံပါတ် ဖြစ်တာပါ — အစကနေ သုည လှမ်း ကွာလို့ပါ။
Initialiser က element တွေကို အစဉ်လိုက် ဖြည့်ပါတယ်။ int a[5] = {7, 3}; က a[0] နဲ့ a[1] ကို set လုပ်ပြီး ကျန်တဲ့ သုံးခုကို 0 နဲ့ ဖြည့်ပါတယ်၊ int zeros[4] = {0}; က array တစ်ခုလုံးကို 0 ပေးတဲ့ ပုံမှန်နည်းပါ။ Size ကို ချန်ထားရင် (int scores[] = {80, 75, 90, 60};) compiler က initialiser တွေကို ရေတွက်ပြီး အဲဒီ အရေအတွက် အတိအကျ ရှိတဲ့ array ကို ဖန်တီးပါတယ်။ Initialiser လုံးဝ မပါတဲ့ local array (int a[5];) ထဲမှာတော့ indeterminate value တွေ ရှိလို့ မရေးခင် ဖတ်ရင် bug ပါ။
C က run time မှာ မေးလို့ရတဲ့ နေရာမှာ length ကို မသိမ်းထားသလို index တွေကိုလည်း မစစ်ပါဘူး။ Length အချက်အလက် ရှိတာ compile time type ထဲမှာပဲ ဖြစ်ပြီး sizeof scores / sizeof scores[0] က အဲဒါကို element အရေအတွက် ပြောင်းပေးပါတယ် — 16 byte / 4 byte = 4 ပါ။ ဒီနည်းက array ကိုယ်တိုင် scope ထဲမှာ ရှိတဲ့ နေရာမှာပဲ အလုပ်လုပ်ပါတယ်။ Expression အများစုမှာ array နာမည်က ပထမ element ကို ညွှန်တဲ့ pointer အဖြစ် decay ဖြစ်ပြီး array ကို function ဆီ ပို့တဲ့အခါ function က အဲဒီ pointer ကိုပဲ ရပါတယ်။ Function ထဲမှာ sizeof က array မဟုတ်ဘဲ pointer ရဲ့ size (ပုံမှန် 64-bit system တွေမှာ 8 byte) ကို ပေးပါတယ်။ ဒါကြောင့် count ကို size_t parameter သီးသန့်အဖြစ် array နဲ့ တွဲပို့ရပါတယ်။
Array တစ်ခုလုံးကို assign လုပ်တာ၊ နှိုင်းယှဉ်တာ မလုပ်နိုင်ပါဘူး — b = a; က compile error ("assignment to expression with array type") ဖြစ်ပြီး a == b က content ကို မဟုတ်ဘဲ address တွေကို နှိုင်းယှဉ်ပါတယ်။ Element တစ်ခုချင်းကို loop နဲ့ copy လုပ်ပါ ဒါမှမဟုတ် <string.h> ထဲက memcpy() နဲ့ memcmp() ကို သုံးပါ။
int scores[4] in memory (int = 4 bytes here)
--------------------------------------------
index: [0] [1] [2] [3] | [4] ???
+--------+--------+--------+--------+-----------
value: | 80 | 75 | 90 | 60 | not yours
+--------+--------+--------+--------+-----------
offset: +0 +4 +8 +12 | +16 bytes
sizeof scores = 16 bytes count = 16 / 4 = 4
valid indexes: 0 .. count - 1 (scores[4] is undefined behaviour)- Type တူ အကွက်တွေ တန်းစီ၊ size ပုံသေ
- အကွက် i က အစကနေ i လှမ်း ကွာ
- Valid index: 0 ကနေ count - 1
- Array နဲ့ count ကို အမြဲ တွဲပို့
Array က သူ့ size ကို ကြေညာထားတဲ့ နေရာမှာပဲ သိပြီး တခြား နေရာတိုင်းမှာ count ကို ကိုယ်တိုင် သယ်ရပါတယ်။
အထွေထွေ ပုံစံ
type name[COUNT]; /* local, no initialiser: garbage */
type name[] = {v0, v1, v2}; /* size counted by the compiler */
type name[COUNT] = {0}; /* every element is 0 */
size_t count = sizeof name / sizeof name[0];
for (size_t i = 0; i < count; i++) {
use(name[i]);
}
result function_name(const type values[], size_t count);COUNT က compile time မှာ သိတဲ့ positive integer ဖြစ်ရပါတယ်။ Element အရေအတွက်ကို sizeof နဲ့ တွက်ပြီး index loop ကို 0 ကနေ < count နဲ့ ပတ်ပါ။ Function တွေက array ကို ပြောင်းဖို့ မလိုရင် const pointer နဲ့ count နှစ်ခုလုံးကို လက်ခံပါတယ်။
Example: score လေးခုရဲ့ total နဲ့ average
Loop က 4 ဆိုတဲ့ number ကို ဘယ်နေရာမှာမှ hard-code မလုပ်ထားတာကို သတိထားကြည့်ပါ။ Element အရေအတွက်ကို array ကနေ တွက်ယူထားလို့ score ငါးခုမြောက် တစ်ခု ထပ်ထည့်ရင် initialiser line တစ်ကြောင်းတည်းကို ပြင်ရုံပါပဲ။
#include <stdio.h>
int main(void) {
int scores[] = {80, 75, 90, 60};
size_t count = sizeof scores / sizeof scores[0];
int total = 0;
for (size_t i = 0; i < count; i++) {
total += scores[i];
}
printf("Count: %zu\n", count);
printf("Total: %d\n", total);
printf("Average: %.2f\n", (double)total / count);
printf("First: %d, last: %d\n", scores[0], scores[count - 1]);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Size from the initialiser
int scores[] = {80, 75, 90, 60};Bracket ထဲမှာ size မပါလို့ compiler က value လေးခုကို ရေတွက်ပြီး int[4] array တစ်ခု ဖန်တီးပါတယ်။
- Element count
size_t count = sizeof scores / sizeof scores[0];sizeof scores က array တစ်ခုလုံးရဲ့ byte (16) ဖြစ်ပြီး sizeof scores[0] က element တစ်ခုရဲ့ byte (4) ပါ။ စားလဒ် 4 က int ရဲ့ size ဘယ်လောက်ပဲ ဖြစ်ဖြစ် မှန်ပါတယ်။
- Valid indexes only
for (size_t i = 0; i < count; i++) {i က 0, 1, 2, 3 ဖြစ်ပြီး i == 4 မှာ < က ရပ်ပေးလို့ array အပြင်ကို ဘယ်တော့မှ မရောက်ပါဘူး။ i ရဲ့ type က count နဲ့ တူတဲ့ size_t ဖြစ်လို့ sign-compare warning လည်း မရှိပါဘူး။
- Real division
printf("Average: %.2f\n", (double)total / count);(double) cast မပါရင် total / count က integer division ဖြစ်ပြီး 76 ရကာ %.2f နဲ့လည်း type မကိုက်တော့ပါဘူး။ Cast က 305.0 / 4 = 76.25 ကို ရစေပါတယ်။
- First and last
scores[0], scores[count - 1]ပထမ element က အမြဲ [0] ဖြစ်ပြီး နောက်ဆုံး element က [count - 1] ပါ၊ [count] မဟုတ်ပါဘူး။
Count: 4
Total: 305
Average: 76.25
First: 80, last: 60Initialise လုပ်တာနဲ့ copy လုပ်တာ
အောက်က code က initialiser မပြည့်တဲ့အခါ ကျန်တဲ့ element တွေ 0 ဖြစ်တာနဲ့ array တစ်ခုကို နောက်တစ်ခုထဲ element တစ်ခုချင်း copy လုပ်တာကို ပြပါတယ်။ copy = partial; လို့ ရေးရင် compile error ဖြစ်လို့ loop ကို သုံးရတာပါ။
#include <stdio.h>
int main(void) {
int partial[5] = {7, 3}; /* the rest become 0 */
int zeros[4] = {0}; /* every element is 0 */
int copy[5];
size_t n = sizeof partial / sizeof partial[0];
for (size_t i = 0; i < n; i++) {
copy[i] = partial[i] * 10; /* arrays are copied element by element */
}
for (size_t i = 0; i < n; i++) {
printf("%d ", partial[i]);
}
printf("| %d %d | %d\n", zeros[0], zeros[3], copy[1]);
return 0;
}Array ကို function ဆီ ပို့တဲ့အခါ
Function တွေကို Functions သင်ခန်းစာမှာ အသေးစိတ် လေ့လာရမှာ ဖြစ်ပေမယ့် array နဲ့ ပတ်သက်တဲ့ စည်းမျဉ်း တစ်ခုကို အခုကတည်းက သိထားသင့်ပါတယ်။ sum_all(scores, count) လို့ ခေါ်တဲ့အခါ scores က ပထမ element ရဲ့ address အဖြစ် decay ဖြစ်ပြီး function က copy မဟုတ်ဘဲ မူလ data ကို ညွှန်တဲ့ pointer ကို ရပါတယ်။ const int values[] က "ဒီ function က element တွေကို မပြောင်းဘူး" လို့ ကတိပေးတာပါ။
#include <stdio.h>
int sum_all(const int values[], size_t count) {
int total = 0;
for (size_t i = 0; i < count; i++) {
total += values[i];
}
return total;
}
int main(void) {
int scores[] = {80, 75, 90, 60};
size_t count = sizeof scores / sizeof scores[0];
printf("sizeof scores in main: %zu bytes\n", sizeof scores);
printf("sum: %d\n", sum_all(scores, count));
return 0;
}sum_all ထဲမှာ sizeof values / sizeof values[0] လို့ ရေးမိရင် 64-bit system မှာ 8 / 4 = 2 ရပြီး element နှစ်ခုကိုပဲ ပေါင်းပါလိမ့်မယ် (80 + 75 = 155)။ GCC က ဒီအမှားကို default အနေနဲ့ပဲ "'sizeof' on array function parameter 'values' will return size of 'const int *'" လို့ သတိပေးပါတယ်။ values[] ဆိုတဲ့ ရေးပုံက array လို ပေါ်ပေမယ့် parameter ရဲ့ type အစစ်က const int * ပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Memory နည်းတဲ့ ဒါမှမဟုတ် ကြိုတင်ခန့်မှန်းနိုင်ရမယ့် နေရာတွေမှာ fixed-size array တွေကို နေရာတိုင်း တွေ့ရပါတယ်။ Embedded firmware က နောက်ဆုံး sensor sample 16 ခုကို ring buffer array ထဲ သိမ်းပါတယ်၊ network code က packet တွေကို char buf[1500] လို buffer ထဲ ဖတ်ပါတယ်၊ game တွေက particle 256 ခုစာ fixed pool ကို ထားပါတယ်၊ လတစ်လစီရဲ့ ရက်အရေအတွက်လို lookup table တွေကို const array အဖြစ် ထားပါတယ်။
C standard library နဲ့ C codebase တော်တော်များများမှာ တွေ့ရမယ့် API စည်းမျဉ်းက "pointer နဲ့ count" ပါ — qsort(base, count, size, compare), fwrite(ptr, size, count, file), memcpy(dest, src, bytes) စတာတွေပါ။ Array အပြင်ဘက်ကို ရေးတဲ့ bug (buffer overflow) တွေက ဆယ်စုနှစ်ပေါင်းများစွာ C program တွေမှာ အန္တရာယ်အရှိဆုံး security bug အမျိုးအစားတွေထဲမှာ ပါခဲ့ပါတယ်။ ဒါကြောင့် team တွေက element တစ်ခုပဲ ရှိတာ၊ အပြည့် ရှိတာလို edge case တွေနဲ့ စမ်းပြီး GCC နဲ့ Clang ရဲ့ AddressSanitizer (-fsanitize=address) လို tool တွေနဲ့ test လုပ်ကြပါတယ်၊ အဲဒီ tool တွေက out-of-bounds access အများစုကို ဖြစ်တဲ့ နေရာမှာတင် program ကို ရပ်ပြီး report ထုတ်ပေးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. i < count နေရာမှာ i <= count
Problem section က bug ပါ — နောက်ဆုံး pass က array[count] ကို ထိပြီး undefined behaviour ဖြစ်ပါတယ်။ Index loop တွေကို i = 0; i < count ပုံစံနဲ့ပဲ ရေးပါ။
2. Function parameter ပေါ်မှာ sizeof သုံးတာ
int values[] parameter က တကယ်တော့ int * ဖြစ်လို့ sizeof values က pointer size ကို ပေးပါတယ်။ Function ထဲမှာ sizeof နဲ့ count ကို မတွက်ပါနဲ့၊ caller က sizeof နဲ့ တွက်ပြီး size_t count parameter အဖြစ် ပို့ပေးပါ။
3. Length ကို hard-code လုပ်တာ
for (int i = 0; i < 4; i++) နဲ့ total / 4.0 လို့ ရေးထားရင် score ငါးခုမြောက် ထပ်ထည့်တဲ့အခါ loop က အသစ်ကို တိတ်တဆိတ် ကျော်ပြီး average လည်း မှားပါတယ်။ Array ကို လျော့လိုက်ရင်တော့ အပြင်ဘက်ကို ဖတ်ပါတယ်။ Count ကို sizeof ကနေ တစ်နေရာတည်းမှာ တွက်ပြီး နေရာတိုင်းမှာ အဲဒီ variable ကို သုံးပါ။
4. Array ကို = နဲ့ copy လုပ်ဖို့ ကြိုးစားတာ
b = a; က "assignment to expression with array type" compile error ပေးပါတယ်။ Loop နဲ့ element တစ်ခုချင်း copy လုပ်ပါ ဒါမှမဟုတ် memcpy(b, a, sizeof a) ကို သုံးပါ — b က a လောက် အနည်းဆုံး ကြီးရပါမယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Element အရေအတွက်ကို sizeof arr / sizeof arr[0] နဲ့ တွက်ပြီး size_t variable တစ်ခုထဲ သိမ်းပါ။
- Local array တွေကို အမြဲ initialise လုပ်ပါ၊ {0} က အလွယ်ဆုံး နည်းပါ။
- Array လက်ခံတဲ့ function တိုင်းမှာ count parameter ထည့်ပြီး မပြောင်းရင် const ကို သုံးပါ။
- Test လုပ်တဲ့အခါ element တစ်ခု၊ အပြည့်နဲ့ boundary index (0 နဲ့ count - 1) တွေကို စမ်းပါ။
Mini challenge
Mini challenge: တစ်ပတ်စာ အပူချိန်
တစ်ပတ်စာ နေ့စဉ် အမြင့်ဆုံး အပူချိန် ခုနစ်ခုကို array ထဲမှာ သိမ်းပါ။ Loop တစ်ခုတည်းနဲ့ အမြင့်ဆုံး၊ အနိမ့်ဆုံး အပူချိန်နဲ့ 32 ဒီဂရီထက် ပူတဲ့ ရက်အရေအတွက်ကို ရှာပါ။
- int temps[] = {31, 34, 29, 36, 33, 28, 35}; ကို သုံးရမယ်
- ရက်အရေအတွက်ကို 7 လို့ hard-code မလုပ်ဘဲ sizeof နဲ့ တွက်ရမယ်
- max နဲ့ min ကို 0 နဲ့ မဟုတ်ဘဲ ပထမ element နဲ့ စရမယ်
- Loop တစ်ခုတည်းထဲမှာ max, min နဲ့ hot day count သုံးခုလုံးကို update လုပ်ရမယ်
#include <stdio.h>
int main(void) {
int temps[] = {31, 34, 29, 36, 33, 28, 35};
// TODO: compute the number of days with sizeof
// TODO: max and min start from the first element
// TODO: a counter for days above 32
// TODO: one loop over every valid index:
// update max, update min, count the hot days
// TODO: print the days, "Max: .., min: .." and the hot-day count
return 0;
}ရလဒ်: Program က ဒီ line သုံးကြောင်းကို print ထုတ်ရပါမယ်: Days: 7 Max: 36, min: 28 Days above 32: 4
အရိပ်အမြွက် ကြည့်ရန်
max ကို 0 နဲ့ စရင် အပူချိန် အားလုံး အနုတ် ဖြစ်တဲ့ ဆောင်းရာသီ data မှာ မှားပါလိမ့်မယ်၊ min ကို 0 နဲ့ စရင်လည်း အလားတူပါပဲ။ temps[0] နဲ့ စတာက data ဘယ်လိုပဲ ဖြစ်ဖြစ် မှန်ပါတယ်။
အဖြေ ကြည့်ရန်
#include <stdio.h>
int main(void) {
int temps[] = {31, 34, 29, 36, 33, 28, 35};
size_t days = sizeof temps / sizeof temps[0];
int max = temps[0];
int min = temps[0];
int hot_days = 0;
for (size_t i = 0; i < days; i++) {
if (temps[i] > max) {
max = temps[i];
}
if (temps[i] < min) {
min = temps[i];
}
if (temps[i] > 32) {
hot_days++;
}
}
printf("Days: %zu\n", days);
printf("Max: %d, min: %d\n", max, min);
printf("Days above 32: %d\n", hot_days);
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Array က type တူ element အရေအတွက် ပုံသေကို memory ထဲမှာ ကပ်လျက် သိမ်းပြီး index က 0 ကနေ စပါတယ်။
- Valid index တွေက 0 ကနေ count - 1 ပဲ ဖြစ်ပြီး အပြင်ဘက် access က undefined behaviour ဖြစ်ပါတယ် — C က မစစ်ပေးပါဘူး။
- sizeof arr / sizeof arr[0] က array ကြေညာထားတဲ့ scope ထဲမှာပဲ element အရေအတွက်ကို ပေးပါတယ်။
- Function ဆီ ပို့တဲ့အခါ array က pointer အဖြစ် decay ဖြစ်လို့ count ကို size_t parameter အဖြစ် တွဲပို့ပါ။
SEI CERT C Coding Standard — Carnegie Mellon SEI