Closures, Objects, and the Fauna of the Heap
value types and reference types. A value of value type is the actual value. A value of reference type is a reference to another value.
value types: stack
reference types: heap
Lexical scope vs. dynamic scope
貌似正则是type 3, c是type2, 范式角度我现在不好理解, 缺乏语言学的知识.
Programming Language Pragmatics 3rd
C Traps and Pitfalls
Expert C Programming
Programming language theory
Programming language theory
What is formal language and the relationg between fl and pl?
- Object type and function type
- Object – void, scalar,aggregate types, composite
- Scalar type – Arithmetic types and pointer types
What does Scalar mean?
scalar 词源上由scale演化而来, scalar type来自scalar processor and vector processor.
超标量也是来自这里concurrent SISD =>MIMD 所谓的流水线.
Arithmetic type – Integer and floating types
Incomplete and complete
void; struct s; union u;
only effect to scalar type data
error: conversion to non-scalar type requested
无符号数 转换成upper rank的有符号数, 还是无符号数, 不管最初是-1!
unsigned int i = 1; long f = -10;if ( i > f ) printf(“ok\n”); true 证明确实usigned 向long转换了.
unsigned int i = -3; long f = -5u;if ( i > f ) printf(“ok\n”); true
做signed 和unsigned 貌似both convert to unsigned gcc才给警告, 否则不给即便是signed和unsigned比较.
lvalue rvalue modfiable rvalue
Understanding lvalues and rvalues in C and C++
If a variable/expression has a address, it’s of lvalue.
数组退化的初衷, 可能是K&R当年计算资源紧缺, 导致不允许函数传值copy数组内容.
Exception to array not decaying into a pointer?
在c11 188.8.131.52 Lvalues, arrays, and function designators第3点说明了
array不会退化的4种场景. Except when it is
the operand of the sizeof operator,
the _Alignof operator, or the
unary & operator, or
is a string literal used to initialize an array, an expression that has type
‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’
that points to the initial element of the array object and is not an lvalue.
If the array object has register storage class, the behavior is undefined.
首先array decay是type上的转化array -> pointer.
其次数组名原来是lvalue -> not an lvalue更谈不上modifiable.
type: tyepof(array) *
value: &array or array
property: not lvalue
实现的个人猜测是: 根据lvalue的定义decay后数组名还是lvalue, 只不过一直都不是modifiable.
因为形参数组名被当初pointer处理modifiable lvalue, 而实参数组名只是传值而已.
Why innermost dimension can be omit in array
In essence, all arrays in C are one-dimensional.
Because the array will decay to pointer and to calculate offset to
the elements of the array you do not need to know the innermost dimension.
Compiler has to know by how much to increment the pointer when
indexing on the first dimension for example. So if an int array is named a,
Scopes of identifiers
For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope.
* four kinds of scopes: function, file, block, and function prototype
linkages of identifiers
An identifier declared in different scopes or in the same scope more than once can be
made to refer to the same object or function by a process called linkage.
* There are three kinds of linkage: external, internal, and none.
* There is no linkage between different identifiers.
* external linkage
In the set of translation units and libraries that constitutes an entire program, each
declaration of a particular identifier with external linkage denotes the same object or
If the name has external linkage,
the entity that name denotes may be referred to from another translation unit
using a distinct declaration for that same name,
and from other scopes within the same translation unit using distinct declarations. –wikipedia
* Internal linkage
Within one translation unit, each declaration of an identifier with internal
linkage denotes the same object or function. –c11
Were the name given internal linkage,
such a declaration would denote a distinct entity, although using the same name,
but its entity could be referred to by distinct declarations within the same translation unit. –wikipedia
* No linkage
Each declaration of an identifier with no linkage denotes a unique entity. –c11
A name that has no linkage at all cannot be referred to from declarations in different scopes,
not even from within the same translation unit. –wikipedia
Name spaces of identifiers
Storage durations of objects
An object has a storage duration that determines its lifetime.
There are four storage durations: static, thread, automatic, and allocated.
The lifetime of an object is the portion of program execution during which storage is
guaranteed to be reserved for it.
In book 21st Century C, it was named memory mode.
* static storage duration
An object whose identifier is declared without the storage-class specifier _Thread_local,
and either with external or internal linkage or with the storage-class specifier static(for no linkage –firo),
has static storage duration.
Its lifetime is the entire execution of the program and its stored value is initialized only once,
prior to program startup.
* automatic storage duration
An object whose identifier is declared with no linkage and without the storage-class
specifier static has automatic storage duration, as do some compound literals.
* alloced storage duration
The lifetime of an allocated object extends from the allocation until the deallocation.
* Thread storage duration
details in c11
Alignment of objects
An alignment is an implementation-defined integer value representing the number of bytes between
successive addresses at which a given object can be allocated.
More deatils in cs.md
A declaration specifies the interpretation and attributes of a set of identifiers.
A definition of an identifier is a declaration for that identifier that:
for an object, causes storage to be reserved for that object;
for a function, includes the function body;
for an enumeration constant, is the (only) declaration of the identifier;
for a typedef name, is the first (or only) declaration of the identifier.
The declaration specifiers consist of a sequence of specifiers that indicate the linkage,
storage duration, and part of the type of the entities that the declarators denote.
A cast does not yield an lvalue.
An integer character constant has type int.
Greedy lexical analysis
Write tokens with blank!
x = y/p; / oops, hidden error.*/
You donot need to remember it! Just use it!
Need to remember, but if you not sure, parenthess.
没有调用的开销效率很高, 但是调试代码复杂了, 内链函数的实现是拷贝副本消耗内存.
C standard library
Logical operation with signed value? x86!
int c = 0xFFFFFFFF; int d = c >> 31; => d == f;
unsigned c = 0xFFFFFFFF; int d = c >> 31; d!= f;
shift beyond bits long?
c >> 296 == c >> 8
变量同名不同类型不同文件, 通过extern, 否则就是multiple definition
C standard library
* Copying functions
strcpy: dest, 拷贝\0
strncpy: dest, if src_len >= n; 0 NULL, if src _len < n; (n - len) NULL;
strlcpy: src_len, mini(n -1, src_len -1) + \0, src_len 用你返回啊!
* Concatenation functions
strcat: dest, overwrite dest \0 with src util src \0
strncat: dest, 末尾一定有\0
* Comparison functions
strcmp: -1, 0, 1, 如果整个s1都比完了NULL or –n ==0 for strncmp, return 0
* Search functions
memchr, NULL or p;
strrchr,ditto, last occurence.
strstr: 找子串, 用memcmp
A wildcard character is a type of meta character
Standard Wildcards (globbing patterns)
File and directory patterns
?: must stand for a character
* Regular expression