Hi,
Can i use realloc to deallocate the memory allocated using new operator. For example:

int * i = new int (10);
realloc (i, 0);

Thanks in advance.

Recommended Answers

All 12 Replies

You can, but it might not turn out well. I don't think malloc/realloc/free are supposed to be mixed with new/delete. If you want to realloc some memory allocated with new, you should do some kind of allocate-copy-free pattern.

template <typename T>
T *MyRealloc( T*& src, int oldSize, int newSize ) {
  T *dst = new T[newSize];

  if ( newSize < oldSize ) {
    // The block is getting smaller
    // Truncate anything past the new end
    oldSize = newSize;
  }

  while ( --oldSize >= 0 ) {
    dst[oldSize] = src[oldSize];
  }

  // Free the old block and reuse the pointer
  delete[] src;
  src = dst;

  // Return the pointer for convenience
  return src;
}

> Can i use realloc to deallocate the memory allocated using new operator
you should not. see http://www.research.att.com/~bs/bs_faq2.html#realloc
as suggested, using a vector is the simplest solution.

hamrick's idea has some potential problems:

T *dst = new T[newSize];

will only work if T has accessible default initialization (a trivial constructor or a public default constructor)

while ( --oldSize >= 0 ) { dst[oldSize] = src[oldSize]; }

wil work only if T has an accessible assignment operator

even if both are available for T, constructing by default and then assigning may not be efficient.

instead you could use malloc/free/realloc with calls to placement new (or or std::uninitialized_copy) and direct calls of destructor. using a std::allocator<T>::allocate/construct/destroy/deallocate would also work, but that is what a std library container (like vector) does in any case.

and since you have asked this question, that is not something you should attempt at this stage. just use a std::vector for now.

Should I not be trying to help? You guys are much better programmers than I am so tell me if I'm doing more harm than good and I'll shut up until I'm more of an expert. I don't want to lead people astray.

Should I not be trying to help? ... and I'll shut up until I'm more of an expert. ...

you should definitely try to help to the best of your ability. that is how you can enrich the community. and please do not treat a technical comment on a post you made as a personal attack. i'm sorry if it upset you; that was not my intention at all. so please continue contributing to the forum as you have been doing; you will be doing no harm, but a lot of good. to others and to yourselves.

Oh, I didn't mean it like that. I'm not upset at all. It's just that this is the second time in two or three posts that someone has implied that my answer wasn't helpful because the code was bad.

... my answer wasn't helpful because ...

on the contrary, your answer was very helpful; the fundamental difference between C and C++ memory management is that in C++, we have variables that may require non-trivial initialization/deinitialization (construction/destruction) and your answer helped bring that difference into clear focus.

Ooh, so I'm here to be a warning to others by providing bad examples for smarter people to tear apart? ;)

Member Avatar for iamthwee

Correct.

And don't feel bad if someone posts a correction to whatever you said -- none of us are perfect. And you might be supprised at how much I've learned since joining DaniWeb too. Development is a life-long learning process and nobody stops learning.

And don't feel bad if someone posts a correction to whatever you said -- none of us are perfect.

I'm good with someone correcting me like vijayan121 did. That's productive and I learn from it. What I'm not good with is someone using various unhelpful phrases to describe my code without offering constructive suggestions. I've seen that all over from reading older posts and recently received such a critique myself.

Oh well, just a little venting. Feel free to ignore me. :)

I don't know how many others do this, but I've often found myself drafting replies to problems which people have posted, but not actually sending my reply straight away. On many occasions, i've kept the reply in a notepad document, and ended up deleting them, if I think i'm wrong.

What I find interesting, is comparing my drafted reply with the real replies which others like SoS, Salem, Ancient Dragon, Narue, etc come up with. (All of whom make my knowledge of C/C++ look fairly humble)

I can't even begin to quantify how much I've learned by simply drafting replies and comparing them to other peoples' responses over the past couple of years (Its also stopped me posting some of my sloppier responses, which, in the past, got me burnt fingers..)

Sorry for straying off-topic, but it seems appropriate in this thread now. :)

You should never mix new/delete with malloc/realloc/free and other c-functions
The new and delete being the "operators" in c++, allocates the memory as well as call the constructors and destructors. However the c functions just play with the memory chunks.

also in c++, a class manages the data of its own so i think there is no sense in using realloc or free etc functions on the "object pointers". However if needed, the object's methods can use realloc internally.
Please see the code below to see the difference between the new/delete and c-library functions:

#include <iostream>

using namespace std;

class Test{
    public:
    Test();
    ~Test();
};

Test::Test()
{
    cout<<"Test Constructor Invoked\n";
}

Test::~Test()
{
    cout<<"Test Destructor Invoked\n";
}
int main()
{

Test * T_new[2];
T_new[0] = new Test;
T_new[1] = new Test;
Test * T_malloc = (Test*)malloc(sizeof(Test));

delete T_new[0];
realloc(T_new[1], 0);
realloc(T_malloc,0);//  equivalent to free. See http://linux.die.net/man/3/realloc

return 0;
}
commented: bump 3 year old thread -7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.