Thuta Learning
C
ProjectsProgrammingအခြေခံ

Summary: C အားလုံးကို program တစ်ခုထဲမှာ

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

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

  • Struct၊ pointer၊ dynamic memory နဲ့ file တွေကို အလုပ်လုပ်တဲ့ program တစ်ခုထဲမှာ ချိတ်ဆက်နိုင်ဖို့
  • Memory တစ်ခုချင်းစီကို ဘယ်သူပိုင်လဲ၊ ဘယ်မှာ ပြန်လွှတ်လဲ ရှင်းပြနိုင်ဖို့
  • Fail ဖြစ်နိုင်တဲ့ call တိုင်းရဲ့ return value ကို စစ်နိုင်ဖို့
  • Course ပြီးရင် ဆက်လေ့လာရမယ့် multi-file build၊ debugger၊ sanitizer တွေကို ရွေးနိုင်ဖို့

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

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

C အကြောင်း အများကြီး တွေ့ခဲ့ပြီးပါပြီ။ Variable နဲ့ type၊ operator၊ branch နဲ့ loop၊ array နဲ့ string၊ function နဲ့ recursion၊ input၊ file၊ address နဲ့ pointer၊ malloc နဲ့ free၊ struct၊ union နဲ့ enum တွေပါ။ ဒီ lesson မှာ အသစ် ဘာမှ မထည့်ဘဲ သူတို့ကို အတူတူ ချိတ်ပေးပါမယ်။

Course ကို tool box တစ်ခုလို မြင်ကြည့်ပါ။ Lesson တစ်ခုစီက tool တစ်ခုစီ ပေးခဲ့ပါတယ်။ နေ့စဉ် အသုံးစရိတ်တွေကို စာရင်းလုပ်ပြီး disk ထဲ သိမ်းတာလို အလုပ်အစစ်တစ်ခုမှာတော့ tool အများကြီးကို တစ်ပြိုင်နက် လိုပါတယ်။ ကျွမ်းကျင်မှုဆိုတာ ဘယ် tool ကို ရွေးရမလဲ၊ သူတို့ ဘယ်လို ကိုက်ညီလဲ သိတာပါ။ အောက်က capstone example က အဲဒီ အလုပ်ပဲ ဖြစ်ပြီး line တိုင်းက သင်သိပြီးသား တစ်ခုခုကို သုံးထားပါတယ်။

Problem → Why → Solution

ပြဿနာ: ငါ့ စက်မှာ တစ်ခါ အလုပ်လုပ်ခဲ့တယ်

မှားနေတဲ့ program ကိုလည်း C က ပျော်ပျော်ကြီး compile လုပ်ပေးပါတယ်။ Array အဆုံးကို element တစ်ခု ကျော်ဖတ်တာ၊ free လုပ်ပြီးသား memory ကို သုံးတာ၊ signed int ကို overflow ဖြစ်စေတာ၊ fopen အောင်မြင်လား မစစ်တာ စတာတွေ အားလုံးက ဒီနေ့ အဖြေမှန် print ထုတ်ပြီး မနက်ဖြန် crash ဖြစ်နိုင်ပါတယ်။ ဒါမှမဟုတ် တခြား computer မှာမှ၊ optimisation ဖွင့်ထားမှသာ ပျက်နိုင်ပါတယ်။ C standard က ဒီလို အခြေအနေတွေကို undefined behaviour လို့ ခေါ်ပြီး နောက်မှာ ဘာဖြစ်ရမလဲဆိုတာကို ဘာ requirement မှ မသတ်မှတ်ထားပါဘူး။

ဒါကြောင့် မှန်သလို ထင်ရတဲ့ run တစ်ခုက သိပ်ပြီး ဘာမှ သက်သေမပြပါဘူး။ Bounds check၊ return value check၊ malloc တိုင်းအတွက် ရှင်းလင်းတဲ့ owner၊ warning ဖွင့်ထားတာ စတဲ့ ဒီ course က ပျိုးထောင်ပေးခဲ့တဲ့ အလေ့အကျင့်တွေကပဲ တိုက်ဆိုင်လို့ အလုပ်လုပ်နေတဲ့ program နဲ့ တကယ်မှန်တဲ့ program ကို ခွဲခြားပေးပါတယ်။

ဘာကြောင့် run ကောင်းတစ်ခါက သက်သေမဟုတ်တာလဲ

