[ create a new paste ] login | about

Link: http://codepad.org/U3dauDhB    [ raw code | output | fork ]

AaronMiller - C++, pasted on May 20:
#include <cstdio>
#include <cstddef>
#include <cstdlib>
#include <cstring>

#include <new>
#include <string>

class Node {
public:
  Node();
  ~Node();

  void SetName(const char *name);
  const char *GetName() const;

protected:
  std::string mName;
};

Node::Node() {
}
Node::~Node() {
  mName.clear();
}

void Node::SetName(const char *name) {
  mName.assign(name);
}
const char *Node::GetName() const {
  return mName.c_str();
}

inline void *New(size_t n) {
  void *p;

  fprintf(stdout, "new(%u)\n", (unsigned int)n); fflush(stdout);

  p = malloc(n);
  if (!p) {
    fprintf(stderr, "ERROR: Failed to allocate memory.\n");
    exit(EXIT_FAILURE);
  }

  return p;
}
inline void Delete(void *p) {
  fprintf(stdout, "delete(%p)\n", p); fflush(stdout);

  if (!p)
    return;

  free(p);
}

void *operator new(size_t n) throw(std::bad_alloc) { return New(n); }
void *operator new(size_t n, const std::nothrow_t &) throw() { return New(n); }

void operator delete(void *p) throw() { Delete(p); }
void operator delete(void *p, const std::nothrow_t &) throw() { Delete(p); }

int main() {
  Node *p;

  p = new Node();
  p->SetName("Test");
  printf("%s\n", p->GetName());
  delete p;

  return EXIT_SUCCESS;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
new(8)
new(20)
new(112)
new(112)
new(48)
new(2)
delete(0x8050248)
new(8)
new(20)
new(112)
new(112)
new(48)
new(2)
delete(0x8050248)
new(4)
new(17)
Test
delete(0x8050460)
delete(0x8050438)


Create a new paste based on this one


Comments: