C storage class และ lifetime



3 แนวคิดที่ต่างกันแต่มักปนกัน:

  • scope — โค้ดตรงไหน “เห็น” ชื่อนี้ (block, file)
  • lifetime — ตัวแปรมีชีวิตอยู่ช่วงไหน (ตลอดโปรแกรม, ตลอดฟังก์ชัน)
  • linkage — ชื่อเดียวกันในไฟล์ต่างกันหมายถึงตัวเดียวกันไหม

ตัวแปร local ปกติ (automatic)

void f(void)
{
    int x = 0;             // automatic storage
    x++;
    printf("%d\n", x);     // 1 ทุกครั้งที่เรียก f
}
  • scope: ในฟังก์ชัน (block ที่ประกาศ)
  • lifetime: ตั้งแต่ประกาศ ถึงจบ block — เกิดใหม่ทุกครั้งที่เข้า
  • linkage: ไม่มี
  • keyword auto มีอยู่แต่ไม่มีใครเขียน (มันคือค่าเริ่มต้น)

static local — จำค่าข้ามการเรียก

#include <stdio.h>

int next_id(void)
{
    static int counter = 0;    // ตั้งค่าครั้งเดียว, อยู่ตลอดโปรแกรม
    return ++counter;
}

int main(void)
{
    printf("%d\n", next_id());   // 1
    printf("%d\n", next_id());   // 2
    printf("%d\n", next_id());   // 3
    return 0;
}
  • scope: ในฟังก์ชัน (มองเห็นเฉพาะที่นี่)
  • lifetime: ตลอดโปรแกรม — ไม่หายเมื่อฟังก์ชัน return
  • initialize ครั้งเดียว ตอนโปรแกรมเริ่ม (ไม่ใช่ทุกครั้งที่เข้าฟังก์ชัน)
  • ค่าเริ่มต้นเป็น 0 ถ้าไม่ระบุ (ต่างจาก automatic ที่เป็นขยะ)

ใช้ตอน: cache, counter, สถานะที่ต้องจำระหว่างการเรียก, buffer ที่ไม่อยาก malloc ทุกครั้ง

ระวัง: static local ที่มีการ เขียน (เช่น counter ข้างบน) ทำให้ฟังก์ชัน ไม่ reentrant / ไม่ thread-safe — สองผู้เรียกแชร์ state ที่เปลี่ยนได้ตัวเดียวกัน (static local ที่อ่านอย่างเดียว เช่น lookup table คงที่ ไม่มีปัญหานี้)

static ระดับไฟล์ — internal linkage

// counter.c
static int total = 0;         // ตัวแปร global แต่ "มองเห็นเฉพาะไฟล์นี้"

static void reset(void)       // ฟังก์ชันภายในไฟล์นี้
{
    total = 0;
}

void add(int n)               // ฟังก์ชัน public (ไม่มี static)
{
    total += n;
}
  • scope: ทั้งไฟล์ counter.c
  • lifetime: ตลอดโปรแกรม
  • linkage: internal — ไฟล์อื่นเรียก total หรือ reset ไม่ได้แม้ประกาศ extern

เหมือน private ของโมดูล (บทที่ 26) — ใส่ static กับทุกอย่างที่ไม่ต้อง export

extern — external linkage

// config.c
int g_debug_level = 1;        // นิยาม — จองพื้นที่ (external linkage โดยดีฟอลต์)

// config.h
extern int g_debug_level;     // ประกาศ — "มีตัวนี้อยู่ที่ไหนสักที่"

// main.c
#include "config.h"
// ใช้ g_debug_level ได้ — linker เชื่อมไปที่ config.c
  • ตัวแปร global (ไม่มี static) มี external linkage — ชื่อเดียวกันในทุกไฟล์ = ตัวเดียวกัน
  • นิยามได้ครั้งเดียว ทั้งโปรแกรม; ประกาศ (extern) ซ้ำได้
  • ฟังก์ชันก็มี external linkage โดยดีฟอลต์ (นั่นคือเหตุที่ prototype ใน .h ใช้ได้)

