Where can i find more info about the inner implementation of new/delete??
There is a lot of info about the different kinds of malloc but i can't find anything about new/delete.
IS this compiler specific or defined by the standard??

thx in advance

Recommended Answers

All 9 Replies

>IS this compiler specific or defined by the standard??
Only the interface is standard. How new and delete operate internally is up to the implementation. However, memory managers tend to be similar and you can make certain assumptions about the relationship between malloc/free and new/delete at the lowest level without being incorrect.

>IS this compiler specific or defined by the standard??
Only the interface is standard. How new and delete operate internally is up to the implementation. However, memory managers tend to be similar and you can make certain assumptions about the relationship between malloc/free and new/delete at the lowest level without being incorrect.

I don't want to make assumptions, i want the exact implementation. There must be a difference, example: creating of virtual table (or is this done by the compiler?)
I am writing my thesis and that's why i need the inner working
thx

>i want the exact implementation
Then you'll have to pick a compiler and read its source code.

>There must be a difference, example: creating of virtual table
No, the function behind new only allocates memory. It's basically the same thing as malloc and can even be implemented using malloc. Any C++ specific magic is performed by the operator new after new memory is returned. The memory manager simply grabs N bytes of free space. At that level, one of several generic memory management schemes will be used, and you've likely seen several of them in your research of malloc.

>I am writing my thesis and that's why i need the inner working
Is your thesis about dynamic memory management in general using C++ as a case study? Or is the topic how C++ handles dynamic memory?

>i want the exact implementation
Then you'll have to pick a compiler and read its source code.

>There must be a difference, example: creating of virtual table
No, the function behind new only allocates memory. It's basically the same thing as malloc and can even be implemented using malloc. Any C++ specific magic is performed by the operator new after new memory is returned. The memory manager simply grabs N bytes of free space. At that level, one of several generic memory management schemes will be used, and you've likely seen several of them in your research of malloc.

>I am writing my thesis and that's why i need the inner working
Is your thesis about dynamic memory management in general using C++ as a case study? Or is the topic how C++ handles dynamic memory?

One topic in my thesis is a comparision of new/delete versus malloc/free (and more specific dlmalloc).
I know how dlmalloc works and i know the difference in interface between them (malloc gives null if memory allocation fails and new throws an exception, .... )
I thougth that new intern uses malloc, but i am not sure. One reason was about the vritual table pointer and who declares it.
I must also search for the vulnerabilities in malloc and also what vulnerabilities new/delete has. But i can only look for the vulnerabilities of new if i know the inner working.

>One topic in my thesis is a comparision of new/delete versus malloc/free
This sounds more like a comparison of features than internal implementation. New is far more flexible because after the memory is allocated, it does more than malloc, which just allocates raw memory.

>I thougth that new intern uses malloc, but i am not sure.
In practice it doesn't for performance reasons, but in theory new could use malloc to get its memory.

>One reason was about the vritual table pointer and who declares it.
You keep going on about the virtual table, but it has nothing to do with how dynamic memory works. The virtual table for an object (if the implementation uses this method) is allocated by the constructor if the class has virtual functions. Virtual table allocation is one of the hidden tasks of the constructor, and the memory asked for will be adjusted accordingly. The memory manager doesn't give a hoot about virtual tables, it just allocates memory.

>But i can only look for the vulnerabilities of new if i know the inner working.
You can only look for vulnerabilities if you aren't confused about what does what.

>One topic in my thesis is a comparision of new/delete versus malloc/free
This sounds more like a comparison of features than internal implementation. New is far more flexible because after the memory is allocated, it does more than malloc, which just allocates raw memory.

Ok what does it more??

>
>One reason was about the vritual table pointer and who declares it.
You keep going on about the virtual table, but it has nothing to do with how dynamic memory works. The virtual table for an object (if the implementation uses this method) is allocated by the constructor if the class has virtual functions. Virtual table allocation is one of the hidden tasks of the constructor, and the memory asked for will be adjusted accordingly. The memory manager doesn't give a hoot about virtual tables, it just allocates memory.

The compiler handles virtual tables, but the pointer to the table resides in the object (on the heap/stack). If the object is on the heap, there must also been allocated room for the pointer. The compiler can't know what type the object shall be, so it can't create the pointer. I assumed that new does that job.

The compiler handles virtual tables, but the pointer to the table resides in the object (on the heap/stack). If the object is on the heap, there must also been allocated room for the pointer. The compiler can't know what type the object shall be, so it can't create the pointer. I assumed that new does that job.

My mistake, your previous explanation was correct, i wrote it too fast and not good enough. ;)

>One topic in my thesis is a comparision of new/delete versus malloc/free
This sounds more like a comparison of features than internal implementation. New is far more flexible because after the memory is allocated, it does more than malloc, which just allocates raw memory.

What does it more

>I thougth that new intern uses malloc, but i am not sure.
In practice it doesn't for performance reasons, but in theory new could use malloc to get its memory

How does it work?? Is there a general and most used algorithm??

>What does it more
When talking about new there are two distinct parts: the operator new, and the function operator new(). The last time I went over this with somebody, it took them ages to get it. What happens is that the operator new calls the function operator new() to allocate memory. The function operator new() acts just like malloc, it's a general memory manager that takes memory from a pool and doles it out to the program as requested. Just like malloc, operator new() also handles fragmentation issues and the like. Everything you learned about malloc applies to operator new().

The comes the operator new. Once it has the memory that it needs, operator new will then proceed to handle basic construction maintenance including--but not always limited to--calling constructors for the object to be created. You can think of operator new() as allocate_memory() and operator new as initialize_memory(). Operator new is the one that determines how much memory is needed (such as the space for a virtual table if the object is polymorphic) based on the type of object being created, operator new is the one that partitions the new memory as needed (such as any housekeeping data for the object that only the compiler knows about), operator new is the one that makes sure the memory is initialized before being used (such as calling the object's constructor). Once the constructor is called, it takes over further partitioning and initializing of the memory because operator new doesn't know the layout of the object.

>How does it work??
In the same way as malloc. The only reason malloc isn't actually used is to avoid the overhead of calling the function (or the considerable overhead of malloc itself), so standard implementations of operator new() will basically do the same thing as malloc without calling malloc itself and attempt to optimize the algorithm chosen as much as possible.

>Is there a general and most used algorithm??
Not to my knowledge. Every implementation that I've seen does it differently, but the operations are the same: dole out memory from a pool and rearrange the pool when possible to avoid fragmentation.

thx for all the info, now i really got the picture.
So now i am gonna search for the new algorthm implementation.

thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.