Created
December 4, 2018 17:54
-
-
Save lambdageek/eb070e1b16d6e54dc7f0c22793291e4f to your computer and use it in GitHub Desktop.
C++ dynamic alloc wrapper using placement forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Undefined symbols for architecture x86_64: | |
"operator delete(void*)", referenced from: | |
A::~A() in pla.o | |
B::~B() in pla.o | |
ld: symbol(s) not found for architecture x86_64 | |
clang: error: linker command failed with exit code 1 (use -v to see invocation) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include "me.hpp" | |
int | |
main () | |
{ | |
A *a = factory (); | |
g_delete(a); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
clang -xc++ -c -Wall -std=c++11 -Wextra -fno-rtti -fno-exceptions hark.cpp pla.cpp | |
clang -fno-rtti -fno-exceptions -o hark hark.o pla.o | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _MY_ME_HPP | |
#define _MY_ME_HPP | |
#include <stdlib.h> | |
#include <new> | |
#include <utility> | |
inline void* | |
g_malloc (size_t sz) | |
{ | |
return malloc (sz); | |
} | |
inline void | |
g_free (void *ptr) | |
{ | |
free (ptr); | |
} | |
template<typename T, typename... Args> | |
inline | |
T* g_new(Args&&... args) | |
{ | |
void* p = g_malloc (sizeof (T)); | |
T* t = new (p) T(std::forward<Args>(args)...); | |
return t; | |
} | |
template<typename T> | |
inline | |
void | |
g_delete (T *ptr) | |
{ | |
ptr->~T(); | |
g_free (static_cast<void*>(ptr)); | |
} | |
class A { | |
public: | |
A () = default; | |
virtual ~A(); | |
}; | |
class B : public A { | |
public: | |
B () = default; | |
virtual ~B(); | |
}; | |
A* factory (); | |
#endif | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <utility> | |
#include "me.hpp" | |
A::~A() { | |
printf ("A destructor %p\n", static_cast<void*>(this)); | |
} | |
B::~B() { | |
printf ("B destructor %p\n", static_cast<void*>(this)); | |
} | |
A* factory() { | |
return g_new<B> (); | |
} |
That seems wierd. I can try to look into it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just calling a destructor relies on
::operator delete(void*)
being available, apparently.