0

I'm learning C++ and in book section about overloading operator =, it says: "we need to free the old space and assign new values to each data element". My question is: why we need to deallocate memory when it's going to be overwritten anyway? Why can't we simply increment pointers and write to same memory (and data will be of same size since we'll assigning object to an object of a same type)? EDIT: Code from a book:

template <class T> Vec<T>& Vec<T>::operator=(const Vec& rhs) { // check for self-assignment if (&rhs != this) { // free the array in the left-hand side uncreate(); // copy elements from the right-hand to the left-hand side create(rhs.begin(), rhs.end()); } return *this; } 
2
  • You can reuse the same memory, just by copying over the contents. But, what if the data you want to copy is larger than the data you already have? Or considerably smaller (which will waste memory)? The easy solution to these (and other corner-cases) is to free the memory you have, allocate new of the exact size and do the copy. Commented Jan 16, 2015 at 21:09
  • Can you post the relevant snippets of code from the book? Commented Jan 16, 2015 at 21:16

2 Answers 2

2
  • Old allocation is of the wrong size.
  • Old object might be shared with others, cannot be overwritten.
  • Old object is const
  • Old object contains references.

And there might be others.

Sign up to request clarification or add additional context in comments.

2 Comments

1) How can objects of a same type have different sizes (if we're not talking about inheritance)? 2) Why would compiler allow deallocation, but not overwriting?
1) It's not the size of the object that matters, it's the size of the buffer allocated by the object. For example, a string could hold a 10-character buffer or a 100-character buffer. The whole point of an object controlling a dynamically-allocated buffer is that different objects (of the same type) could have buffers of various sizes. 2) That's how const works in C++ for example. If you have int const* p, you can delete p but you cannot overwrite *p.
1

You are right, you could try to reuse the memory that's already allocated. That is even likely a good idea if the memory is a buffer that is usually larger than the data it holds (like in any good std::vector<> implementation).

However, you cannot reuse the memory if the required size is larger than the buffer that's already allocated. That is, you would need to include the code to throw away the current allocation and replace it with a larger allocation in either case. Thus the memory reusing code is significantly more complex than the non-reusing code.

As such, unconditionally throwing away the old allocation and replacing it with a new allocation is an application of the KISS principle, and should always be the first implementation. If you find later that this operation is a bottleneck in your code, you can still go back and plug in an optimized implementation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.