c/cpp-faqs
linkages in function and object
1 | /* object */ /* function */ |
The difference is case (2); where functions do pick up a previous linkage even without "extern", objects don't.
extern mean in a function declaration
extern is significant only with data declarations. In function declarations, it can be used as a stylistic hint to indicate that the function's definition is probably in another source file, but there is no formal difference between
extern int f();
and int f()
;
What's the difference between these two declarations?
1 | struct x1 { ... }; |
The first form declares a structure tag; the second declares a typedef. The main difference is that the second declaration is of a slightly more abstract type--its users don't necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it:
1 | x2 b; |
Structures declared with tags, on the other hand, must be defined with the
1 | struct x1 a; |
What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways:
- As the initializer for an array of char, as in the declaration of char a[] , it specifies the initial values of the characters in that array (and, if necessary, its size).
- Anywhere else, it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. In an expression context, the array is converted at once to a pointer, as usual (see section 6), so the second declaration initializes p to point to the unnamed array's first element.
Some compilers have a switch controlling whether string literals are writable or not (for compiling old code), and some may have options to cause string literals to be formally treated as arrays of const char (for better error catching).
array 是数组, 则array 和 &array 的区别
区别在于类型 在标准c中, &array生成一个"T型数组"的指针,指向整个数组. 对于数组的简单引用则生成一个T型的指针,指向数组的第一个元素.
int a[10]
对a的引用类型是"int型的指针", 而&a是"10个int的数组的指针".
int array[row][col]
对array的引用类型是"col个int的数组的指针", 而&array的类型是"row个col个int的数组的数组的指针".
无符号规则(unsigned preserving)和值规则(value preserving)
“unsigned preserving,” is used: when an unsigned type needs to be widened, it is widened to an unsigned type; when an unsigned type mixes with a signed type, the result is an unsigned type.
The other rule, specified by ISO C, is known as “value preserving,” in which the result type depends on the relative sizes of the operand types. When an unsigned char or unsigned short is widened, the result type is int if an int is large enough to represent all the values of the smaller type. Otherwise, the result type is unsigned int. The value preserving rule produces the least surprise arithmetic result for most expressions.
Compilation Behavior: Only in the transition or ISO modes (-Xt or -Xs) does the ISO C compiler use the unsigned preserving promotions; in the other two modes, conforming (–Xc) and ISO (–Xa), the value preserving promotion rules are used.
int main() vs int main(void)
- 在c++中两种方式都表示声明一个函数, 它没有参数; 一样的
- 在c中, int main(void) 在c11标准中定义, 表示函数没有参数
members can initialize in class declaration
in c++11 non-static data members, static constexpr data members, and static const data members of integral or enumeration type may be initialized in the class declaration. e.g.
1
2
3
4
5
6 struct X {
int i=5;
const float f=3.12f;
static const int j=42;
static constexpr float g=9.5f;
};
c++中继承的相关问题
- 在public inheritance中 > hide an inherited public: member function is evil
delete 一个指针
- 当一个指针是NULL时, delete p是安全的
- delete p twice, it is a disaster.
随笔
- “M & M rule”: for a member variable, the mutable and mutex(or atomic) go together.
- to the compiler, the function signature is same whether you include const in front of a value parameter or not.
显示gcc/g++ 预定义变量的区别
1 | [16:13:41 0 ~] $ g++ -E -dM -std=c++0x -x c++ /dev/null >b |
c++ ctor and dtor
base-class subobjects, const or reference nonstatic data members. A base-class subobject, or a nonstatic data member, not mentioned in the member-initializer list is implicitly initialized with a default constructor, i.e. a constructor that can be called with no argument. Consequently, the language rules imply that all subobjects are constructed by the time the first statement of the constructor body (if not empty) is executed.
iterator validation after erase
- sequence container
- vector: the iterator following the itertor which erased is invalid
- string: the iterator following the itertor which erased is invalid
- array: no erase function
- deque: all iterator is invalid, unless the erased iterator is the begining or the end, in which case, only the erased iterator is invalid.
- list: not affect other iterator