Undefined behaviour ဆိုတာ သတ်သတ်မှတ်မှတ် error တစ်ခု မဟုတ်ပါဘူး။ ဘာ rule မှ မရှိတာ ဖြစ်ပါတယ်။ Compiler က အဲဒါ ဘယ်တော့မှ မဖြစ်ဘူးလို့ ယူဆပြီး optimise လုပ်နိုင်လို့ လက္ခဏာတွေက နေရာရွှေ့သွားတာ၊ ပျောက်သွားတာ၊ အမှားအစစ်နဲ့ ဝေးတဲ့နေရာမှာ ပေါ်လာတာ ဖြစ်နိုင်ပါတယ်။ Code ထဲက check တွေနဲ့ -Wall -Wextra၊ sanitizer လို tool တွေကသာ ဒီ bug တွေကို မြင်သာအောင် လုပ်ပေးပါတယ်။

Core concept: C program တိုင်းကို ဖြတ်သွားတဲ့ rule တွေ

နောက်ကို ပြန်ကြည့်ရင် lesson တော်တော်များများက rule အနည်းငယ်ထဲက တစ်ခုခုကို သင်ပေးခဲ့တာပါ။ Syntax က လွယ်တဲ့အပိုင်းပါ။ အတွေ့အကြုံရှိတဲ့ C programmer တွေက line တိုင်းမှာ စဉ်းစားနေတာက ဒီ rule တွေပါ။

Ideaမှတ်ထားရမယ့် rule
Type နဲ့ integerint / int က ဒသမကို ဖြတ်ပစ်ပြီး signed overflow က undefined ဖြစ်လို့ ငွေကို cents အပြည့်နဲ့ သိမ်းပါ
Array နဲ့ stringArray က သူ့ length ကို မသိပါဘူး။ Pointer တိုင်းနဲ့ size ကို တွဲပေးပြီး '\0' အတွက် နေရာချန်ပါ
Pointerညွှန်ထားတဲ့ object အသက်ရှင်နေသရွေ့ပဲ pointer က valid ဖြစ်ပြီး NULL က "object မရှိ" လို့ ဆိုလိုပါတယ်
Dynamic memorymalloc ဒါမှမဟုတ် realloc တိုင်းမှာ နာမည်ရှင်းတဲ့ owner တစ်ယောက်က free ကို တစ်ကြိမ်အတိအကျ ခေါ်ရပါမယ်
Errormalloc၊ realloc၊ fopen၊ scanf၊ fprintf အားလုံး fail ဖြစ်နိုင်လို့ return ပြန်တာကို စစ်ပါ
Build-Wall -Wextra နဲ့ compile လုပ်ပြီး warning တွေကို ပြင်ပါ။ Linux ဒါမှမဟုတ် macOS မှာ စမ်းနေတုန်း -fsanitize=address,undefined ထည့်ပါ

Compilation model ကလည်း အဲဒီ rule တွေရဲ့ အစိတ်အပိုင်းပါ။ Preprocessor က #include file တွေကို ကူးထည့်ပြီး #define တွေကို ချဲ့ပေးပါတယ်။ Compiler က .c file တစ်ခုချင်းကို object file အဖြစ် ပြောင်းပြီး linker က အဲဒီ object file တွေကို C library နဲ့ ပေါင်းပေးပါတယ်။ Prototype ကျန်ခဲ့တာက compiler ပြဿနာ ဖြစ်ပြီး "undefined reference" ဆိုတာကတော့ linker ပြဿနာပါ။ Error ဘယ်အဆင့်က လာလဲ သိရင် ဘယ်နေရာ ရှာရမလဲ သိပါတယ်။

ဒီ rule တွေကို object lifetime က ချိတ်ပေးထားပါတယ်။ Local variable က သူ့ block ပြီးတဲ့အထိ အသက်ရှင်ပါတယ်။ static ဒါမှမဟုတ် global variable က program တစ်ခုလုံး အသက်ရှင်ပါတယ်။ malloc နဲ့ ယူထားတဲ့ block ကတော့ တစ်ယောက်ယောက်က free မခေါ်မချင်း ရှိနေပါတယ်။ ပြင်းထန်တဲ့ C bug အများစုက ဒီနှစ်မျိုးထဲက တစ်မျိုးပါ။ Pointer က သူ့ object ထက် ပိုကြာကြာ အသက်ရှင်နေတာ (use after free၊ local ရဲ့ address ကို return ပြန်တာ) ဒါမှမဟုတ် object က သူ့ကို ညွှန်တဲ့ pointer အားလုံးထက် ပိုကြာနေတာ (leak) ပါ။

