| | |
new implementation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>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.
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'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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 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?
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'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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?
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.
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.
I'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
•
•
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.
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
•
•
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.
•
•
•
•
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
>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.
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- doubly linked list implementation (Java)
- dividing class implementation (C)
- implementation of interface to optimize the search engines using sementics (Java)
- CA Etrust Audit Software Implementation (Windows NT / 2000 / XP)
- CA Etrust Audit Software Implementation (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: this code isn't working...why..plz help
- Next Thread: formatting help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






