ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
အခုထိ ရေးခဲ့တဲ့ variable နဲ့ array တွေရဲ့ အရွယ်အစားကို compile မလုပ်ခင်ကတည်းက ဆုံးဖြတ်ထားရပါတယ်၊ function ပြီးတာနဲ့လည်း အလိုအလျောက် ပျောက်သွားပါတယ်။ Dynamic memory ကတော့ ပစ္စည်းသိုလှောင်ရုံကနေ နေရာငှားတာနဲ့ တူပါတယ် — program run နေတုန်း လိုသလောက် တောင်းပြီး ကိုယ်တိုင် ပြန်အပ်တဲ့အထိ ကိုယ့်လက်ထဲမှာ ရှိနေပါတယ်။
သိုလှောင်ရုံမှာ နေရာ ကုန်နိုင်လို့ တောင်းတိုင်း ရချင်မှ ရပါမယ်၊ ဒါကြောင့် အဖြေကို အမြဲ စစ်ရပါတယ်။ ပြန်မအပ်ဘဲ မေ့ထားရင် program ပြီးတဲ့အထိ နေရာ ဆက်ယူထားပြီး အပ်ပြီးသားနေရာကို ပြန်ဝင်သုံးရင်တော့ တခြားသူရဲ့ ပစ္စည်းကို ထိမိနိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: user ထည့်တဲ့ အရေအတွက်ကို fixed array က မနိုင်
User ရိုက်ထည့်တဲ့ n အလိုက် ပထမ square number n လုံးကို တည်ဆောက်ပေးတဲ့ function တစ်ခု လိုချင်တယ် ဆိုပါစို့။ Fixed-size array တွေပဲ သုံးရင် နှစ်ခါ ပိတ်မိပါတယ်။ Element 100 ပါတဲ့ local array က user က 500 ရိုက်ရင် ကျော်လျှံပြီး 3 ရိုက်ရင် memory ကို အလဟဿ ဖြုန်းပါတယ်။ ပိုဆိုးတာက အဲဒီ local array ကို return ပြန်လိုက်တာက caller ကို dangling pointer ပေးတာဖြစ်ပြီး GCC က function returns address of local variable ဆိုပြီး warning ပေးပါတယ်။
#include <stdio.h>
int *make_squares(int n) {
int result[100]; /* too small for n > 100 */
for (int i = 0; i < n; i++) {
result[i] = i * i;
}
return result; /* result dies when the function returns */
}
int main(void) {
int n = 0;
printf("How many squares? ");
if (scanf("%d", &n) != 1) return 1;
int *squares = make_squares(n);
printf("%d\n", squares[n - 1]); /* undefined behaviour */
return 0;
}Stack memory က ဘာကြောင့် မလုံလောက်တာလဲ
Local array တွေက function ရဲ့ stack frame ထဲမှာ နေပါတယ် — အရွယ်အစားကို compile လုပ်တဲ့အချိန် သတ်မှတ်ပြီး function return ပြန်တာနဲ့ frame တစ်ခုလုံး ပျောက်သွားပါတယ်။ လိုအပ်တာက run နေချိန်မှာ အရွယ်အစား ရွေးလို့ရပြီး lifetime ကို ကိုယ်တိုင် ထိန်းချုပ်နိုင်တဲ့ memory ပါ — "ပြီးပြီ" လို့ ကိုယ်တိုင် မပြောမချင်း ရှိနေရပါမယ်။ Heap က malloc(), calloc(), realloc() နဲ့ free() ကနေ အဲဒါကို အတိအကျ ပေးပါတယ်။
Core concept: တောင်း၊ စစ်၊ သုံး၊ ပြန်အပ်
Dynamic memory က heap ကနေ လာပါတယ်၊ heap ဆိုတာ C library က စီမံပေးတဲ့ memory pool တစ်ခုပါ။ malloc(bytes) က အနည်းဆုံး အဲဒီ byte အရေအတွက်ရှိတဲ့ block တစ်ခုကို ယူထားပြီး ပထမ byte ကို ညွှန်တဲ့ void * ကို return ပြန်ပါတယ်။ void * က int * လို object pointer မည်သည့်ဟာကိုမဆို အလိုအလျောက် ပြောင်းလို့ C မှာ cast မလိုပါဘူး။ malloc block အသစ်ရဲ့ အကြောင်းအရာက indeterminate ဖြစ်လို့ မရေးခင် ဖတ်တာ bug ပါ။ calloc(count, size) ကတော့ size byte ရှိတဲ့ element count ခုစာ ယူပြီး byte တိုင်းကို သုည ထားပေးလို့ integer တွေအတွက် 0 ရပါတယ်။
Allocation တိုင်း fail ဖြစ်နိုင်ပြီး fail ဖြစ်ရင် NULL ကို return ပြန်ပါတယ်။ မသုံးခင် ရလဒ်ကို အကြိမ်တိုင်း စစ်ပါ။ အရွယ်အစားကို count * sizeof *ptr လို့ ရေးပါ — sizeof *ptr က ptr ညွှန်တဲ့ type ရဲ့ အရွယ်အစားဖြစ်ပြီး ဘာကိုမှ dereference မလုပ်ဘဲ compile time မှာ တွက်တာပါ၊ နောက်ပိုင်း element type ပြောင်းရင်လည်း မှန်နေဆဲပါ။ count က program အပြင်ကနေ လာရင် if (count > SIZE_MAX / sizeof *ptr) နဲ့ မြှောက်ခြင်းကို ကာကွယ်ပါ၊ ဒါမှ count အကြီးကြီးက wrap around ဖြစ်ပြီး size အသေးလေး ဖြစ်မသွားမှာပါ။
realloc(ptr, new_bytes) က block ရဲ့ အရွယ်အစားကို ပြောင်းပါတယ်။ နေရာမှာတင် ချဲ့နိုင်သလို block အသစ်ယူ၊ အဟောင်းထဲက data ကို copy ကူး၊ အဟောင်းကို free လုပ်တာလည်း ဖြစ်နိုင်လို့ address ပြောင်းသွားနိုင်ပါတယ်။ Fail ဖြစ်ရင် NULL ပြန်ပြီး block အဟောင်းကို မထိဘဲ ထားခဲ့ပါတယ် — ဒါကြောင့် ရလဒ်ကို temporary pointer ထဲ အရင် ထည့်ရတာပါ။ ptr = realloc(ptr, n) လို့ ရေးရင် fail တဲ့အခါ allocate လုပ်ထားဆဲ block ကို ညွှန်တဲ့ တစ်ခုတည်းသော pointer ပျောက်သွားပြီး leak သေချာပေါက် ဖြစ်ပါတယ်။
free(ptr) က block ကို ပြန်အပ်ပါတယ်။ အောင်မြင်တဲ့ allocation တိုင်းမှာ free တစ်ကြိမ်အတိအကျ လိုပြီး block ကို ပိုင်တဲ့ program အစိတ်အပိုင်းက လုပ်ရပါမယ်။ free ပြီးရင် pointer ထဲမှာ address အဟောင်း ရှိနေဆဲပေမယ့် မသုံးရတော့ပါဘူး — အဲဒီကနေ ဖတ်တာ၊ ရေးတာ (use-after-free) ဒါမှမဟုတ် ထပ် free လုပ်တာ (double free) က undefined behaviour ပါ။ free ပြီးပြီးချင်း pointer ကို NULL ထားလိုက်ရင် မတော်တဆ ပြန်သုံးမိတာက သိသာတဲ့ bug ဖြစ်လာပြီး free(NULL) က ဘာမှ မလုပ်ဘူးလို့ standard က သတ်မှတ်ထားပါတယ်။
int *values = calloc(5, sizeof *values);
----------------------------------------
stack (main's frame) heap
+---------------------+ +----+----+----+----+----+
| count = 5 | | 0 | 10 | 20 | 30 | 40 |
| values = -----------+-----> +----+----+----+----+----+
+---------------------+ 5 * sizeof(int) = 20 bytes
released automatically lives until free(values)
when main returns- တောင်း: p = malloc(n * sizeof *p)
- စစ်: p == NULL ဆိုရင် ကိုင်တွယ်
- သုံး: p ကို item n ခု array လို သုံး
- ပြန်အပ်: free(p); p = NULL;
Allocation တစ်ခု၊ ပိုင်ရှင် တစ်ယောက်၊ free တစ်ကြိမ် — block ကို allocate လုပ်တဲ့အချိန်မှာပဲ ဘယ်သူ free လုပ်မလဲ ဆုံးဖြတ်ပါ။
အထွေထွေ ပုံစံ
type *p = malloc(count * sizeof *p); /* uninitialized block */
type *z = calloc(count, sizeof *z); /* zero-filled block */
if (p == NULL) { /* handle the failure */ }
type *tmp = realloc(p, new_count * sizeof *p);
if (tmp != NULL) p = tmp; /* on failure p is still valid */
free(p);
p = NULL;အရွယ်အစားကို element အရေအတွက် × sizeof *p နဲ့ အမြဲ တွက်ပါ။ Allocation တိုင်းနောက်မှာ NULL စစ်ပြီး realloc ရလဒ်ကို temporary pointer ထဲ အရင်ထည့်ပါ။ free ပြီးတာနဲ့ pointer ကို NULL ထားပါ။
Example: run time မှာ int array တစ်ခု ယူခြင်း
ဒီ program က int ငါးခုစာ နေရာကို heap ဆီ တောင်းပြီး တန်ဖိုးဖြည့်၊ print ထုတ်ပြီးနောက် memory ကို ပြန်အပ်ပါတယ်။ ဒီမှာ count က variable တစ်ခုဖြစ်ပေမယ့် user input ဒါမှမဟုတ် file ကနေလည်း လာနိုင်ပါတယ် — ဒါက အဓိက အချက်ပါ၊ အရွယ်အစားကို program run နေချိန်မှာ ဆုံးဖြတ်တာပါ။ Output က 0 10 20 30 40 ပါ။
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t count = 5;
int *values = calloc(count, sizeof *values);
if (values == NULL) return EXIT_FAILURE;
for (size_t i = 0; i < count; ++i) values[i] = (int)(i * 10);
for (size_t i = 0; i < count; ++i) printf("%d%c", values[i], i + 1 == count ? '\n' : ' ');
free(values);
values = NULL;
return EXIT_SUCCESS;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 6
calloc(count, sizeof *values)calloc က count နဲ့ element အရွယ်အစားကို သီးခြားစီ လက်ခံပြီး မြှောက်ခြင်းကို ကိုယ်တိုင် လုပ်ပေးကာ byte အားလုံးကို သုည ထားပေးပါတယ်။ ပင်မ C library တွေက မြှောက်လဒ် overflow ဖြစ်မယ်ဆိုရင် block အသေးလေး မပေးဘဲ NULL ပြန်ပါတယ်။
- Line 6
sizeof *valuesvalues ညွှန်တဲ့ type (int) ရဲ့ အရွယ်အစားပါ။ sizeof က သူ့ operand ကို evaluate မလုပ်လို့ values ကို assign မလုပ်ရသေးခင် သုံးတာ ဘေးကင်းပြီး နောက်ပိုင်း type ကို double ပြောင်းရင်လည်း အလိုအလျောက် မှန်နေပါတယ်။
- Line 7
if (values == NULL) return EXIT_FAILURE;Allocation fail ဖြစ်ရင် NULL ကို index လုပ်မယ့်အစား ချက်ချင်း ရပ်ပါတယ်။ EXIT_FAILURE က stdlib.h က portable exit code ပါ။
- Line 9
values[i] = (int)(i * 10);Heap block ကို ရိုးရိုး array လိုပဲ index နဲ့ သုံးပါတယ်။ i က size_t ဖြစ်လို့ (int) cast က ရည်ရွယ်ချက်ရှိရှိ ပြောင်းတာ ဖြစ်ကြောင်း ပြပါတယ်။
- Lines 12-13
free(values);Block ကို တစ်ကြိမ်တည်း ပြန်အပ်ပြီး နောက် line က values = NULL; လုပ်လို့ နောက်ပိုင်း မတော်တဆ ပြန်သုံးမိရင် free လုပ်ပြီးသား address အစား NULL ကိုပဲ တွေ့ပါမယ်။
0 10 20 30 40realloc နဲ့ array ကို ချဲ့ခြင်း
အသုံးများတဲ့ pattern တစ်ခုက သေးသေးလေးနဲ့ စပြီး ပြည့်တိုင်း capacity ကို နှစ်ဆ တိုးတဲ့ dynamic array ပါ။ နှစ်ဆတိုးတာက realloc ခေါ်ရတဲ့ အကြိမ်ရေကို နည်းစေပါတယ် — item တစ်သန်း သိမ်းဖို့ resize အကြိမ် နှစ်ဆယ်လောက်ပဲ လိုပါတယ်။ bigger က ရလဒ်ကို လက်ခံပြီး call အောင်မြင်မှပဲ items ကို အစားထိုးတာကို သတိထားကြည့်ပါ။
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t capacity = 2, length = 0;
int *items = malloc(capacity * sizeof *items);
if (items == NULL) return EXIT_FAILURE;
for (int value = 1; value <= 5; value++) {
if (length == capacity) {
size_t new_capacity = capacity * 2;
int *bigger = realloc(items, new_capacity * sizeof *items);
if (bigger == NULL) { /* items is still valid here */
free(items);
return EXIT_FAILURE;
}
items = bigger;
capacity = new_capacity;
printf("grew to %zu slots\n", capacity);
}
items[length++] = value * value;
}
for (size_t i = 0; i < length; i++) printf("%d ", items[i]);
printf("\n");
free(items);
items = NULL;
return EXIT_SUCCESS;
}ဒီ program က grew to 4 slots၊ grew to 8 slots ပြီးနောက် 1 4 9 16 25 ကို print ထုတ်ပါတယ်။ realloc က တစ်ဝက်မှာ fail ဖြစ်ခဲ့ရင် items က block အဟောင်း (မှန်ကန်ဆဲ) ကို ညွှန်နေဆဲမို့ error လမ်းကြောင်းက leak မဖြစ်ဘဲ သန့်သန့်ရှင်းရှင်း free လုပ်နိုင်ပါတယ်။
malloc, calloc, realloc နှိုင်းယှဉ်ချက်
| Function | ရလာတာ | ဘယ်အချိန် သုံးမလဲ |
|---|---|---|
| malloc(bytes) | byte အရေအတွက်အတိုင်း block၊ အကြောင်းအရာ indeterminate | Element တိုင်းကို ချက်ချင်း ရေးမယ့်အခါ |
| calloc(count, size) | count * size byte အားလုံး သုည၊ မြှောက်ခြင်းကို library က လုပ်ပေး | သုည လိုချင်တဲ့အခါ ဒါမှမဟုတ် count က input ကနေ လာတဲ့အခါ |
| realloc(p, bytes) | အရွယ်အစား ပြောင်းထားတဲ့ block၊ address အသစ် ဖြစ်နိုင်၊ data အဟောင်း ပါ | ရှိပြီးသား buffer ကို ချဲ့ ဒါမှမဟုတ် ချုံ့ ရမယ့်အခါ |
Byte 0 ခု မတောင်းပါနဲ့
malloc(0) က ဘာ return ပြန်မလဲဆိုတာ implementation-defined ဖြစ်ပြီး realloc(p, 0) ကလည်း C library အလိုက် ပြုမူပုံ မတူပါဘူး (C23 မှာတော့ undefined behaviour အဖြစ် သတ်မှတ်ထားပါတယ်)။ Size 0 ဖြစ်နိုင်တဲ့ case ကို allocation မခေါ်ခင် ကိုယ်တိုင် ကိုင်တွယ်ပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Run မှ အရွယ်အစား သိရတဲ့ အရာမှန်သမျှ heap ပေါ်မှာ နေပါတယ် — edit လုပ်နေတဲ့ file ရဲ့ line တွေ၊ database ကနေ load လုပ်ထားတဲ့ record တွေ၊ disk ကနေ decode လုပ်ထားတဲ့ ပုံတစ်ပုံ၊ linked list ရဲ့ node တွေ။ Server လို ကြာကြာ run နေတဲ့ program တွေက request တိုင်းအတွက် memory ယူပြီး free လုပ်ကြလို့ request တစ်ခုမှာ byte အနည်းငယ်စီ leak ဖြစ်တာက တဖြည်းဖြည်း ကြီးလာပြီး နောက်ဆုံး process ကို kill လုပ်ရတဲ့အထိ ဖြစ်ပါတယ်။
Real codebase တွေက ownership ကို ရှင်းရှင်းလင်းလင်း ဖော်ပြကြပါတယ်။ Allocate လုပ်ထားတဲ့ memory ကို return ပြန်တဲ့ function က caller က free လုပ်ရမယ်လို့ document လုပ်ပါတယ် (POSIX system တွေက strdup() နဲ့ getline() က standard ဥပမာတွေပါ)၊ component ကြီးတွေမှာ create function တစ်ခုကို ကိုက်ညီတဲ့ destroy function နဲ့ တွဲထားလေ့ ရှိပါတယ်။ Leak, use-after-free နဲ့ double free တွေက data ကို ဖျက်ဆီးတဲ့နေ့ မရောက်မချင်း မှန်သလို ထင်ရတဲ့ output ထုတ်တတ်လို့ team တွေက test တွေကို AddressSanitizer ဒါမှမဟုတ် Valgrind အောက်မှာ run ကြပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. realloc ရလဒ်ကို pointer တစ်ခုတည်းထဲ တိုက်ရိုက် ပြန်ထည့်တာ
data = realloc(data, n * sizeof *data); က သပ်ရပ်သလို ထင်ရပေမယ့် realloc fail ဖြစ်တဲ့အခါ NULL ပြန်ပြီး address ရဲ့ တစ်ခုတည်းသော copy ကို ဖျက်ကာ block အဟောင်းကို leak ဖြစ်စေပါတယ်။ Temporary pointer သုံးပြီး အောင်မြင်မှပဲ data ကို အစားထိုးပါ။
2. free() ပြီးမှ memory ကို ဆက်သုံးတာ
free(values); ပြီးရင် pointer ထဲမှာ address အဟောင်း ရှိနေဆဲမို့ values[0] က ခဏတာ ဂဏန်းအဟောင်းကိုတောင် print ထုတ်နိုင်ပါတယ်။ ဒါက use-after-free ဖြစ်ပြီး နောက်မှ heap ကို ဖျက်ဆီးနိုင်တဲ့ undefined behaviour ပါ။ Free ပြီးတာနဲ့ pointer ကို NULL ထားပြီး block တစ်ခုကို တစ်ကြိမ်ပဲ free လုပ်ပါ။
3. Element အရွယ်အစားကို မေ့တာ
malloc(count) က int count ခု မဟုတ်ဘဲ byte count ခုကိုပဲ ယူပါတယ်၊ ဒါကြောင့် int count ခု ရေးရင် block ကို လေးဆ ကျော်ရေးမိပါတယ်။ sizeof *p နဲ့ အမြဲ မြှောက်ပါ၊ sizeof(p) လို့ ဘယ်တော့မှ မရေးပါနဲ့ — အဲဒါ pointer ကိုယ်တိုင်ရဲ့ အရွယ်အစား 8 byte ပါ၊ သူညွှန်တဲ့ အရာရဲ့ အရွယ်အစား မဟုတ်ပါဘူး။
4. NULL မစစ်တာ
Desktop ပေါ်မှာ allocation အသေးလေးတွေ fail ဖြစ်တာ ရှားလို့ check မပါတာကို program ကို အလုပ်များတဲ့ server ဒါမှမဟုတ် embedded device ပေါ် run မှ သတိထားမိပါတယ်။ ရလဒ်တိုင်းကို စစ်ပြီး ဘာလုပ်မလဲ ဆုံးဖြတ်ပါ — error code ပြန်တာ၊ message print ထုတ်တာ ဒါမှမဟုတ် သန့်သန့်ရှင်းရှင်း exit လုပ်တာပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Allocation ကို p = malloc(count * sizeof *p) ပုံစံနဲ့ ရေးပြီး နောက် line မှာတင် NULL စစ်ပါ။
- Block တစ်ခုစီကို allocate လုပ်တဲ့အချိန်မှာ ဘယ်သူ ပိုင်မလဲ ဆုံးဖြတ်ပြီး caller က free လုပ်ရမယ့် memory ပြန်ပေးတဲ့ function တွေကို document လုပ်ပါ။
- Test လုပ်တဲ့အခါ -fsanitize=address နဲ့ build လုပ်ပါ ဒါမှမဟုတ် Valgrind နဲ့ run ပါ။
Mini challenge
Mini challenge: copy ကူးပြီး ချဲ့ခြင်း
Heap memory နဲ့ int array ကို copy ကူးတဲ့ function နဲ့ element တစ်ခု ထပ်ထည့်ပေးတဲ့ function ကို ရေးပြီး memory ကို မှန်မှန်ကန်ကန် ပြန်အပ်ပါ။
- int *duplicate(const int *src, size_t count) က heap copy အသစ်ကို ပြန်ပေးရမယ်၊ src က NULL ဖြစ်ရင်၊ count 0 ဖြစ်ရင်၊ ဒါမှမဟုတ် allocation fail ဖြစ်ရင် NULL ပြန်ရမယ်
- Byte အရေအတွက် overflow ဖြစ်နိုင်တာကို SIZE_MAX / sizeof *src နဲ့ ကာကွယ်ရမယ်
- int *append(int *arr, size_t count, int value) က realloc ကို temporary pointer နဲ့ သုံးပြီး fail ရင် NULL ပြန်ကာ arr ကို မထိဘဲ ထားခဲ့ရမယ်
- main က copy ကို print ထုတ်၊ 16 နဲ့ 23 ကို append လုပ်၊ မူရင်း array မပြောင်းကြောင်း ပြ၊ free လုပ်ပြီး pointer ကို NULL ထားရမယ်
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Returns a heap copy of src, or NULL. The caller must free() it. */
int *duplicate(const int *src, size_t count) {
// TODO: reject NULL, count == 0 and counts whose byte size would overflow
// TODO: malloc the copy, check it, copy the data with memcpy, return it
}
/* Grows arr by one slot. On failure returns NULL and arr is untouched. */
int *append(int *arr, size_t count, int value) {
// TODO: realloc into a temporary pointer, then store value in the new slot
}
int main(void) {
int original[] = {4, 8, 15};
// TODO: duplicate, print, append 16 and 23, print, print the original,
// then free the copy and set it to NULL
return 0;
}ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: copy: 4 8 15 after append: 4 8 15 16 23 original: 4 8 15 duplicate(NULL, 3) returns NULL
အရိပ်အမြွက် ကြည့်ရန်
memcpy(copy, src, count * sizeof *copy) က byte တွေကို တစ်ခါတည်း copy ကူးပေးပါတယ်။ append() ထဲမှာ realloc(arr, (count + 1) * sizeof *arr) ကို int *bigger ထဲ ထည့်ပြီး NULL မဟုတ်မှ bigger[count] = value; ရေးပါ။ main ထဲမှာ append ရဲ့ ရလဒ် NULL မဟုတ်မှ copy ကို အစားထိုးပါ။
အဖြေ ကြည့်ရန်
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Returns a heap copy of src, or NULL. The caller must free() it. */
int *duplicate(const int *src, size_t count) {
if (src == NULL || count == 0 || count > SIZE_MAX / sizeof *src) {
return NULL;
}
int *copy = malloc(count * sizeof *copy);
if (copy == NULL) {
return NULL;
}
memcpy(copy, src, count * sizeof *copy);
return copy;
}
/* Grows arr by one slot. On failure returns NULL and arr is untouched. */
int *append(int *arr, size_t count, int value) {
if (count >= SIZE_MAX / sizeof *arr) {
return NULL;
}
int *bigger = realloc(arr, (count + 1) * sizeof *bigger);
if (bigger == NULL) {
return NULL;
}
bigger[count] = value;
return bigger;
}
static void print_all(const char *label, const int *values, size_t count) {
printf("%s:", label);
for (size_t i = 0; i < count; i++) {
printf(" %d", values[i]);
}
printf("\n");
}
int main(void) {
int original[] = {4, 8, 15};
size_t count = 3;
int *copy = duplicate(original, count);
if (copy == NULL) {
return EXIT_FAILURE;
}
print_all("copy", copy, count);
int extra[] = {16, 23};
for (size_t i = 0; i < 2; i++) {
int *grown = append(copy, count, extra[i]);
if (grown == NULL) {
free(copy);
return EXIT_FAILURE;
}
copy = grown;
count++;
}
print_all("after append", copy, count);
print_all("original", original, 3);
free(copy);
copy = NULL;
int *none = duplicate(NULL, 3);
printf("duplicate(NULL, 3) returns %s\n", none == NULL ? "NULL" : "memory");
free(none); /* free(NULL) is allowed and does nothing */
return EXIT_SUCCESS;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- malloc, calloc, realloc က run time မှာ အရွယ်အစား ရွေးလို့ရပြီး lifetime ကို ကိုယ်တိုင် ထိန်းရတဲ့ heap memory ကို ပေးပါတယ်။
- Allocation တိုင်းကို NULL စစ်ပြီး အရွယ်အစားကို count * sizeof *p နဲ့ တွက်ပါ။
- realloc ရလဒ်ကို temporary pointer ထဲ ထည့်ပါ — fail ဖြစ်ရင် block အဟောင်း မှန်ကန်ဆဲပါ။
- Block တစ်ခုကို free တစ်ကြိမ်ပဲ လုပ်ပြီး pointer ကို NULL ထားပါ — free ပြီးသား memory ကို သုံးတာ undefined behaviour ပါ။
SEI CERT C Coding Standard — Carnegie Mellon SEI