ตารางสรุป

ประกาศที่keywordscopelifetimelinkage
ในฟังก์ชัน(ไม่มี)blockตลอด blocknone
ในฟังก์ชันstaticblockตลอดโปรแกรมnone
ระดับไฟล์(ไม่มี)ทั้งไฟล์ตลอดโปรแกรมexternal
ระดับไฟล์staticทั้งไฟล์ตลอดโปรแกรมinternal
ระดับไฟล์externทั้งไฟล์ตลอดโปรแกรมexternal (อ้างที่อื่น)

block scope ซ้อน

int x = 1;
{
    int x = 2;             // ตัวใหม่ บังตัวนอก (shadowing)
    printf("%d\n", x);     // 2
    {
        int x = 3;
        printf("%d\n", x); // 3
    }
    printf("%d\n", x);     // 2
}
printf("%d\n", x);         // 1

-Wshadow เตือนเมื่อตัวแปรบังตัวนอก — มักเป็นบั๊ก (ตั้งใจแก้ตัวนอกแต่แก้ตัวใน)

register — เกือบไม่มีความหมายแล้ว

register int i;            // "ขอให้เก็บใน CPU register ถ้าทำได้"

คอมไพเลอร์สมัยใหม่ตัดสินใจเรื่องนี้เองดีกว่าเราเสมอ — ผลเดียวที่เหลือคือ &i ใช้ไม่ได้ (เอาที่อยู่ของ register variable ไม่ได้) โดยทั่วไป ไม่ต้องใช้

initialization ของ static / global

static int a = 5;             // ต้องเป็น constant expression
static int b = a + 1;         // ❌ error — a ไม่ใช่ compile-time constant
static int arr[3] = {1, 2, 3};
static int *p = NULL;         // OK
// static int *q = &local;    // ❌ ถ้า local ไม่ใช่ static

static/global initializer ต้องคำนวณได้ตอนคอมไพล์ (ต่างจาก local ที่ init ด้วยอะไรก็ได้)

เรื่องที่คนมักพลาด

  • คิดว่า static int x = 0; ในฟังก์ชัน reset เป็น 0 ทุกครั้ง — init ครั้งเดียว จำค่าข้ามการเรียก
  • static local ที่ *แก้ค่า* ในฟังก์ชันที่หลาย thread เรียก — race condition (ปัญหาอยู่ที่ shared mutable state ไม่ใช่คำว่า static เอง)
  • ลืม static กับฟังก์ชัน/ตัวแปรตัวช่วยในโมดูล — ชื่อชนกับไฟล์อื่น
  • extern int x = 5; ใน header — = 5 ทำให้เป็นนิยาม → multiple definition
  • shadowing โดยไม่ตั้งใจ — ประกาศ int i ใน block ซ้อนแล้วแก้ตัวใน คิดว่าแก้ตัวนอก ใช้ -Wshadow
  • คืน pointer ไป static local buffer — ใช้ได้ แต่การเรียกครั้งถัดไปเขียนทับ (เหมือน strtok, ctime — ต้องระวัง)
  • register แล้วพยายาม &i — error

สรุป

  • scope (เห็นตรงไหน) ≠ lifetime (มีชีวิตช่วงไหน) ≠ linkage (ตัวเดียวกันข้ามไฟล์ไหม)
  • local ปกติ = automatic — เกิดใหม่ทุกครั้ง, ค่าขยะ, no linkage
  • static ในฟังก์ชัน = จำค่าข้ามการเรียก, init ครั้งเดียว, ค่าเริ่ม 0 (ไม่ thread-safe)
  • static ระดับไฟล์ = private ต่อโมดูล (internal linkage)
  • global ปกติ + extern ใน header = แชร์ข้ามไฟล์ (external linkage) — นิยามครั้งเดียว
  • register เลิกใช้; static/global initializer ต้องเป็น constant expression

บทถัดไปว่าด้วยการจัดการข้อผิดพลาด — return code, errno, perror และรูปแบบ goto cleanup