Allocation fail နိုင်ပြီးsuccessful allocation တစ်ခုစီတွင်owner တစ်ခုနဲ့matching free တစ်ကြိမ်ရှိရပါမယ်။
ပိုပြီးနားလည်ထားရမယ့် အချက်
Dynamic allocation တွင်requested size overflow, allocation failure, initialization, ownership transfer, exact-one free နဲ့use-after-free ကိုထိန်းရပါတယ်။ `sizeof *ptr` pattern, checked size arithmetic နဲ့single cleanup path ကိုသုံးပါ။
လက်တွေ့မှာ ဘယ်လိုအသုံးချမလဲ
User count အလိုက်`calloc` array allocate လုပ်ပြီး failure, zero/large count, normal use နဲ့free-after-use ကိုAddressSanitizer ဖြင့်စမ်းပါ။
ဒီသင်ခန်းစာပြီးရင်
#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;
}0 10 20 30 40ကိုယ်တိုင်စမ်းကြည့်ရန်
User count အလိုက်`calloc` array allocate လုပ်ပြီး failure, zero/large count, normal use နဲ့free-after-use ကိုAddressSanitizer ဖြင့်စမ်းပါ။
Memory/Safety သတိပေးချက်
`malloc` result မစစ်ခြင်း၊size multiplication overflow နဲ့free ပြီးpointer ဆက်သုံးခြင်းကcritical memory bugs ဖြစ်ပါတယ်။
SEI CERT C Coding Standard — Carnegie Mellon SEI