第五章笔记

new和delete表达式

① 初始化

如果不显式的初始化,动态创建的对象与在函数内订阅和ideas变量的初始化方式相同(数据类型和未提供默认构造函数的类不初始化,有默认构造函数的类自动调用默认构造函数初始化)

对于内置类型或没有定义默认构造函数的类型可采用如下方式                                                                                                    

   int *pi = new int(); //指向一个值为0的int

② 耗尽内存

如果new无法获取需要的内存空间,系统将抛出名为bad_alloc的异常

③ 撤销

int *p = new int();

delete p;

如果指针指向不是用new分配的内存地址,则在该指针上使用delete是不合法的

如果指针的值为0,则删除是合法的 :

int *p = 0;

delete p';

当执行撤销后,指针所指向内存被释放,但是指针仍然指向该内存,此时指针成为dangling pointer。。建议删除指向对象后,立即将指针置为0,避免再次使用出现程序错误。。

④ 警告

释放内存失败容易造成memory leak(内存泄露)

读写已删除的对象同样很危险。。如上文提到,建议置0

对同一块内存两次使用delete,此时自由储存区可能会被破坏。。

 

类型转换:

(隐式类型转换)-编译器自动执行

      负数转换为unsigned 型时,转换方式如下:

   -1转换为8位unsigned char 型时

       -1 :  10000001 

      取反:  11111110

      补码:  11111111   =  511(十进制)

      对256求模:511%256=255

例如

#include 
using namespace std;

int main()
{
	unsigned char i = -1;//-1 can be replaced by 511
	cout << (int)i;
	return 0;
}
 
 不仅仅算术值可以转换为bool型,指针值也可以转换为bool型。
算术值或指针值为0,则bool值为false,否则为true
 const_cast < type-id > ( expression )

A pointer to any object type or a pointer to a data member can be explicitly converted to a type that is identical except for the const, volatile, and __unaligned qualifiers. For pointers and references, the result will refer to the original object. For pointers to data members, the result will refer to the same member as the original (uncast) pointer to data member. Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior.

You cannot use the const_cast operator to directly override a constant variable's constant status.

The const_cast operator converts a null pointer value to the null pointer value of the destination type.

MSDN上的解释

 

一般采用指针操作,不能使用const_cast直接去除一个变量的const属性

例1

#include 
using namespace std;

int main()
{
	int i = 5;
	const int cj = i;
	int &j = const_cast<int&>(cj);
	j = 6;
	cout << cj;
	return 0;
}

例2

#include 
using namespace std;

int main()
{
	int i = 5;
	const int cj = i;
	int *j = const_cast<int*>(&cj);
	*j = 6;
	cout << cj;
	return 0;
}
问题出来了,无论在例1还是例2中,当你输出一下i的值的时候,你会发现,i的值仍然是5,而没有发生改变,当你输出&i和j的时候,你会发现,二者的地址完全相同,
但是为什么输出的值就不一样了呢,书上没有给出说明,Google一下,发现很多人通过查看汇编代码看到了猫腻,但是我不会汇编,大致理解了他们的意思,其实在编译
代码的时候,C++习惯性的将const类型当做宏定义来实现,在代码中用5来代替i,所以,即使内存中i的值变了,但是程序只会去代码中寻找所谓的宏定义来代替i,所
以i输出的值并未发生改变,所以,在此一定要慎用const_cast。。。。
 reinterpret_cast < type-id > ( expression ) 
通常为操作数的位模式提供较低层次的重新解释,操作符修改了操作数类型,但仅仅是重新解释了给出的对象的比特模型而没有进行二进制转换

The reinterpret_cast operator also allows any integral type to be converted into any pointer type and vice versa. Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired conversion is inherently low-level, you should use one of the other cast operators.

The reinterpret_cast operator can be used for conversions such as char* to int*, or One_class* to Unrelated_class*, which are inherently unsafe.

The result of a reinterpret_cast cannot safely be used for anything other than being cast back to its original type. Other uses are, at best, nonportable.

The reinterpret_cast operator cannot cast away the const, volatile, or __unaligned attributes. See const_cast Operator for information on removing these attributes.

The reinterpret_cast operator converts a null pointer value to the null pointer value of the destination type.

这是MSDN中的解释,我看明白了,字面看明白了,理解不深刻,这俩cast都挺愁人的。。。reinterpret_cast要慎用,如果用,尽量在指针类型中用,不过一定要谨慎,现在理解不深刻,用到的时候再补充理解,MSDN说hash函数中有例子。。有空研究一下。。。

 static_cast ( expression )

编译器隐式执行的任何类型的转换都可以由static_cast显示的完成,例如对于一个较大的算术类型到一个较小的类型的复制,一般编译器都会产生个警告。当我们现实的提供强制类型转换时,警告信息就会关闭。。(此时要注意精度损失)

No run-time type check is made to help ensure the safety of the conversion.

The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class. Such conversions are not always safe.

In general you use static_cast when you want to convert numeric data types such as enums to ints or ints to floats, and you are certain of the data types involved in the conversion. static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does. A dynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if nothing were wrong; this can be dangerous. Although dynamic_cast conversions are safer, dynamic_cast only works on pointers or references, and the run-time type check is an overhead. See dynamic_cast for more details.

MSDN上的解释

 dynamic_cast < type-id > ( expression )

18.2节讨论,届时再补充笔记

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值