Mixing "untyped" operator new and "typed" delete

I want to make a class factory that can create classes knowing nothing about them (eg no headers included). I created a factory class with map, that stores info about classes (sizeof and pointer to static method) by strings ( map(string, mystruct) ), which is filled from each class.

When factory is producing a class, it allocates raw memory and calls static method, which then calls the default constructor on that memory:

void *ptr = operator new(classSize); classCCaller(ptr);//void (*classCCaller)(void*)

then inside the static method of that class the following code is executed:

new (ptr) SomeClassConstructor();

The produced class is then put in some container.

Question - is it ok to delete classes, created that way, by

delete p;//SomeClass *p;

or should I manually call destructor and use operator delete on untyped pointer?

-------------Problems Reply------------

No, you have to say p->~SomeClass(); operator delete(ptr);, where p = static_cast<SomeClass*>(ptr).

Category:c# Views:4 Time:2019-01-08

Related post

  • Would combining raw operator new, placement new and standard delete be legal? 2011-07-09

    guys! Out of curiosity – the following code would probably not be legal, would it? T *p = ::operator new(sizeof(T)); // allocate memory for a T new (p) T; // construct a T into the allocated memory delete p; //delete the object using the standard del

  • Difference between operator new and operator new[]? 2010-01-17

    I've overloaded the global operator new/delete/new[]/delete[] but simple tests show that while my versions of new and delete are being called correctly, doing simple array allocations and deletes with new[] and delete[] causes the implementations in

  • Mixing operator new[] and placement new with ordinary delete[] 2011-06-27

    Just out of curiosity, is the following legal? X* p = static_cast<X*>(operator new[](3 * sizeof(X))); new(p + 0) X(); new(p + 1) X(); new(p + 2) X(); delete[] p; // Am I allowed to use delete[] here? Or is it undefined behavior? Similarly: X* q

  • Prevent inheriting operator new and delete 2011-08-25

    (I did scan, but couldn't find anything similiar, if dupe please close). Is there a way to prevent these two operators from being inherited? For example: struct foo{ static void* operator new(std::size_t) { // special } static void operator delete(vo

  • Operator new and bad_alloc on linux 2009-10-20

    On Linux, malloc doesn't necessarily return a null pointer if you're out of memory. You might get back a pointer and then have the OOM killer start eating processes if you're really out of memory. Is the same true for c++'s operator new or will you g

  • Strange issues with cursor and typing / deleting 2014-10-09

    Why is the cursor typing / deleting with a few letters ahead of the place where I place the cursor? This happens in Google Documents only. How to fix it? Thank you. --------------Solutions------------- Since it only happens in Google Documents you ma

  • C++ : Structs, operator "new", and const 2015-08-10

    I'm going to be speaking in hypotheticals because I haven't tested anything as of yet. I'm not even sure where to begin. So I have a few structs that I initialized with values and I want them to be passed to a class. But I want each instance of the c

  • Is it "safe" on Linux to mix `new[]` and `delete`? 2012-01-20

    Someone on IRC claimed that, although allocating with new[] and deleting with delete (not delete[]) is UB, on Linux platforms (no further details about the OS) it would be safe. Is this true? Is it guaranteed? Is it to do with something in POSIX that

  • Stop calling overloaded new and delete operator by stl containers 2011-02-09

    I am developing a memoryleak tool. In this i am overloading new and delete operator. Its working fine. But the code for which i am creating this tool is around 15000 lines. I cannot change the existing code only i can invoke the memoryleak tool funct

  • Overloading global operator new/delete in C++ 2009-03-12

    I am trying to overload the global operator new and delete for a performance sensitive application. I have read the concerns described at http://www.informit.com/articles/article.aspx?p=30642&seqNum=3 and the recommendations to use Intel TBB's al

  • Difference between 'new operator' and 'operator new'? 2009-12-11

    What is difference between "new operator" and "operator new"? Thanks. --------------Solutions------------- I usually try to phrase things differently to differentiate between the two a bit better, but it's a good question in any case. Operator new is

  • Recursive calls when trying to wrap and override the global operator new 2009-12-13

    I have not programmed C++ for a while and encountered a strange behavior when toying with overloaded global operators new and delete. The essence of the problem seems to be that a wrapper build around the default global new and residing in a separate

  • operator new overloading and alignment 2010-03-02

    I'm overloading operator new, but I recently hit a problem with alignment. Basically, I have a class IBase which provides operator new and delete in all required variants. All classes derive from IBase and hence also use the custom allocators. The pr

  • Does STL Vector use 'new' and 'delete' for memory allocation by default? 2010-11-11

    I am working on a plugin for an application, where the memory should be allocated by the Application and keep track of it. Hence, memory handles should be obtained from the host application in the form of buffers and later on give them back to the ap

  • How should I write ISO C++ Standard conformant custom new and delete operators? 2011-08-25

    How should I write ISO C++ standard conformant custom new and delete operators? This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ, Operator overloading, and its follow-up, Why should one replace default new a

  • new and delete in C program 2011-12-04

    I have the following algorithm to implement new in C void *newinc(unsigned int s) { // allocate and align storage of size s // handle failure via exception // return pointer to storage } I have the following implementation: void *newinc(unsigned int

  • new and delete in wdk 2012-05-02

    I am building a code using the native subsystem and I have problem with operator new and delete. I want to use WDK new and delete but I get these errors: : error LNK2019: unresolved external symbol "void __cdecl operator delete(void *)" ([email protected]@Z

  • Legality and morality if differeing scopes of 'new' and 'delete' 2012-01-23

    I am creating a dynamic array inside a function. The code (posted below) runs without any problem. I am wondering if the way I have written is the right way to do it or if it will create problems in more complicated codes in the future. I am aware th

  • Are new and delete still useful in C++14? 2018-10-30

    Given availability of make_unique and make_shared, as well as automatic deletion by unique_ptr and shared_ptr destructors, what are the situations (apart from supporting legacy code) for using new and delete in C++14? --------------Solutions---------

  • Which to use - "operator new" or "operator new[]" - to allocate a block of raw memory in C++? 2010-03-23

    My C++ program needs a block of uninitialized memory and a void* pointer to that block so that I can give it to a third party library. I want to pass control of the block lifetime to the library, so I don't want to use std::vector. When the library i

  • Macro to replace C++ operator new 2009-03-06

    Is it possible to create macros to replace all forms of operator new with overloads that include additional args...say __FILE__ and __LINE__? The trouble appears to be that operator new can either be coded with or without parentheses, therefore: obje

  • Is it possible to replace the global "operator new()" everywhere? 2010-01-20

    I would like to replace the global operator new() and operator delete() (along with all of their variants) in order to do some memory management tricks. I would like all code in my application to use the custom operators (including code in my own DLL

  • On C++ global operator new: why it can be replaced 2010-04-28

    I wrote a small program in VS2005 to test whether C++ global operator new can be overloaded. It can. #include "stdafx.h" #include "iostream" #include "iomanip" #include "string" #include "new" using namespace std; class C { public: C() { cout<<

  • C# Free memory allocated by operator new from p/invoke DLL 2010-09-01

    I have a C# application which is using a third party (closed source) native DLL through p/invoke. During some of these calls, the DLL will allocate memory using operator new / operator new[] and return a pointer to use for reading the results. This m

  • How could I sensibly overload placement operator new? 2010-09-09

    C++ allows overloading operator new - both global and per-class - usual operator new, operator new[] used with new[] statement and placement operator new separately. The former two of those three are usually overloaded for using customized allocators

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.068 (s). 11 q(s)