Manual construction and destruction

Started by
35 comments, last by Servant of the Lord 11 years, 5 months ago
I think this covers it - any exception should roll back the Resize(), properly deconstructing any elements constructed.

[the entire code]
[just the relevant parts]

Did I make a mistake anywhere?

The code seems less straight-forward than before, but I guess that's what I get when I mess with manual allocation and deallocation. laugh.png
Advertisement
Your moveElements() function doesn't look right. When an exception occurs in the constructors and destroying elements, only the last row show go up to lastColumn, all the previous rows should go to columns. The way you've coded it looks like it misses destroying elements at the end of previous rows.
Great catch - fixed.
Also if reallocateMemory() is called with an empty newCapacity, it frees memory without destroying objects first.
Excellent, fixed that, and simplified the for-loops.
[latest version] ([size=2]If anyone wants to use the code, for whatever reason, it's public domain - slap your own name on it and use it in your code)

Thank you for all the help, everyone!
Any other mistakes in it I'll find through practical usage in my code.

Here's some example usage code.
If that's your latest code then you also seem to have a rule of three violation: I don't see either a copy constructor or an assignment operator. And since you're using C++11's rvalue references you might also want to add a move constructor and move assignment operator. A nothrow swap function might also be a good idea.
Thanks, I usually don't manually handle memory, so I often forget the rule of three.

Here's how I'm implementing it:
[relevant parts] [entire code]

Doing a few tests, I counted the number of destructions, constructions, moves, and copies, and the numbers balance out.

This topic is closed to new replies.

Advertisement