Language ကိုယ်တိုင်လည်း ဆက်တိုးတက်နေပါတယ်။ C23 က standard ရဲ့ နောက်ဆုံး edition ဖြစ်ပြီး project တော်တော်များများကတော့ C11 ဒါမှမဟုတ် C17 နဲ့ compile လုပ်နေဆဲပါ။ ဒီ course ရဲ့ example တွေကိုလည်း C17 နဲ့ စစ်ထားပါတယ်။ အပေါ်က rule တွေက version တိုင်းမှာ အတူတူပဲ မှန်ပါတယ်။

Mental model: ဖန်တီး၊ သုံး၊ စစ်၊ ပြန်လွှတ်
  1. ဖန်တီး: object ကို declare/allocate လုပ်
  2. စစ်ထားတဲ့ pointer တွေကနေ သုံး
  3. Fail ဖြစ်နိုင်တဲ့ call တိုင်းကို စစ်
  4. လမ်းကြောင်းတစ်ခုတည်းမှာ တစ်ကြိမ်ပဲ လွှတ်

Pointer တိုင်းကို မေးပါ: ဘယ် object ကို ညွှန်နေတာလဲ၊ အဲဒီ object က အသက်ရှင်နေသေးလား။

အခြေခံ ပုံစံ: cleanup label တစ်ခုတည်း

c
int run(void) {
    int status = 1;                    /* assume failure */
    FILE *f = NULL;
    struct Thing *t = malloc(sizeof *t);
    if (t == NULL || (f = fopen("out.txt", "w")) == NULL) goto cleanup;
    /* ... work; any failure jumps to cleanup ... */
    status = 0;                        /* reached only on success */
cleanup:
    if (f != NULL) fclose(f);
    free(t);                           /* free(NULL) does nothing */
    return status;
}

Resource တိုင်းကို NULL နဲ့ စပြီး ရလဒ်ကို failure လို့ အရင် မှတ်ထားပါ။ တစ်ခုခု မှားသွားရင် label တစ်ခုတည်းဆီ jump လုပ်ပါ။ Cleanup block က ရထားသမျှကို ပြန်လွှတ်ပါတယ်။ fclose ကို guard လုပ်ထားပြီး free(NULL) က ဘာမှ မလုပ်လို့ ဘေးကင်းပါတယ်။

Capstone: save နဲ့ load ကို ကျော်ဖြတ်နိုင်တဲ့ expense list

Program က အသုံးစရိတ် သုံးခုကို ကြီးလာတဲ့ heap array ထဲ မှတ်ပါတယ်။ ပြီးရင် သူတို့ကို temporary file တစ်ခုထဲ text အဖြစ် ရေးတယ်၊ အဲဒီ file ကို ဒုတိယ list ထဲ ပြန်ဖတ်တယ်၊ total နှစ်ခု ကိုက်လားဆိုတာ စစ်ပါတယ်။ ငွေကို long နဲ့ cents အပြည့်အဖြစ် သိမ်းလို့ 12.50 က 1250 ဖြစ်ပြီး rounding error ဝင်လာစရာ မရှိပါဘူး။ tmpfile() က close လုပ်တဲ့အခါ အလိုအလျောက် ဖျက်ပစ်တဲ့ scratch file တစ်ခု ပေးလို့ disk ပေါ်မှာ ဘာမှ မကျန်ခဲ့ပါဘူး။ အပိုင်းတစ်ခုစီက ဘယ် topic ကို ပြန်သုံးလဲဆိုတာ ဒီမှာပါ:

  • Struct: Expense က item နဲ့ ဈေးနှုန်းကို စုထားပြီး ExpenseList က array ကို count နဲ့ capacity နဲ့ တွဲထားပါတယ်
  • Pointer: function တိုင်းက list ကို pointer နဲ့ ယူပြီး ဖတ်ရုံပဲဆိုရင် const သုံးပါတယ်
  • Dynamic memory: realloc က array ကို ကြီးအောင်လုပ်ပြီး list_free က ပြန်လွှတ်ပါတယ်
  • File: save_list က expense တစ်ခုကို line တစ်ကြောင်း ရေးပြီး load_list က fgets နဲ့ sscanf နဲ့ ပြန် parse လုပ်ပါတယ်
  • String: snprintf က item နာမည်ကို copy ကူးပြီး ငွေကို buffer မကျော်ဘဲ format လုပ်ပါတယ်
expenses.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Expense {
    char item[24];
    long cents;                  /* whole cents: integer maths never rounds */
};

