operator new, operator delete; inline and const specifier

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huan
    New Member
    • Apr 2007
    • 12

    operator new, operator delete; inline and const specifier

    int *p=new int(7)
    delete p;

    could be written as

    void* buf=operator new(sizeof(int) );
    int* p=new (buf) int(7);
    operator delete(p);

    My question is how to use operator new, placement new to allocate and initialize the storage and use operator delete to deallocate the storage?

    BTW, why the inline specifier for a member function can appear in the declaration or the definition or both;whereas the const specifier fo a member function must appear in both of them?
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by huan
    int *p=new int(7)
    delete p;

    could be written as

    void* buf=operator new(sizeof(int) );
    int* p=new (buf) int(7);
    operator delete(p);

    My question is how to use operator new, placement new to allocate and initialize the storage and use operator delete to deallocate the storage?

    BTW, why the inline specifier for a member function can appear in the declaration or the definition or both;whereas the const specifier fo a member function must appear in both of them?
    It could be written that way, but don't. ;) Your use of operator new is not necessarily bad, but the use of operator delete is real bad (though not in this case).

    The reason it is bad is because when you call operator delete, the destructor is not called. For a type like int that has no destructor, that is ok, but it will cause a resource leak if you try it that does have a destructor to cleanup the resources.

    As for how to use the new and delete operators to do in place initialisation and destruction, well... it depends as to why I guess.

    Tell me why you want to do this, and I would be able to give you an explanation.

    As to the inline question, that is probably due to the fact that it is not part of the function signature, where as const is. You omit the const keyword in one of the places and it will think it is for a completely different function. Inline is merely a hint to the compiler to inline the function.


    Adrian

    Comment

    • huan
      New Member
      • Apr 2007
      • 12

      #3
      Originally posted by AdrianH
      It could be written that way, but don't. ;) Your use of operator new is not necessarily bad, but the use of operator delete is real bad (though not in this case).

      The reason it is bad is because when you call operator delete, the destructor is not called. For a type like int that has no destructor, that is ok, but it will cause a resource leak if you try it that does have a destructor to cleanup the resources.

      As for how to use the new and delete operators to do in place initialisation and destruction, well... it depends as to why I guess.

      Tell me why you want to do this, and I would be able to give you an explanation.

      As to the inline question, that is probably due to the fact that it is not part of the function signature, where as const is. You omit the const keyword in one of the places and it will think it is for a completely different function. Inline is merely a hint to the compiler to inline the function.


      Adrian
      I'm very appreciative for your patient reply.
      Actually, I've lost one paragragh, which is the most important part.....
      I know that this is not necessary, but I'd like to know how to use operater new
      ,placement new and operator delete when

      int**p=new int*(new(int(7) )) ;

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by huan
        I'm very appreciative for your patient reply.
        Actually, I've lost one paragragh, which is the most important part.....
        I know that this is not necessary, but I'd like to know how to use operater new
        ,placement new and operator delete when

        int**p=new int*(new(int(7) )) ;
        But placement new is not necessary, you just wrote the valid code yourself right there.

        Placement new is when you have memory already allocated or allocated in a special way and you wish to use it. But it takes some care to do it properly, esp if you have a destructor.

        A simple example.
        Code:
        char allocatedMem[sizeof(int)];
        int * intValue = new(allocatedMem) int(7);
        ...
        intValue->~int();  // Not specificly needed in this case, but illistrates the point.
        Do you understand?


        Adrian

        Comment

        • huan
          New Member
          • Apr 2007
          • 12

          #5
          Originally posted by AdrianH
          But placement new is not necessary, you just wrote the valid code yourself right there.

          Placement new is when you have memory already allocated or allocated in a special way and you wish to use it. But it takes some care to do it properly, esp if you have a destructor.

          A simple example.
          Code:
          char allocatedMem[sizeof(int)];
          int * intValue = new(allocatedMem) int(7);
          ...
          intValue->~int();  // Not specificly needed in this case, but illistrates the point.
          Do you understand?


          Adrian
          OK~!Got it~!
          Thanks~!!:-)

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by huan
            OK~!Got it~!
            Thanks~!!:-)
            No prob. Glad to help.


            Adrian

            Comment

            Working...