c/cpp-faqs

linkages in function and object

1
2
3
4
5
6
7
8
9
10
11
/* object */	  /* function */

int o1; int f1(); /* external linkage */
static int o2; static int f2(); /* internal linkage */
static int o3; static int f3(); /* internal linkage */

static int o1; static int f1(); /* ERROR, both have external linkage */
int o2; /* ERROR, o2 has internal linkage */
int f2(); /* OK, picks up internal linkage */
extern int o3; extern int f3(); /* OK, both pick up internal linkage */

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
2
3
struct x1 { ... };
typedef struct { ... } x2;

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:

  1. 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).
  2. 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)

  1. “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.

  2. 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)

  1. 在c++中两种方式都表示声明一个函数, 它没有参数; 一样的
  2. 在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++中继承的相关问题

  1. 在public inheritance中 > hide an inherited public: member function is evil

delete 一个指针

  1. 当一个指针是NULL时, delete p是安全的
  2. delete p twice, it is a disaster.

随笔

  1. “M & M rule”: for a member variable, the mutable and mutex(or atomic) go together.
  2. to the compiler, the function signature is same whether you include const in front of a value parameter or not.

显示gcc/g++ 预定义变量的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
[16:13:41 0 ~] $ g++ -E -dM -std=c++0x -x c++ /dev/null >b
[16:13:44 0 ~] $ g++ -E -dM -std=c++98 -x c++ /dev/null >a
[16:13:50 0 ~] $ diff -u a b
--- a 2010-06-02 16:13:50.200787591 +0200
+++ b 2010-06-02 16:13:44.456912378 +0200
@@ -20,6 +20,7 @@
#define __linux 1
#define __DEC32_EPSILON__ 1E-6DF
#define __unix 1
+#define __GXX_EXPERIMENTAL_CXX0X__ 1
#define __LDBL_MAX_EXP__ 16384
#define __linux__ 1
#define __SCHAR_MAX__ 127

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
  1. vector: the iterator following the itertor which erased is invalid
  2. string: the iterator following the itertor which erased is invalid
  3. array: no erase function
  4. deque: all iterator is invalid, unless the erased iterator is the begining or the end, in which case, only the erased iterator is invalid.
  5. list: not affect other iterator
-------------本文结束感谢您的阅读-------------