进程的虚拟地址空间:
一个经典例子:
int a = 0; //全局初始化区 char *p; //全局未初始化区 void main() { int b; //栈区 char s[] = "abcd"; //栈区 char *p2; //栈区 char *p3 = "123456"; //123456在常量区,p3在栈区 static int c = 0; //全局(静态)初始化区 p1 = (char *)malloc(10); //分配所得的10字节区域位于堆区 p2 = (char *)malloc(20); //分配所得的20字节区域位于堆区 strcpy(p1, "123456"); }
const
属性void aFunction(int* a) { cout << *a << endl; } int main() { int a = 10; const int* iVariable = &a; aFunction(const_cast<int*>(iVariable)); /*Since the function designer did not specify the parameter as const int*, we can strip the const-ness of the pointer iVariable to pass it into the function. Make sure that the function will not modify the value. */ return 0; }
参考资料:
1 C++ casting
2 When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
3 C++:18---强制类型转换(static_cast、const_cast、dynamic_cast、reinterpret_cast)