C/C++ 笔记

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节:

   We indicate that we want to value-initialize the newly allocated object by following the type nameby a pair of empty parentheses. The empty parentheses signal that we want initialization but arenot supplying a specific initial value. In the case of class types (such as string) that define their own constructors, requesting value-initialization is of no consequence: The object is initialized by running the default constructor whether we leave it apparently uninitialized orask for value-initialization. In the case of built-in types or types that do not define any constructors, the difference is significant:
     int *pi = new int;         // pi points to an uninitialized int 
     int *pi = new int();       // pi points to an int value-initialized to 0 
In the first case, the int is uninitialized; in the second case, the int is initialized to zero.

摘自: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、程序代码区—存放函数体的二进制代码。 

volatile的作用: 

作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值。简单地说就是防止编译器对代码进行优化.比如如下程序:
XBYTE[2]=0x55;    XBYTE[2]=0x56;    XBYTE[2]=0x57;   XBYTE[2]=0x58;
对外部硬件而言,上述四条语句分别表示不同的操作,会产生四种不同的动作,但是编译器就不能像对待纯粹的程序那样对上述四条语句进行优化,只认为XBYTE[2]=0x58(即忽略前三条语句,只产生一条机器代码)。如果键入volatile,则编译器会逐一的进行编译并产生相应的机器代码(四条)。
IEEE浮点位:
float: 1位符号位(s)、8位指数(e),23位尾数(m,共32位)
double: 1位符号位(s)、11位指数(e),52位尾数(m,共64位)

void参数:

在C语言里void (*pf)()和void (*pf)(void)是不等价的,前者没有指定参数,所以参数可以任意,只要在定义处实现即可,后者指定参数为空;C++中两者等价。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值