基础数据的体系结构无关抽象

Freestanding 环境下也可以使用的定义

  • stddef.h - size_t
  • stdint.h - int32_t, uint64_t
  • stdbool.h - bool, true, false
  • float.h
  • limits.h
  • stdarg.h
    • syscall 就用到了 (但 syscall0, syscall1, ... 更高效)
  • inttypes.h
    • 回答了你多年来的疑问:32/64-bit printf 格式字符串
    • 在你过了小白阶段以后,就真的是 friendly manual 了

字符串和数组操作

string.h: 字符串/数组操作

简单,不简单

void *memset(void *s, int c, size_t n) {
  for (size_t i = 0; i < n; i++) {
    ((char *)s)[i] = c;
  }
  return s;
}

让我们看看 clang -O3 把它编译成了什么……

  • 以及,线程安全性?(例子)
  • 标准库只对 “标准库内部数据” 的线程安全性负责
    • 例子:printf 的 buffer

排序和查找

低情商 (低配置) API

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

void *bsearch(const void *key, const void *base,
              size_t nmemb, size_t size,
              int (*compar)(const void *, const void *));

高情商 API

sort(xs.begin(), xs.end(), [] (auto& a, auto& b) {...});
xs.sort(lambda key=...)

更多的例子

RTFM!

  • 更多的 stdlib.h 中的例子
    • atoi, atol, atoll, strtoull, ...
    • rand (注意线程安全), ...
  • math.h
    • 这玩意复杂了; 《操作系统》课直接摆烂
  • setjmp.h
    • 曾经是 lab 的一部分