struct ExpenseList {
    struct Expense *data;        /* heap array owned by the list */
    size_t count;
    size_t capacity;
};

/* Returns 0 on success, -1 if memory ran out (the list is left unchanged). */
int list_add(struct ExpenseList *list, const char *item, long cents) {
    if (list->count == list->capacity) {
        size_t cap = list->capacity == 0 ? 2 : list->capacity * 2;
        struct Expense *bigger = realloc(list->data, cap * sizeof *bigger);
        if (bigger == NULL) {
            return -1;
        }
        list->data = bigger;
        list->capacity = cap;
    }
    struct Expense *e = &list->data[list->count];
    snprintf(e->item, sizeof e->item, "%s", item);
    e->cents = cents;
    list->count++;
    return 0;
}

long list_total(const struct ExpenseList *list) {
    long sum = 0;
    for (size_t i = 0; i < list->count; i++) {
        sum += list->data[i].cents;
    }
    return sum;
}

void list_free(struct ExpenseList *list) {
    free(list->data);
    list->data = NULL;
    list->count = list->capacity = 0;
}

/* One "item;cents" line per expense. */
int save_list(FILE *f, const struct ExpenseList *list) {
    for (size_t i = 0; i < list->count; i++) {
        if (fprintf(f, "%s;%ld\n", list->data[i].item, list->data[i].cents) < 0) {
            return -1;
        }
    }
    return 0;
}

int load_list(FILE *f, struct ExpenseList *list) {
    char line[64], item[24];
    long cents;
    while (fgets(line, sizeof line, f) != NULL) {
        if (sscanf(line, "%23[^;];%ld", item, &cents) != 2) {
            return -1;
        }
        if (list_add(list, item, cents) != 0) {
            return -1;
        }
    }
    return 0;
}

/* Writes "12.50" into buf; the caller says how big buf is. */
void format_money(long cents, char *buf, size_t size) {
    snprintf(buf, size, "%ld.%02ld", cents / 100, cents % 100);
}

