Click here to Skip to main content
15,920,633 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic22-Oct-02 15:58
Nemanja Trifunovic22-Oct-02 15:58 
GeneralRe: overloaded operator new and STL vector Pin
Christian Graus22-Oct-02 16:01
protectorChristian Graus22-Oct-02 16:01 
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic22-Oct-02 16:10
Nemanja Trifunovic22-Oct-02 16:10 
GeneralRe: overloaded operator new and STL vector Pin
Christian Graus22-Oct-02 16:17
protectorChristian Graus22-Oct-02 16:17 
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic22-Oct-02 18:46
Nemanja Trifunovic22-Oct-02 18:46 
GeneralRe: overloaded operator new and STL vector Pin
Christian Graus22-Oct-02 18:48
protectorChristian Graus22-Oct-02 18:48 
GeneralRe: overloaded operator new and STL vector Pin
Joaquín M López Muñoz22-Oct-02 21:09
Joaquín M López Muñoz22-Oct-02 21:09 
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic23-Oct-02 5:10
Nemanja Trifunovic23-Oct-02 5:10 
Hello, Joaquín. Thanks for your answer.

Joaquín M López Muñoz wrote:
This is the first problem in your program. An overloaded new operator is meant to be taking care of memory allocation only: the call to the class contructor is generated automatically by the compiler. Please note that the new you provide is calling implicitly the ctor through the use of golbal ::new (altough in this case the error will go unnoticed cause kasa has a trivial ctor).

As I already wrote to Christian, my original new and delete look quite different. I replaced my code with calls to global new and delete, because I wanted to make my examlple short and clear. I am aware of the issues you are talking about.


Joaquín M López Muñoz wrote:
In particular, it won't use the so-called placement new, whose signature is:void* operator new (size_t size,void* pos);
Placement new does not really do anything: the caller already provides the space (in pos) where the object is to be constructed.


Exactly. And that's what confuses me a lot. If you take a look at the implementation of placement new and delete in <new> you will see

inline void *__cdecl operator new(size_t, void *_P)
	{return (_P); }
#if     _MSC_VER >= 1200
inline void __cdecl operator delete(void *, void *)
	{return; }


What is this good for? Confused | :confused:


Joaquín M López Muñoz wrote:
Now, placement new is extensively used by allocators in STL, and this is what the compiler is complaining about: since you overloaded new you also have to provide an overload for the placement variant

You are 100% right. The default allocator uses placement new in function construct, and when I put this code in my class

void* operator new (size_t size,void* pos)  {    return pos;  }
void operator delete (void*, void*) {}


everything seems to work fine.

However, I still don't understand why. What is such implementation of placement new and delete good for? I know that in some cases you can provide i.e. your custom memory pool as a second argument, but here it is not the case. Why do they use this placement new in default allocator?

Beer | [beer]
GeneralRe: overloaded operator new and STL vector Pin
Nemanja Trifunovic23-Oct-02 5:36
Nemanja Trifunovic23-Oct-02 5:36 
QuestionHow to enumerate NICs? Pin
Anonymous22-Oct-02 12:11
Anonymous22-Oct-02 12:11 
GeneralDatabase and MFC! HHHEEEELLLLPPPP! Pin
MFC is the Best22-Oct-02 11:58
MFC is the Best22-Oct-02 11:58 
GeneralRe: Database and MFC! HHHEEEELLLLPPPP! Pin
Marc Clifton22-Oct-02 12:17
mvaMarc Clifton22-Oct-02 12:17 
GeneralRe: Database and MFC! HHHEEEELLLLPPPP! Pin
MFC is the Best22-Oct-02 12:43
MFC is the Best22-Oct-02 12:43 
GeneralRe: Database and MFC! HHHEEEELLLLPPPP! Pin
Marc Clifton22-Oct-02 13:13
mvaMarc Clifton22-Oct-02 13:13 
GeneralRe: Database and MFC! HHHEEEELLLLPPPP! Pin
Alexandru Savescu22-Oct-02 20:53
Alexandru Savescu22-Oct-02 20:53 
Questionedit box- change colors? Pin
jimNLX22-Oct-02 11:51
jimNLX22-Oct-02 11:51 
AnswerRe: edit box- change colors? Pin
Joaquín M López Muñoz22-Oct-02 11:54
Joaquín M López Muñoz22-Oct-02 11:54 
AnswerRe: edit box- change colors? Pin
adamUK22-Oct-02 21:45
adamUK22-Oct-02 21:45 
GeneralAssertion Failure Pin
monrobot1322-Oct-02 11:46
monrobot1322-Oct-02 11:46 
GeneralRe: Assertion Failure Pin
Chris Losinger22-Oct-02 11:49
professionalChris Losinger22-Oct-02 11:49 
GeneralRe: Assertion Failure Pin
Joaquín M López Muñoz22-Oct-02 11:50
Joaquín M López Muñoz22-Oct-02 11:50 
GeneralXML & STL Pin
Giles22-Oct-02 11:25
Giles22-Oct-02 11:25 
GeneralRe: XML & STL Pin
Christian Graus22-Oct-02 11:32
protectorChristian Graus22-Oct-02 11:32 
GeneralRe: XML & STL Pin
Giles22-Oct-02 11:41
Giles22-Oct-02 11:41 
GeneralRe: XML & STL Pin
Christian Graus22-Oct-02 12:01
protectorChristian Graus22-Oct-02 12:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.