非常教程

C参考手册

C 语法

Memory model

为C抽象机器的目的定义计算机内存存储的语义。

C程序可用的数据存储(内存)是一个或多个连续的字节序列。内存中的每个字节都有一个唯一的地址

字节

字节是最小的可寻址内存单元。它被定义为一个连续的位序列,足够大以容纳基本执行字符集的任何成员(96个字符必须是单字节)。C支持8位或更大的字节。

存储和值表示的类型charunsigned charsigned char使用一个字节。一个字节中的位数可以被访问为CHAR_BIT

内存位置

存储器位置是。

  • 标量类型的对象(算术类型,指针类型,枚举类型)
  • 或者是非零长度的最大的连续比特字段序列
struct S {
    char a;     // memory location #1
    int b : 5;  // memory location #2
    int c : 11, // memory location #2 (continued)
          : 0,
        d : 8;  // memory location #3
    struct {
        int ee : 8; // memory location #4
    } e;
} obj; // The object 'obj' consists of 4 separate memory locations

线程和数据竞争执行线程是程序中的一个控制流程,以thrd_create或其他方式调用顶级函数开始。任何线程都可能访问程序中的任何对象(具有自动和线程本地存储持续时间的对象仍然可以由另一个线程通过指针访问)。不同的执行线程总是被允许同时访问(读取和修改)不同的内存位置,不会产生干扰,也不需要同步。(注意,如果在它们之间声明的所有成员也是(非零长度)位字段,则同时更新同一结构中的两个非原子位字段是不安全的,不管这些介入位字段的大小如何,田野碰巧是)。当表达式的评估写入内存位置并且另一个评估读取或修改相同的内存位置时,表达式被认为是冲突的。有两个相互冲突的评估的程序除了两者之外还有数据竞争。两个冲突的评估都是原子操作冲突评估之一发生在另一个之前(请参阅memory_order)如果发生数据竞争,则程序的行为未定义。(特别是,mtx_unlock是与synchronized同步的,因此会在另一个线程的同一个互斥体的mtx_lock之前发生,这使得使用互斥锁来防范数据争夺成为可能)。内存顺序当一个线程从一个线程读取一个值内存位置,它可能会看到初始值,写入同一线程的值或写入另一个线程的值。

(自C11以来)

  • 两个相互冲突的评估都是原子操作
  • 其中一个冲突评估发生在另一个之前(请参阅memory_order

如果发生数据竞争,程序的行为是未定义的。(特别mtx_unlock是。

另一个线程在同一个互斥体之前 mtx_lock进行同步并因此发生,这使得可以使用互斥锁来防止数据竞争)内存顺序

当线程从内存位置读取值时,它可能会看到初始值,写入同一线程的值或写入另一个线程的值。请参阅有关memory_order线程写入的顺序对其他线程可见的详细信息。

(since C11)

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 3.6 byte (p: 4)
    • 3.14 memory location (p: 5)
    • 5.1.2.4 Multi-threaded executions and data races (p: 17-21)
  • C99 standard (ISO/IEC 9899:1999):
    • 3.6 byte (p: 4)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 1.6 DEFINITIONS OF TERMS

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
54.Increment/decrement operators
55.Initialization
56.inline function specifier
57.integer constant
58.Lifetime
59.Logical operators
60.Lookup and name spaces
61.Main function
62.Member access operators
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。