944,062 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5297
  • C++ RSS
Oct 28th, 2004
0

new implementation

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onsbomma is offline Offline
10 posts
since Oct 2004
Oct 28th, 2004
0

Re: new implementation

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 29th, 2004
0

Re: new implementation

Quote originally posted by Narue ...
>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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onsbomma is offline Offline
10 posts
since Oct 2004
Oct 29th, 2004
0

Re: new implementation

>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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 29th, 2004
0

Re: new implementation

Quote originally posted by Narue ...
>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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onsbomma is offline Offline
10 posts
since Oct 2004
Oct 29th, 2004
0

Re: new implementation

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 30th, 2004
0

Re: new implementation

Quote originally posted by Narue ...
>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??

Quote originally posted by Narue ...
>
>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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onsbomma is offline Offline
10 posts
since Oct 2004
Oct 30th, 2004
0

Re: new implementation

Quote originally posted by narue ...
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.
Quote originally posted by narue ...
>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
Quote originally posted by narue ...
>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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onsbomma is offline Offline
10 posts
since Oct 2004
Oct 30th, 2004
0

Re: new implementation

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 3rd, 2004
0

Re: new implementation

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

thx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onsbomma is offline Offline
10 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: this code isn't working...why..plz help
Next Thread in C++ Forum Timeline: formatting help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC