new A 和 new A()的区分
转自:http://www.cppblog.com/peakflys/archive/2013/04/08/199208.html
在A类中存在显示构造函数的情况下,两者操作相同,先调用operator new分配内存,然后调用显示构造函数。
在A类不存在显示构造函数的情况下,首先还是operator new分配内存,然后new A()会对其基本类型(如int)的成员变量初始化赋空值。而new A的成员变量则是无意义的随机值,如果A类含有POD类型,则POD类型的成员变量处于未定义状态,如果含有非POD类型则调用该类型的默认构造函数。
摘自:http://blog.csdn.net/cay22/article/details/3393869
POD类型:plain old data,将对象的各字节拷贝到一个字节数组中,然后再将它重新拷贝到原先的对象所占的存储区中,此时该对象应该具有它原来的值。本质就是与c兼容的数据类型。
POD类型的特点:所有POD类型都可以作为union的成员,反之,所有非POD类型都不能作为union的成员。
摘自:C++Primer第四版5.11节:
摘自:ISO/IEC 14882:2003(E) 5.3.4 - 15
— If the new-initializer is omitted:
— If T is a (possibly cv-qualified) non-POD class type (or array thereof), the object is default-initialized(8.5). If T is a const-qualified type, the underlying class type shall have a user-declared default constructor.
— Otherwise, the object created has indeterminate value. If T is a const-qualified type, or a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of const-qualified type, the program is ill-formed;
— If the new-initializer is of the form (), the item is value-initialized (8.5);
C/C++程序内存占用
转自:http://www.cppblog.com/dawnbreak/archive/2009/03/10/76135.aspx
1、栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。
2、堆区(heap) — 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表。
3、全局区(静态区)(static)—,全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 - 程序结束后有系统释放
4、文字常量区 —常量字符串就是放在这里的。 程序结束后由系统释放
5、程序代码区—存放函数体的二进制代码。