I know I'm supposed to use new and delete, but I really need to use realloc, and I'm curious to how it can be done.

myclass {
...
public:
myclass()
{
cout << "Hi, I'm the constuctor" << endl;
};

int main()
{
myclass *m;
m = (myclass*) malloc (sizeof(myclass));
//m->myclass()  ?!
}

I don't think this is possible, as malloc is just allocating a block of memory for you, it won't actually put anything in.
Could you tell us why you really have to do this? It's often better to ask how to do something than 'why doesn't my implementation work'.

wow, the answer is ugly

myclass {
...
public:
myclass()
{
cout << "Hi, I'm the constuctor" << endl;
};

int main()
{
myclass *m;
m = (myclass*) malloc (sizeof(myclass));
m = &(myclass());
}

I mostly wanted to know out of curiousity

I don't know that you've found your answer yet:

#include <iostream>
using namespace std;

class myclass
{
public:
   myclass() { cout << "Hi, I'm the constuctor" << endl; }
};

int main()
{
   myclass *m;
   m = (myclass*) malloc (sizeof(myclass));
   m = &(myclass());
   return 0;
}

main.cpp:Error 50: Attempted to take the address of a non-lvalue
main.cpp:Warning 423: Creation of memory leak in assignment to 'm'

[edit] Here is a related thread elseweb.

Edited 6 Years Ago by Dave Sinkula: n/a

>I know I'm supposed to use new and delete, but I really need to use realloc
malloc/calloc, realloc, and free don't fit well at all with objects of a class type. You really should forget they exist unless you really know what you're doing. Fortunately, realloc can be simulated in C++ with minimal effort:

#include <algorithm>
#include <cstddef>

template <typename T>
T *array_realloc ( T *p, std::size_t old_n, std::size_t new_n )
{
  if ( new_n == 0 ) {
    delete[] p;
    p = 0;
  }
  else if ( p == 0 ) {
    p = new T[new_n];
  }
  else {
    T *save = new T[new_n];
    std::copy ( p, p + std::min ( old_n, new_n ), save );
    delete[] p;
    p = save;
  }

  return p;
}

>//m->myclass() ?!
The safe way to explicitly manage object memory is with a custom allocator. Here is the basic usage with the standard allocator:

#include <iostream>
#include <memory>

class myclass {
public:
  int x;
public:
  myclass(): x ( 0 ) { }
  myclass ( const myclass& o ): x ( o.x ) { }
};

namespace {
  myclass DEFAULT;
}

int main()
{
  std::allocator<myclass> alloc;
  myclass *p = alloc.allocate ( sizeof *p );

  alloc.construct ( p, DEFAULT );

  std::cout<< p->x <<'\n';

  alloc.destroy ( p );
  alloc.deallocate ( p, 1 );
}

However, you're looking for the wrong solution to your problem. The right solution to not having realloc is to write your own realloc, not monkey around with non-portable ways to call a constructor on random memory blocks.

Cool, I have no idea what this does tho:

std::copy ( p, p + std::min ( old_n, new_n ), save );

or this

namespace {
  myclass DEFAULT;
}

int main()
{
  std::allocator<myclass> alloc;
  myclass *p = alloc.allocate ( sizeof *p );

  alloc.construct ( p, DEFAULT );

  std::cout<< p->x <<'\n';

  alloc.destroy ( p );
  alloc.deallocate ( p, 1 );
}

Is this in the stl? Any good places to look these up?

>Cool, I have no idea what this does tho:
It's functionally equivalent to this:

// std::copy ( p, p + std::min ( old_n, new_n ), save );
std::size_t n = old_n < new_n ? old_n : new_n;

for ( int i = 0; i < n; i++ )
  save[i] = p[i];

>or this
So I would assume. I was giving you an example of a new concept, that concept being the use of allocators.

>Is this in the stl? Any good places to look these up?
The std::allocator class is in the standard library. You can read about it here.

This article has been dead for over six months. Start a new discussion instead.