Placement new - application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • elviin@gmail.com

    Placement new - application

    Hello.
    I tried a sample programm using placement new. I am not sure if i
    understand this technique correctly. I do not mean usage but a real
    world application. Could anyone to describe some applications where it
    gives reason to use placement new? My doubts come from my consideration
    that if I delete an object (e.g. 50MB) then system can allocate this
    chunk of memory to another process and my process will not be able to
    re-use this segment. This technique is good for preventing memory from
    fragmentation. Because if I'm using the same segment of memory it
    should be more sensible to keep this memory and use the same object and
    just update it's members according to "streams" from outside. Can
    anyone explain me the steps from new() to delete() to another
    allocation with placement new()? Thank you.

    Elviin Nela

  • Jacques Labuschagne

    #2
    Re: Placement new - application

    elviin@gmail.co m wrote:
    [color=blue]
    > Hello.
    > I tried a sample programm using placement new. I am not sure if i
    > understand this technique correctly. I do not mean usage but a real
    > world application. Could anyone to describe some applications where it
    > gives reason to use placement new?[/color]

    How about for constructing objects in memory not allocated by malloc?
    Say shared memory used for inter-process communication.

    Jacques.

    Comment

    • Peter Koch Larsen

      #3
      Re: Placement new - application


      <elviin@gmail.c om> skrev i en meddelelse
      news:1117007754 .884320.285950@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > Hello.
      > I tried a sample programm using placement new. I am not sure if i
      > understand this technique correctly. I do not mean usage but a real
      > world application. Could anyone to describe some applications where it
      > gives reason to use placement new? My doubts come from my consideration
      > that if I delete an object (e.g. 50MB) then system can allocate this
      > chunk of memory to another process and my process will not be able to
      > re-use this segment. This technique is good for preventing memory from
      > fragmentation. Because if I'm using the same segment of memory it
      > should be more sensible to keep this memory and use the same object and
      > just update it's members according to "streams" from outside. Can
      > anyone explain me the steps from new() to delete() to another
      > allocation with placement new()? Thank you.
      >
      > Elviin Nela
      >[/color]

      While the standard operator new has two steps, namely to (1) allocate memory
      and (2) create an object from that raw memory, the placement operator new
      skips the first step, using the supplied memory to construct the object. It
      is useful several places, e.g. when you implement a container (std::vector
      almost certainly uses placement new).
      Perhaps I'm stupid, but i do not see how this helps avoid memory
      fragmentation. If you have the memory for your "expensive" object present
      all the time, you could just avoid destroying the object until you finish
      your program. In case you need to reinitialize the object, it would be a
      better idea - in my opinion - to have a "reset" function available.

      /Peter


      Comment

      • red floyd

        #4
        Re: Placement new - application

        elviin@gmail.co m wrote:[color=blue]
        > Hello.
        > I tried a sample programm using placement new. I am not sure if i
        > understand this technique correctly. I do not mean usage but a real
        > world application. Could anyone to describe some applications where it
        > gives reason to use placement new? My doubts come from my consideration
        > that if I delete an object (e.g. 50MB) then system can allocate this
        > chunk of memory to another process and my process will not be able to
        > re-use this segment. This technique is good for preventing memory from
        > fragmentation. Because if I'm using the same segment of memory it
        > should be more sensible to keep this memory and use the same object and
        > just update it's members according to "streams" from outside. Can
        > anyone explain me the steps from new() to delete() to another
        > allocation with placement new()? Thank you.
        >
        > Elviin Nela
        >[/color]

        Memory mapped hardware in an embedded system.

        Comment

        • elviin@gmail.com

          #5
          Re: Placement new - application

          I'd like to know how it is done when I call desctructor (after
          placement new) so that the system keeps (??) the segment of memory for
          my process until next placement new. Because I was somewhere reading
          that it prevents memory from fragmentation - sorry I do not understand
          this statement.

          Comment

          • red floyd

            #6
            Re: Placement new - application

            elviin@gmail.co m wrote:[color=blue]
            > I'd like to know how it is done when I call desctructor (after
            > placement new) so that the system keeps (??) the segment of memory for
            > my process until next placement new. Because I was somewhere reading
            > that it prevents memory from fragmentation - sorry I do not understand
            > this statement.
            >[/color]

            You don't call delete. You call the destructor directly.

            T* p = new(placment);
            // do stuff

            p->~T();

            // object is destroyed.

            Comment

            • elviin@gmail.com

              #7
              Re: Placement new - application

              i'm sorry... I wanted to write dtor.

              Comment

              • CodeCracker

                #8
                Re: Placement new - application



                elviin@gmail.co m wrote:[color=blue]
                > i'm sorry... I wanted to write dtor.[/color]

                Isn't it said that if you provide implementation of placement new you
                also have to provide implementation of the placement delete which would
                take care of returning the allocated block to the common pool
                maintained by the application.

                Comment

                • Peter Koch Larsen

                  #9
                  Re: Placement new - application


                  "CodeCracke r" <sanjaym365@gma il.com> skrev i en meddelelse
                  news:1117056897 .265865.159720@ g49g2000cwa.goo glegroups.com.. .[color=blue]
                  >
                  >
                  > elviin@gmail.co m wrote:[color=green]
                  >> i'm sorry... I wanted to write dtor.[/color]
                  >
                  > Isn't it said that if you provide implementation of placement new you
                  > also have to provide implementation of the placement delete which would
                  > take care of returning the allocated block to the common pool
                  > maintained by the application.
                  >[/color]
                  Nope. If you got the object via placement new, you would not normally delete
                  it. Your scenario looks more like you provided your own new operator in
                  which case you will also provide the delete operator.

                  /Peter


                  Comment

                  Working...