int main(void) {
    int status = 1;
    FILE *f = NULL;
    struct ExpenseList today = {NULL, 0, 0};
    struct ExpenseList loaded = {NULL, 0, 0};
    char money[32];

    if (list_add(&today, "rice", 1250) != 0 || list_add(&today, "bus fare", 300) != 0 ||
        list_add(&today, "notebook", 2725) != 0) {
        goto cleanup;
    }

    f = tmpfile();
    if (f == NULL || save_list(f, &today) != 0) {
        goto cleanup;
    }
    printf("Saved %zu expenses\n", today.count);

    rewind(f);
    if (load_list(f, &loaded) != 0) {
        goto cleanup;
    }
    printf("Loaded %zu expenses back:\n", loaded.count);
    for (size_t i = 0; i < loaded.count; i++) {
        format_money(loaded.data[i].cents, money, sizeof money);
        printf("  %-10s %8s\n", loaded.data[i].item, money);
    }
    format_money(list_total(&loaded), money, sizeof money);
    printf("Total: %s\n", money);
    printf("Round trip matches: %s\n",
           list_total(&loaded) == list_total(&today) ? "yes" : "no");
    status = 0;

cleanup:                          /* one exit path releases everything */
    if (f != NULL) {
        fclose(f);
    }
    list_free(&today);
    list_free(&loaded);
    return status;
}

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

  1. Grow safely with reallocstruct Expense *bigger = realloc(list->data, cap * sizeof *bigger);

    ရလဒ်ကို pointer အသစ်ထဲ အရင်ထည့်ပါတယ်။ realloc fail ဖြစ်ရင် NULL ပြန်ပေမယ့် block အဟောင်းက valid ဖြစ်နေဆဲပြီး list->data ကပဲ ပိုင်ဆိုင်နေဆဲပါ။ ဒါကြောင့် ဘာမှ leak မဖြစ်ပါဘူး။ list->data = realloc(list->data, ...) လို့ ရေးရင် အဲဒီ block ကို ညွှန်တဲ့ တစ်ခုတည်းသော pointer ကို overwrite လုပ်ပစ်ပါလိမ့်မယ်။

  2. Bounded string copysnprintf(e->item, sizeof e->item, "%s", item);

    snprintf က sizeof e->item byte ထက် ပို မရေးဘဲ string ကို '\0' နဲ့ အမြဲ ပိတ်ပေးပါတယ်။ ဒါကြောင့် နာမည်ရှည်ရင် struct ကို ကျော်မထွက်ဘဲ ဖြတ်တောက်ခံရပါတယ်။ strcpy မှာ size မရှိလို့ 24 byte ကို ကျော်ရေးသွားပါလိမ့်မယ်။

  3. Parse what you savedif (sscanf(line, "%23[^;];%ld", item, &cents) != 2) {

    %23[^;] က semicolon မဟုတ်တဲ့ character 23 လုံးအထိ ဖတ်လို့ "bus fare" လို space ပါတဲ့ item နာမည်တွေကို လက်ခံပါတယ်။ Field နှစ်ခုလုံး မထွက်တဲ့ line တွေ့ရင် record တစ်ဝက်တစ်ပျက် load မလုပ်ဘဲ load_list က fail ဖြစ်ပါတယ်။

  4. Money from centssnprintf(buf, size, "%ld.%02ld", cents / 100, cents % 100);

    Integer division က ကျပ်ပြည့်/ဒေါ်လာပြည့် ယူနစ်ကို ပေးပြီး % က cents ကို ပေးပါတယ်။ %02ld က 0 ကို "00" ဖြစ်အောင် ဖြည့်ပေးပါတယ်။ Function က buf ကို သူ့ size နဲ့ တွဲယူပါတယ်။ Array တွေမှာ သုံးခဲ့တဲ့ pointer နဲ့ length တွဲပေးတဲ့ ပုံစံပါပဲ။ ဒီ function က အနုတ်ပမာဏအတွက် မဒီဇိုင်းထားပါဘူး။

  5. Back to the start of the filerewind(f);

    ရေးပြီးတဲ့အခါ file position က အဆုံးမှာ ရှိနေပါတယ်။ rewind က အစကို ပြန်ရွှေ့ပေးလို့ save_list ရေးခဲ့တာကို load_list က ဖတ်နိုင်ပါတယ်။ Stream တစ်ခုတည်းမှာ ရေးရာကနေ ဖတ်ရာကို ပြောင်းတဲ့အခါ ကြားမှာ rewind၊ fseek ဒါမှမဟုတ် fflush လို call တစ်ခု ရှိရမယ်လို့ C standard ကလည်း သတ်မှတ်ထားပါတယ်။

  6. One exit for every outcomecleanup:

    Failure တိုင်းက status 1 နဲ့ ဒီကို jump လုပ်ပြီး success က status 0 နဲ့ ရောက်လာပါတယ်။ ဘယ်လမ်းကြောင်းကပဲ လာလာ list နှစ်ခုလုံးကို free လုပ်ပြီး file ကို တစ်ကြိမ်တည်း close လုပ်ပါတယ်။ free(NULL) က ဘာမှ မလုပ်လို့ list အလွတ်ပေါ်မှာ list_free ခေါ်တာ ဘေးကင်းပါတယ်။

You should see
Saved 3 expenses
Loaded 3 expenses back:
  rice          12.50
  bus fare       3.00
  notebook      27.25
Total: 42.75
Round trip matches: yes

ဒီ course ပြီးရင် ဘယ်ကို ဆက်သွားမလဲ

Core language ကို အခု ရပြီးပါပြီ။ အောက်က skill တွေက အဲဒါကို လက်တွေ့ C ဖြစ်လာအောင် ပြောင်းပေးတာတွေပါ။ အကျိုးရှိတဲ့ အစဉ်အတိုင်း ခန့်မှန်းခြေ စီထားပါတယ်:

  • Code ကို .h header တွေနဲ့ .c file အများအပြားအဖြစ် ခွဲပြီး make ဒါမှမဟုတ် CMake နဲ့ build လုပ်တာ
  • gdb၊ lldb ဒါမှမဟုတ် editor ထဲက debugger နဲ့ run နေတဲ့ program ကို တစ်ဆင့်ချင်း လိုက်ကြည့်တာ
  • Linux ဒါမှမဟုတ် macOS မှာ -fsanitize=address,undefined နဲ့ test တွေ run ပြီး out-of-bounds access၊ use after free၊ overflow တွေကို ဖမ်းတာ
  • Linked list၊ hash table၊ dynamic string လို classic data structure တွေကို ကိုယ်တိုင် ဆောက်ကြည့်တာ
  • Library function တစ်ခုချင်းရဲ့ return value နဲ့ error case တွေကို man page ဒါမှမဟုတ် cppreference မှာ ဖတ်တာ

နောက်ဆုံး error မဟုတ်ဘဲ ပထမ error ကို ဖတ်ပါ

Compiler က message အစောင်နှစ်ဆယ်လောက် ထုတ်လာရင် ပထမတစ်ခုကို ပြင်ပြီး ပြန် compile လုပ်ပါ။ Semicolon ဒါမှမဟုတ် brace တစ်ခု ကျန်ခဲ့တာက ကျန်တဲ့ message အားလုံးကို ဖြစ်စေတတ်ပြီး ပြင်လိုက်တာနဲ့ အားလုံး အတူတူ ပျောက်သွားတတ်ပါတယ်။ Message တွေက ကြမ်းတမ်းသလို ထင်ရပေမယ့် file နာမည်၊ line number နဲ့ ပြဿနာကို သဲလွန်စ ကောင်းကောင်း ပေးလေ့ရှိပါတယ်။

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

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

ဒီ capstone ရဲ့ ပုံသဏ္ဌာန်ဖြစ်တဲ့ heap array ကို ပိုင်ဆိုင်တဲ့ struct တစ်ခုနဲ့ add၊ save၊ load၊ free လုပ်တဲ့ function တွေဆိုတာ production C အများကြီးကို ရေးတဲ့ ပုံစံပါ။ SQLite၊ Redis နဲ့ Linux kernel လို project တွေကို C နဲ့ ရေးထားပြီး exception အစား init၊ add၊ free function တွေပါတဲ့ struct သေးသေးလေးတွေ၊ ဂရုတစိုက်ထိန်းထားတဲ့ length field တွေနဲ့ error code တွေကို အဓိကသုံးပါတယ်။ main() ထဲက goto cleanup ပုံစံကို Linux kernel မှာ အများကြီး တွေ့ရပြီး အကြောင်းရင်းက ဒီမှာ သုံးတာနဲ့ အတူတူပါပဲ။ အားလုံးကို ပြန်လွှတ်ပေးတဲ့ ထွက်ပေါက်တစ်ခုတည်း ရှိဖို့ပါ။

C code ကို ship မလုပ်ခင် team တွေက check တစ်မျိုးထက် ပိုပြီး ဖြတ်ကြပါတယ်။ -Wall -Wextra နဲ့ warning ကင်းတဲ့ build (CI မှာ -Werror ပါ ထည့်လေ့ရှိတယ်)၊ function တစ်ခုချင်းအတွက် unit test၊ တိတ်တဆိတ်ဖြစ်နေတဲ့ memory error တွေကို ကျယ်ကျယ်လောင်လောင် report ဖြစ်အောင် လုပ်ပေးတဲ့ sanitizer run တွေနဲ့ ပြင်ပ input ကို parse လုပ်တဲ့ code အတွက် fuzzing တွေပါ။ တစ်ခုစီက ကျန်တဲ့ check တွေ မဖမ်းမိတဲ့ bug တွေကို ဖမ်းပေးပါတယ်။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

realloc ရလဒ်နဲ့ တစ်ခုတည်းသော pointer ကို overwrite လုပ်တာ

list->data = realloc(list->data, n) က သပ်ရပ်သလို ထင်ရပါတယ်။ ဒါပေမဲ့ realloc က NULL ပြန်ရင် block အဟောင်းက allocate လုပ်ထားဆဲဖြစ်ပြီး သူ့ address ကို ပျောက်သွားပါပြီ။ Leak ဖြစ်တဲ့အပြင် နောက် line က dereference လုပ်မယ့် NULL data pointer တစ်ခုပါ ကျန်ခဲ့ပါတယ်။ Temporary ထဲကို assign လုပ်ပြီး စစ်ပြီးမှ copy ကူးပါ။

float ကို ငွေအဖြစ် သုံးတာ

Binary floating point မှာ 0.1 + 0.2 က 0.3 အတိအကျ မဟုတ်ပါဘူး။ ဒါကြောင့် value တွေ အများကြီး ပေါင်းပြီး နှိုင်းယှဉ်တဲ့အခါ total က cent တစ်ပြားလောက် လွဲသွားတတ်ပါတယ်။ Cents အပြည့်ကို integer type နဲ့ သိမ်းပြီး print ထုတ်တဲ့အခါမှ ပြောင်းပါ။

File ကို rewind ဒါမှမဟုတ် close လုပ်ဖို့ မေ့တာ

Stream တစ်ခုတည်းမှာ rewind ဒါမှမဟုတ် fseek မပါဘဲ ရေးရာကနေ ဖတ်ရာ ပြောင်းတာကို C standard က define မလုပ်ထားပါဘူး။ အလုပ်လုပ်သလို ထင်ရရင်တောင် position က file အဆုံးမှာ ရှိနေလို့ ဖတ်စရာ ဘာမှ မကျန်ပါဘူး။ fclose မေ့ရင် fprintf က memory ထဲမှာ ကိုင်ထားဆဲ buffer data တွေ ပျောက်သွားနိုင်ပြီး file handle လည်း leak ဖြစ်ပါတယ်။

လမ်းကြောင်းမတူတဲ့နေရာတွေမှာ နှစ်ခါ free လုပ်တာ

Error branch တိုင်းမှာ free ခေါ်ပြီး function အဆုံးမှာလည်း ထပ် free ရင် တစ်နေ့နေ့ လမ်းကြောင်းတစ်ခုက block တစ်ခုတည်းကို နှစ်ခါ free လုပ်မိပါလိမ့်မယ်။ ဒါက undefined behaviour ပါ။ Cleanup label တစ်ခုတည်း ထားပြီး free ပြီးတိုင်း pointer ကို NULL ပြန်ထားရင် double free ဖြစ်ဖို့ အများကြီး ခက်သွားပါတယ်။

Mini challenge

Capstone challenge: file ထဲက စကားလုံးတွေကို ရေတွက်ပါ

Course တစ်ခုလုံးကို ပေါင်းသုံးတဲ့ word-frequency counter တစ်ခု ရေးပါ။ File ထဲက စကားလုံးတွေကို ဖတ်ပြီး realloc နဲ့ ကြီးလာတဲ့ struct array ထဲမှာ တစ်ခုချင်း ရေတွက်ပါ။ အများဆုံး ပါတဲ့ စကားလုံးကို report လုပ်ပါ။ စာကြောင်းတစ်ကြောင်းနဲ့ရော file အလွတ်နဲ့ပါ စမ်းပါ။

  • struct WordCount က စကားလုံး (character 31 လုံးအထိ) နဲ့ သူ့ count ကို သိမ်းပါ
  • find_word() က ရှိပြီးသား entry ရဲ့ pointer ဒါမှမဟုတ် NULL ကို ပြန်ပေးပါ
  • add_word() က ရှိပြီးသား စကားလုံးကို တိုးပေးပြီး အသစ်ဆိုရင် ပြည့်နေတဲ့ array ကို ကြီးအောင်လုပ်ပြီး ထည့်ပါ
  • "The" နဲ့ "the" တူအောင် မရေတွက်ခင် tolower နဲ့ စာလုံးအသေး ပြောင်းပါ
  • File အလွတ် အပါအဝင် လမ်းကြောင်းတိုင်းမှာ array နဲ့ file နှစ်ခုလုံးကို ပြန်လွှတ်ပါ
word_count.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct WordCount {
    char word[32];
    int count;
};

struct WordList {
    struct WordCount *items;
    size_t count;
    size_t capacity;
};

struct WordCount *find_word(struct WordList *list, const char *word) {
    // TODO: linear search with strcmp; NULL when the word is absent
}

int add_word(struct WordList *list, const char *word) {
    // TODO: bump an existing count, or grow with realloc and append
    // TODO: return -1 if memory runs out, 0 otherwise
}

int count_words(FILE *f, struct WordList *list) {
    // TODO: read with fscanf(f, "%31s", ...), lower-case each word, add it
}

void report(const struct WordList *list) {
    // TODO: "No words" when empty; else word=count pairs, then the top word
}

int main(void) {
    // TODO: for "The cat and the dog and THE bird\n" and for "":
    //       tmpfile(), fputs the text, rewind, count, report, free, fclose
}

ရလဒ်: စာကြောင်းအတွက် the=3 cat=1 and=2 dog=1 bird=1 ပြီးရင် "Most frequent: the (3)" ထွက်ပါမယ်။ File အလွတ်အတွက် "No words" ထွက်ပါမယ်။ Run နှစ်ခုလုံးမှာ allocation တိုင်းကို free လုပ်ပါတယ်။

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

fscanf ကို %31s နဲ့ သုံးရင် whitespace တွေကို ကျော်ပြီး နောက် space မှာ ရပ်ပါတယ်။ ဒါကြောင့် ခေါ်တိုင်း စကားလုံးတစ်လုံးပဲ ရပြီး character 31 လုံးထက် ဘယ်တော့မှ မပိုပါဘူး။ Character တစ်လုံးချင်းကို tolower((unsigned char)c) ဖြတ်ပါ။ File အလွတ်အတွက် count_words က ဘာမှ မထည့်ပါဘူး။ report ကတော့ items[0] ကို မထိခင် list->count ကို စစ်ရပါမယ်။

အဖြေ ကြည့်ရန်
word_count.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct WordCount {
    char word[32];
    int count;
};

struct WordList {
    struct WordCount *items;
    size_t count;
    size_t capacity;
};

struct WordCount *find_word(struct WordList *list, const char *word) {
    for (size_t i = 0; i < list->count; i++) {
        if (strcmp(list->items[i].word, word) == 0) {
            return &list->items[i];
        }
    }
    return NULL;
}

/* Counts one more use of word. Returns 0 on success, -1 if out of memory. */
int add_word(struct WordList *list, const char *word) {
    struct WordCount *found = find_word(list, word);
    if (found != NULL) {
        found->count++;
        return 0;
    }
    if (list->count == list->capacity) {
        size_t cap = list->capacity == 0 ? 4 : list->capacity * 2;
        struct WordCount *bigger = realloc(list->items, cap * sizeof *bigger);
        if (bigger == NULL) {
            return -1;
        }
        list->items = bigger;
        list->capacity = cap;
    }
    struct WordCount *slot = &list->items[list->count++];
    snprintf(slot->word, sizeof slot->word, "%s", word);
    slot->count = 1;
    return 0;
}

/* Reads every word from f into list, lower-cased. 0 = ok, -1 = no memory. */
int count_words(FILE *f, struct WordList *list) {
    char word[32];
    while (fscanf(f, "%31s", word) == 1) {
        for (char *p = word; *p != '\0'; p++) {
            *p = (char)tolower((unsigned char)*p);
        }
        if (add_word(list, word) != 0) {
            return -1;
        }
    }
    return 0;
}

void report(const struct WordList *list) {
    if (list->count == 0) {
        printf("No words\n");
        return;
    }
    const struct WordCount *top = &list->items[0];
    for (size_t i = 0; i < list->count; i++) {
        const struct WordCount *w = &list->items[i];
        printf("%s%s=%d", i == 0 ? "" : " ", w->word, w->count);
        if (w->count > top->count) {
            top = w;
        }
    }
    printf("\nMost frequent: %s (%d)\n", top->word, top->count);
}

int run(const char *text) {
    FILE *f = tmpfile();
    if (f == NULL) {
        return -1;
    }
    fputs(text, f);
    rewind(f);

    struct WordList list = {NULL, 0, 0};
    int status = count_words(f, &list);
    if (status == 0) {
        report(&list);
    }
    free(list.items);
    fclose(f);
    return status;
}

int main(void) {
    if (run("The cat and the dog and THE bird\n") != 0 || run("") != 0) {
        printf("Something failed\n");
        return 1;
    }
    return 0;
}

Quick Quiz 1 / 3

Capstone ထဲက format_money(-250, buf, sizeof buf) က buf ထဲကို ဘာထည့်မလဲ။

Quick Quiz 2 / 3

list_add က list->data = realloc(list->data, ...) လို့ မရေးဘဲ realloc ရလဒ်ကို bigger ထဲ ဘာကြောင့် သိမ်းတာလဲ။

Quick Quiz 3 / 3

tmpfile() fail ဖြစ်ပြီး main() က cleanup ကို jump လုပ်တယ်။ ဘယ်အချက်က မှန်လဲ။

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

  • C rule အများစုက lifetime၊ bounds၊ integer behaviour နဲ့ ownership ဆီ ပြန်ရောက်လာပါတယ်
  • malloc တိုင်းကို owner တစ်ယောက်နဲ့ free တစ်ကြိမ် ပေးပါ၊ realloc ကို temporary pointer ထဲ ထည့်ပါ
  • Fail ဖြစ်နိုင်တဲ့ call တိုင်းကို စစ်ပြီး resource တွေကို cleanup လမ်းကြောင်း တစ်ခုတည်းမှာ လွှတ်ပါ
  • ငွေကို integer cents နဲ့ သိမ်းပြီး buffer တိုင်းကို သူ့ size နဲ့ တွဲပေးပါ
  • နောက်တစ်ဆင့်: multi-file build၊ debugger၊ sanitizer နဲ့ ကိုယ်ပိုင် data structure တွေ

ISO C Working Group (WG14) — ISO/IEC JTC1/SC22/WG14

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

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

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

Summary: C အားလုံးကို program တစ်ခုထဲမှာ | Thuta Learning