C++编程中new运算符的使用学习教程
new运算符用作从自由存储为 type-name 的对象或对象数组分配内存,并将已适当分类的非零指针返回到对象。 [::] new [placement] new-type-name [new-initializer] [::] new [placement] ( type-name ) [new-initializer] 备注 char (*pchar)[10] = new char[dim][10]; delete [] pchar; type-name 不能包含 const、volatile、类声明或枚举声明。 因此,以下表达式是非法的: volatile char *vch = new volatile char[20]; new 运算符不会分配引用类型,因为这些类型不是对象。 int (**p) () = new (int (*[7]) ()); delete *p; 如果使用不带任何额外参数的 new 运算符,并用 /GX、/EHa 或 /EHs 选项进行编译,则编译器将在构造函数引发异常时生成代码来调用运算符 delete。 // expre_new_Operator.cpp // compile with: /EHsc #include <string.h> class CName { public: enum { sizeOfBuffer = 256 }; char m_szFirst[sizeOfBuffer]; char m_szLast[sizeOfBuffer]; public: void SetName(char* pszFirst,char* pszLast) { strcpy_s(m_szFirst,sizeOfBuffer,pszFirst); strcpy_s(m_szLast,pszLast); } }; int main() { // Allocate memory for the array char* pCharArray = new char[CName::sizeOfBuffer]; strcpy_s(pCharArray,CName::sizeOfBuffer,"Array of characters"); // Deallocate memory for the array delete [] pCharArray; pCharArray = NULL; // Allocate memory for the object CName* pName = new CName; pName->SetName("Firstname","Lastname"); // Deallocate memory for the object delete pName; pName = NULL; } 如果使用 new 运算符的放置新形式(带有参数和分配大小的形式),如果构造函数引发异常,则编译器不支持 delete 运算符的放置形式。 例如: // expre_new_Operator2.cpp // C2660 expected class A { public: A(int) { throw "Fail!"; } }; void F(void) { try { // heap memory pointed to by pa1 will be deallocated // by calling ::operator delete(void*). A* pa1 = new A(10); } catch (...) { } try { // This will call ::operator new(size_t,char*,int). // When A::A(int) does a throw,we should call // ::operator delete(void*,int) to deallocate // the memory pointed to by pa2. Since // ::operator delete(void*,int) has not been implemented,// memory will be leaked when the deallocation cannot occur. A* pa2 = new(__FILE__,__LINE__) A(20); } catch (...) { } } int main() { A a; } 初始化使用 new 运算符分配的对象 // expre_Initializing_Objects_Allocated_with_new.cpp class Acct { public: // Define default constructor and a constructor that accepts // an initial balance. Acct() { balance = 0.0; } Acct( double init_balance ) { balance = init_balance; } private: double balance; }; int main() { Acct *CheckingAcct = new Acct; Acct *SavingsAcct = new Acct ( 34.98 ); double *HowMuch = new double ( 43.0 ); // ... } 在此示例中,使用 CheckingAcctnew 运算符分配了 对象,但未指定默认初始化。 因此,调用了类的默认构造函数 Acct()。 然后,以相同的方式分配了对象 SavingsAcct,只不过将它显式初始化为 34.98。 由于 34.98 是类型 double,因此调用了采用该类型的参数的构造函数来处理初始化。 最后,将非类类型 HowMuch 初始化为 43.0。 // expre_Lifetime_of_Objects_Allocated_with_new.cpp // C2541 expected int main() { // Use new operator to allocate an array of 20 characters. char *AnArray = new char[20]; for( int i = 0; i < 20; ++i ) { // On the first iteration of the loop,allocate // another array of 20 characters. if( i == 0 ) { char *AnotherArray = new char[20]; } } delete [] AnotherArray; // Error: pointer out of scope. delete [] AnArray; // OK: pointer still in scope. } 在上面的示例中,指针 AnotherArray 一旦超出范围,将无法再删除对象。 new 的工作方式 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |