new algorithm

Discussion in 'C++' started by wijhierbeneden, Oct 28, 2004.

  1. Hello

    Where can I find some info about the inner working of new/delete??
    Is this compiler specific or defined by the standard?? There is a lot
    of info about the different malloc implementations but I can't find
    anything about new.

    thx
    wijhierbeneden, Oct 28, 2004
    #1
    1. Advertising

  2. wijhierbeneden wrote:
    > Where can I find some info about the inner working of new/delete??
    > Is this compiler specific or defined by the standard?? There is a lot
    > of info about the different malloc implementations but I can't find
    > anything about new.


    What do you want to know? What C++ book do you have?
    Victor Bazarov, Oct 28, 2004
    #2
    1. Advertising

  3. wijhierbeneden

    Ron Natalie Guest

    wijhierbeneden wrote:
    > Hello
    >
    > Where can I find some info about the inner working of new/delete??
    > Is this compiler specific or defined by the standard??


    The general behavior is defined by the standard in 3.7.3 that talks
    about the allocation functions (operator new/delete) themselves,
    5.3.4 and 5.3.5 which talk about the new/delete expressions themselves,
    and of course 8.5 and 12.1 which talk about how initialization/consturctor
    invocation occurs.

    > There is a lot
    > of info about the different malloc implementations but I can't find
    > anything about new.


    The standard doesn't discuss the actual implentation of the allocation
    deallocation function internals (just their interfaces) other than to
    point out that they are designed to be able to be impelemented on top of
    malloc/delete without much toil. In actuality, most implementations
    do exactly that. Since they need to coexist with malloc, you have to
    pretty much implement one in terms of the other or have them both call
    a third common allocator.
    Ron Natalie, Oct 28, 2004
    #3
  4. wijhierbeneden wrote:

    > Hello
    >
    > Where can I find some info about the inner working of new/delete??
    > Is this compiler specific or defined by the standard?? There is a lot
    > of info about the different malloc implementations but I can't find
    > anything about new.
    >
    > thx


    the end results are defined by the standard, but the compiler has a
    certain amount of leeway in implementation. Basically what you are
    looking for new() is the equivalent of a call to malloc() (assuming you
    are using the allocating new rather than the placement new) followed by
    an invocation of an appropriate constructor if the object being allocated
    has one. If the allocation fails, the user define new handler may be
    called, and if that fails an exception may be thrown. If allocating an
    array, a constructor is called for each element of the array as
    appropriate. For delete, a destructor is called for the object (or each
    object in the array) and then the space is freed.

    David
    David Lindauer, Oct 29, 2004
    #4
  5. Ron Natalie <> wrote in message news:<41815851$0$28303$>...
    > wijhierbeneden wrote:
    > > Hello
    > >
    > > Where can I find some info about the inner working of new/delete??
    > > Is this compiler specific or defined by the standard??

    >
    > The general behavior is defined by the standard in 3.7.3 that talks
    > about the allocation functions (operator new/delete) themselves,
    > 5.3.4 and 5.3.5 which talk about the new/delete expressions themselves,
    > and of course 8.5 and 12.1 which talk about how initialization/consturctor
    > invocation occurs.
    >
    > > There is a lot
    > > of info about the different malloc implementations but I can't find
    > > anything about new.

    >
    > The standard doesn't discuss the actual implentation of the allocation
    > deallocation function internals (just their interfaces) other than to
    > point out that they are designed to be able to be impelemented on top of
    > malloc/delete without much toil. In actuality, most implementations
    > do exactly that. Since they need to coexist with malloc, you have to
    > pretty much implement one in terms of the other or have them both call
    > a third common allocator.



    Is there no info about the exact inner implementations available??
    That is what i want, i want to know how the Virtual table is created
    how the object is build on the heap, ...
    That's why i need the inner working of new
    There must be some info about that, no??
    wijhierbeneden, Oct 29, 2004
    #5
  6. wijhierbeneden wrote:
    > Ron Natalie <> wrote in message news:<41815851$0$28303$>...

    ....
    >
    > Is there no info about the exact inner implementations available??
    > That is what i want, i want to know how the Virtual table is created
    > how the object is build on the heap, ...
    > That's why i need the inner working of new
    > There must be some info about that, no??


    the new operator is associated with dynamically created objects only.

    The compiler is responsible for dealing with how virtual functions and
    auto objects are allocated. This almost certainly has nothing to do
    with new.
    Gianni Mariani, Oct 29, 2004
    #6
  7. (wijhierbeneden) wrote in message news:<>...
    > Hello
    >
    > Where can I find some info about the inner working of new/delete??
    > Is this compiler specific or defined by the standard?? There is a lot
    > of info about the different malloc implementations but I can't find
    > anything about new.
    >
    > thx


    A good book for the inner workings of C++ is "Inside the C++ Object
    Model" by Stanley Lippman
    Stuart Gerchick, Oct 29, 2004
    #7
  8. Gianni Mariani <> wrote in message news:<>...
    > wijhierbeneden wrote:
    > > Ron Natalie <> wrote in message news:<41815851$0$28303$>...

    > ...
    > >
    > > Is there no info about the exact inner implementations available??
    > > That is what i want, i want to know how the Virtual table is created
    > > how the object is build on the heap, ...
    > > That's why i need the inner working of new
    > > There must be some info about that, no??

    >
    > the new operator is associated with dynamically created objects only.
    >
    > The compiler is responsible for dealing with how virtual functions and
    > auto objects are allocated. This almost certainly has nothing to do
    > with new.


    But the pointer to the virtual table is located inside the object.
    Doesn't new has to create the pointer (the place where it points to
    can be chosen by the compiler).
    wijhierbeneden, Oct 29, 2004
    #8
  9. wijhierbeneden wrote:
    > Gianni Mariani <> wrote in message news:<>...

    ....
    >>The compiler is responsible for dealing with how virtual functions and
    >>auto objects are allocated. This almost certainly has nothing to do
    >>with new.

    >
    >
    > But the pointer to the virtual table is located inside the object.
    > Doesn't new has to create the pointer (the place where it points to
    > can be chosen by the compiler).


    The constructor has this responsibility.
    Gianni Mariani, Oct 29, 2004
    #9
  10. wijhierbeneden wrote:
    >
    > Gianni Mariani <> wrote in message news:<>...
    > > wijhierbeneden wrote:
    > > > Ron Natalie <> wrote in message news:<41815851$0$28303$>...

    > > ...
    > > >
    > > > Is there no info about the exact inner implementations available??
    > > > That is what i want, i want to know how the Virtual table is created
    > > > how the object is build on the heap, ...
    > > > That's why i need the inner working of new
    > > > There must be some info about that, no??

    > >
    > > the new operator is associated with dynamically created objects only.
    > >
    > > The compiler is responsible for dealing with how virtual functions and
    > > auto objects are allocated. This almost certainly has nothing to do
    > > with new.

    >
    > But the pointer to the virtual table is located inside the object.
    > Doesn't new has to create the pointer (the place where it points to
    > can be chosen by the compiler).


    The purpose of new (if you write your own) is to get your
    hands at some memory and return a pointer to it. For this new
    is given the size of the requested memory. The details of
    object construction in that memory are taken care of by the code
    which calls new.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 29, 2004
    #10
  11. > Where can I find some info about the inner working of new/delete??
    > Is this compiler specific or defined by the standard?? There is a lot
    > of info about the different malloc implementations but I can't find
    > anything about new.


    Would you like to read this?
    - Freestore management
    http://www.fmi.uni-konstanz.de/~kuehl/c -faq/freestore-mgmt.html

    - Memory Management
    http://gotw.ca/gotw/009.htm
    http://gotw.ca/gotw/010.htm

    - http://en.wikipedia.org/wiki/C_Plus_Plus

    - http://en.wikipedia.org/wiki/Memory_allocation

    - http://citeseer.ist.psu.edu/cis?q=malloc

    - Discussion "memory allocation libraries for multithreaded
    applications"
    http://groups.google.de/groups?threadm=

    How do you think about to look at the sources of free compilers if you
    are interested in implementation details?

    Regards,
    Markus
    Markus Elfring, Oct 29, 2004
    #11
  12. wijhierbeneden

    Ron Natalie Guest

    Gianni Mariani wrote:
    > wijhierbeneden wrote:
    >
    >> Gianni Mariani <> wrote in message
    >> news:<>...

    >
    > ...
    >
    >>> The compiler is responsible for dealing with how virtual functions
    >>> and auto objects are allocated. This almost certainly has nothing to
    >>> do with new.

    >>
    >>
    >>
    >> But the pointer to the virtual table is located inside the object.
    >> Doesn't new has to create the pointer (the place where it points to
    >> can be chosen by the compiler).

    >
    >
    > The constructor has this responsibility.


    The C++ notion of the constructor DOES NOT. Implementations may
    piggy back that initialization on the constructor linkage, but that's
    just an impelementation detail.
    Ron Natalie, Oct 29, 2004
    #12
  13. Gianni Mariani wrote:

    > wijhierbeneden wrote:
    >
    >> Gianni Mariani wrote:

    >
    > ...
    >
    >>> The compiler is responsible
    >>> for dealing with how virtual functions and auto objects are allocated.
    >>> This almost certainly has nothing to do with new.

    >>
    >> But the pointer to the virtual table is located inside the object.
    >> Doesn't new has to create the pointer
    >> (the place where it points to can be chosen by the compiler).

    >
    > The constructor has this responsibility.


    > cat C.h

    #ifndef GUARD_C_H

    class C {
    private:
    // representation
    int I;
    // constructors
    C(int i = 0);
    virtual
    ~C(void);
    };

    #define GUARD_C_H
    #endif//GUARD_C_H

    > cat C.cc

    #include "C.h"

    C::C(int i): I(i) { }
    C::~C(void) { }

    > g++ -Wall -ansi -pedantic -S C.cc
    > cat C.s
    E. Robert Tisdale, Oct 29, 2004
    #13
  14. Gianni Mariani <> wrote in message news:<>...
    > wijhierbeneden wrote:
    > > Gianni Mariani <> wrote in message news:<>...

    > ...
    > >>The compiler is responsible for dealing with how virtual functions and
    > >>auto objects are allocated. This almost certainly has nothing to do
    > >>with new.

    > >
    > >
    > > But the pointer to the virtual table is located inside the object.
    > > Doesn't new has to create the pointer (the place where it points to
    > > can be chosen by the compiler).

    >
    > The constructor has this responsibility.


    Why can the compiler know where the pointer must point to??
    This is determind at runtime. So new has to do this i assume.
    wijhierbeneden, Oct 30, 2004
    #14
  15. "wijhierbeneden" <> wrote in message
    news:...
    > Gianni Mariani <> wrote in message

    news:<>...
    > > wijhierbeneden wrote:
    > > > Gianni Mariani <> wrote in message

    news:<>...
    > > ...
    > > >>The compiler is responsible for dealing with how virtual functions and
    > > >>auto objects are allocated. This almost certainly has nothing to do
    > > >>with new.
    > > >
    > > >
    > > > But the pointer to the virtual table is located inside the object.
    > > > Doesn't new has to create the pointer (the place where it points to
    > > > can be chosen by the compiler).

    > >
    > > The constructor has this responsibility.

    >
    > Why can the compiler know where the pointer must point to??
    > This is determind at runtime. So new has to do this i assume.


    Which pointer are you talking about? The constructor is responsible for
    assigning the pointer to a virtual table. There is one virtual table for
    each class, so it is easy for the compiler to assign this.

    The pointer to the object itself is of course determined by new.

    john
    John Harrison, Oct 30, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    0
    Views:
    777
    Ahmed Moustafa
    Nov 15, 2003
  2. Markus Elfring

    Re: new algorithm

    Markus Elfring, Nov 1, 2004, in forum: C++
    Replies:
    2
    Views:
    342
    red floyd
    Nov 6, 2004
  3. Bapaiah Katepalli
    Replies:
    1
    Views:
    1,495
    Mike Treseler
    Jun 23, 2006
  4. Replies:
    10
    Views:
    669
  5. Replies:
    2
    Views:
    456
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page