Why doesn’t delete zero out its operand?

Consider

	delete p;
	// ...
	delete p;

If the … part doesn’t touch p then the second “delete p;” is a serious error that a C++ implementation cannot effectively protect itself against (without unusual precautions). Since deleting a zero pointer is harmless by definition, a simple solution would be for “delete p;” to do a “p=0;” after it has done whatever else is required. However, C++ doesn’t guarantee that.One reason is that the operand of delete need not be an lvalue. Consider:

	delete p+1;
	delete f(x);

Here, the implementation of delete does not have a pointer to which it can assign zero. These examples may be rare, but they do imply that it is not possible to guarantee that “any pointer to a deleted object is 0.” A simpler way of bypassing that “rule” is to have two pointers to an object:

	T* p = new T;
	T* q = p;
	delete p;
	delete q;	// ouch!

C++ explicitly allows an implementation of delete to zero out an lvalue operand, and I had hoped that implementations would do that, but that idea doesn’t seem to have become popular with implementers.If you consider zeroing out pointers important, consider using a destroy function:

	template<class T> inline void destroy(T*& p) { delete p; p = 0; }

Consider this yet-another reason to minimize explicit use of new and delete by relying on standard library containers, handles, etc.

Note that passing the pointer as a reference (to allow the pointer to be zero’d out) has the added benefit of preventing destroy() from being called for an rvalue:

	int* f();
	int* p;
	// ...
	destroy(f());	// error: trying to pass an rvalue by non-const reference
	destroy(p+1);	// error: trying to pass an rvalue by non-const reference

睿登电源外壳DIY

材料准备

  • 睿登电源面板
  • 明纬电源
  • 品字电源插座(带保险管)
  • 提手
  • 压线端子
  • 连接线
  • 3D打印机

获取尺寸 -> 设计外壳数模 -> 3D打印 -> 装机验证 -> 修正 -> 成品。

点的坐标偏移量 与 坐标系的偏移量 相反

乍一听,好像不知所云。实际上是这样的:假设空间一个点A1相对于坐标系C1的坐标数值为(1,1),将坐标系C1进行平移,沿着原坐标轴方向移动一个单位距离,即偏移量为(-1,-1),那么空间点A1相对于新的坐标系的坐标数值为(2,2)。因为点和原点是相对的,所以点是朝着原点相反方向移动的。所以(2,2)的计算方法是 (1+-(-1),1+-(-1))。

Continue reading