
On Tue, Dec 04, 2007 at 04:59:17AM -0500, Hervé Brönnimann wrote:
Inspection of the g++ string implementation (latest 4.1.x) shows that resize() will *almost* always create a new rep and thus a new data() for the string, if it is shared. The only exception is when the requested size == s.size(). Chuck, was this the bug that hit you?
To be honest, isn't a specific implementation not completely unimportant? You don't want to rely on implementation details, right? There is only one solution: remove the buggy casting code and replace it with a proper one as suggested. Whether the proper one is slower or not is unimportant!
Seems to me, since you are going to rewrite your string anyway, you might as well clear() it first, then resize(size). That'll take care of creating a new representation. The problem with that is that it will write over the string twice, first filling it with 0s then with the read data. But think about it: if the string is shared, you need to construct a new representation with the requested size anyway, and it will have to be filled with 0s.
No, the problem is that this is implementation specific. Don't rely on such stuff, rely on the C++ standard! Jens