Skip to content

Instantly share code, notes, and snippets.

Created September 8, 2015 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/279c6826de5cfc22c488 to your computer and use it in GitHub Desktop.
Save anonymous/279c6826de5cfc22c488 to your computer and use it in GitHub Desktop.
The first example's second case won't compile (complains it expected a class after ~) but the second one through the template does. Anyone know what I'm doing wrong?
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <malloc/malloc.h>
using namespace std;
int main()
{
void *p = malloc(sizeof(std::string));
std::string *a = new(p) std::string();
a->~string();
size_t size = malloc_size(p);
printf("after destructing size = %ld\n", size);
void *q = malloc(sizeof(int));
int *i = new (q) int();
i->~int();
size = malloc_size(q);
printf("after destructing size = %ld\n", size);
return 0;
}
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <malloc/malloc.h>
using namespace std;
template <class T>
void DeleteIt(T *x) { x->~T(); }
int main()
{
void *p = malloc(sizeof(std::string));
std::string *a = new(p) std::string();
DeleteIt(a);
size_t size = malloc_size(p);
printf("after destructing size = %ld\n", size);
void *q = malloc(sizeof(int));
int *i = new (q) int();
DeleteIt(i);
size = malloc_size(q);
printf("after destructing size = %ld\n", size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment