C/C++

在所有高级编程语言中,只有C和Non-C,C是贴地飞行的艺术!几乎所有其它东西,都构建在C的基础上。C is described as high-level and portable assembly language!

tang_yuan.png

++ii++i+=N的汇编一样,是否比i=i+1i=i+N快,就看编译器了,反正gcc编译后用objdump反汇编,指令都一样(x86_64)!从语义上看,单独使用时++i更优(如果i是C++的迭代器,则++i速度更快)。而for循环,写成这样也不用加括号:++*ip(对*ip+1后取值,*ip++是先取值后再对ip指针+1)。

EOF = -1(d), 4294967295(u), 0xFFFFFFFF,EOF是int,不是char!(char在arithmetic operation中会自动执行Integral Promotion

虽然floatdouble也可以执行++--操作,但最好不要作...!(深入浮点数

使用assignment operator op=,也许可以帮助编译器生成更高效的代码,而且code is more compact!op可以是:+,-,*,/,%,<<,>>,&,^,|expr1 op= expr2等价于expr1 = (expr1) op (expr2)

Type conversion is always tricky and buggy! For ternary operator c?t:f, if f is a float and n is an int, then n>0?f:n is of type float regardless of whether n is positive.

=赋值,或接口call时的传参,都存在Implicit Type Conversion的可能!

有两种Right Shift操作,Logical和Arithmetic。前者补0,后者补MSB。基本上所有编译器都选择,signed右移采用后者,unsigned右移采用前者。Left Shift操作只有一种,补0。

malloc后new后检查free后delete后赋NULL或nullptr(防止double free,减少dangling pointer),accordingly!(free(NULL)和delete nullptr很OK,无需额外检查)

C语言选择了这种用\0字符作为字符串的结束符(NULL-terminated string)的设计;strlen的结果不包含最后的\0!(发现strlen的问题

There are 6 [white] space characters in ASCII set.

不建议使用multi-char literal语法,有warning,而且有implementation-defined details.

Wild Pointer: is a pointer that has not been correctly initialized and therefore points to some random piece of memory.

Dangling Pointer: is a pointer that used to point to a valid address but now no longer does. This is usually due to that memory location being freed up and no longer available.

得到.o文件有多种方式:(1)编译.c文件;(2)编译.s文件;(3)objcopy创建。

为什么变量名不可以用数字开头?因为存在这些合法的user-defined literals:1234U1234L....可用的suffix有:ULLLULLUULLLLUF表示float;浮点数literal没有suffix时,表示double类型;浮点数literal跟L时,表示long double类型。C++对这部分有更多扩展支持...

数字前缀:没有前缀时表示十进制;0x表示十六进制;0表示八进制;0b表示二进制。

Function Type Examples:

// In declaration, variable would be ignored.
double get(const vector<double>&, int);  // free function
// type: double(const vector<double>&,int)
char& String::operator[](int index);     // class method
// type: char& String::(int)

Pointer Cast

// C++, not easy to make mistake
p2 = reinterpret_cast<int*>(p1+offset);
// C, very easy to be doomed!
p2 = (int*)(p1+offset);  // (int*)p1+offset is WRONG!!

-- 目录[2] --

-- 文章[37] --