7.30(월) C++ - new/delete 총정리

from Study/C++ 2007/07/31 13:07 view 25297

// 1. new의 동작방식  (A) operator new() 호출해서 메모리 할당.
//                 (B) A가 성공하면 생성자 호출.

 

class Test

{

public:

        Test()  { cout << "Test()" << endl; }

        ~Test() { cout << "~Test()" << endl; }

};

 

void main()

{

        Test* p = (Test*)operator new( sizeof(Test) );

        operator delete( p );

        //Test* p = new Test;

        //delete p;

}

 

// 2. operator new() 를재정의할수있다.

// 3. Array New

// 4. Member New

class Point

{

public:

        void* operator new( size_t s )

        {

               return malloc( s );

        }

};

 

Point* p = new Point;
 

void* operator new( size_t sz )

{

        cout << "operator new" << endl;

 

        return malloc( sz );

}

 

// Array New

void* operator new[]( size_t sz )

{

        cout << "operator new[]" << endl;

        return malloc( sz );

}

 

void main()

{

        int* p = new int;

        delete p;

 

        int* p2 = new int[10]; // Array new 호출 -> 없다면

        delete[] p2;

}

// 5. overloading new

// 다음 함수는 왜 만들었을까요? 메모리부터 잡고 객체를 생성하는 기법.

class Test

{

public:

        Test() { cout << "Test()" << endl; }

};

/* 전달된객체를반환해준다!!!

void* operator new( size_t s, void* p )

{

        return p;

}

*/

void main()

{

        Test* p = new Test; // operator new( size_t )로메모리할당후생성자호출

 

        // 객체를메모리에먼저잡고생성자를호출하는기법MapView( ?? )

        // 메모리는Cmalloc 의잡고나서객체를집어넣는다.

        new(p) Test;      // p를이미생성된메모리에반환!!

                          // operator new( size_t, void* ) 호출후생성자호출.

}


////////////////////////////////////////////////////////////////////////
// 6. nothorw
의 동작 방식!!!

// new : 실패시 예외전달( std::bad_alloc )

// new(nothrow) : 실패시 0


void
* operator new( size_t s )

{

        // 실패시예외발생

}

 

// 코드를설명적으로만들수있다.

class nothrow_t   // empty class - sizeof(nothrow_t) => 1

                  // overloading 함수를만들때사용할수있다. 설명적인코드가된다.

{

};

nothrow_t nothrow;

 

void* operator new( size_t s, nothrow_t )

{

        // 실패시0을리턴

}

 

// new의 새로운 방식

#define new new(nothrow)    // 예전코드의호환을위해서define 해준다.

void main()

{ 

        // VC2005 등의최신컴파일러는모두new 가실패시예외가나온다.

        int *p = 0;

        try

        {

               p = new int[1000];

               *p = 10;

               delete[] p;

        }

        catch( std::bad_alloc e )

        {

               cout << "메모리할당실패" << endl;

        }

///////////////////////////////////////////////////////////////

        int* p = new int[];

        if ( p == 0 )

        {

               cout << "메모리를할당할수없습니다." << endl;

        }

        else

        {

               *p = 10;       // 메모리사용

               delete[] p;

        }

}


/////////////////////////////////////////////////////////////////////
void
* operator new( size_t s )               // 1

{

        return malloc( s );

}

 

void* operator new( size_t s, char* p )      // 2

{

        cout << "new : " << p << endl;

        return malloc( s );

}

 

void main()

{

        int* p = new int;

 

        int* p2 = new ("AAA") int;

 

        delete p;

}

 

 

 

 

Tag |

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다