非常教程

C参考手册

C 语法

Lifetime

C中的每个对象都有一个常量地址,保留其最后存储的值(除非值是不确定的),并且对于VLA来说,它的大小(自C99以来)保持在被称为该对象生命周期的一部分程序执行

对于使用自动,静态和线程存储持续时间声明的对象,寿命等于它们的存储持续时间(请注意非VLA和VLA自动存储持续时间之间的差异)。

对于具有已分配存储持续时间的对象,生命周期从分配函数返回(包括从中返回realloc)开始,并在realloc调用或释放函数被调用时结束。请注意,由于分配的对象没有声明类型,所以首先用于访问此对象的左值表达式的类型成为其有效类型。

在其生命周期外访问对象是未定义的行为。

int* foo(void) {
    int a = 17; // a has automatic storage duration
    return &a;
}  // lifetime of a ends
int main(void) {
    int* p = foo(); // p points to an object past lifetime ("dangling pointer")
    int n = *p; // undefined behavior
}

指向其生命周期结束的对象(或对象之后的对象)的指针具有不确定的值。

临时寿命

包含由非左值表达式指定的数组成员(直接或嵌套结构/联合成员的成员)的结构和联合对象具有临时生存期。当包含完整表达式或完整声明程序结束时(自C11开始),对引用此类对象的表达式进行评估并结束于下一个序列点(直到C11)时临时生存期开始。

任何尝试修改具有临时生命周期的对象都会导致未定义的行为。

struct T { double a[4]; };
struct T f(void) { return (struct T){3.15}; }
double g1(double* x) { return *x; }
void g2(double* x) { *x = 1.0; }
int main(void)
{
    double d = g1(f().a); // C99: UB access to a[0] in g1 whose lifetime ended
                          //      at the sequence point at the start of g1
                          // C11: OK, d is 3.15
    g2(f().a); // C99: UB modification of a[0] whose lifetime ended at the sequence point
               // C11: UB attempt to modify a temporary object
}

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 6.2.4 Storage durations of objects (p: 38-39)
  • C99 standard (ISO/IEC 9899:1999):
    • 6.2.4 Storage durations of objects (p: 32)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 3.1.2.4 Storage durations of objects

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.Logical operators
59.Lookup and name spaces
60.Main function
61.Member access operators
62.Memory model
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 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。