Skip to content

Instantly share code, notes, and snippets.

@lambdageek
Created December 4, 2018 17:54
Show Gist options
  • Save lambdageek/eb070e1b16d6e54dc7f0c22793291e4f to your computer and use it in GitHub Desktop.
Save lambdageek/eb070e1b16d6e54dc7f0c22793291e4f to your computer and use it in GitHub Desktop.
C++ dynamic alloc wrapper using placement forms
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)
#include <stdlib.h>
#include "me.hpp"
int
main ()
{
A *a = factory ();
g_delete(a);
return 0;
}
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
#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
#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> ();
}
@lambdageek
Copy link
Author

Just calling a destructor relies on ::operator delete(void*) being available, apparently.

@jaykrell
Copy link

jaykrell commented Dec 4, 2018

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