Using strings in C++

Started by
11 comments, last by Foopy 24 years, 5 months ago
While I'm not denying the different flavours of solution to this problem there is no question of a pointer being destructed automatically. The only data a pointer contains is the address of whatever it's pointing at. Pointers don't have destructors and whatever the pointer points to has a destructor which is only called explicitly by using delete.
Only the pointer itself is in local scope, the actual data is not a local variable and hence does not go out of scope with the end of the function. Passing back the pointer value is still perfectly valid because the address is passed back before the pointer can go out of scope.
Like I said, this may not be your preferred method, but it is valid.

Btw...just to look stupid...what is a _smart_ pointer?

Advertisement
Ok, here's a simple question.

I really like the convenience of being able to use strings as "pseudo-primitive types" in high-level languages. For instance, in Java you can write

String bob = "hello " + myObject.toString() + "!";

and it's as simple as that; garbage collection will be performed on the String that myObject.toString() returns, so no memory ends up getting lost. But in C++, the string that is returned by a toString() sort of method would return a String that needs to be deallocated by the use of a free() or delete operator (to free the dynamically-created character array in the String). Thus the above command in C++ (if we made a String class and overloaded the proper operators, etc.) would result in memory loss, since myObject.toString() would return a string that never gets deallocated.

The only thing I can think of to fix this is garbage collection, which wouldn't be very hard to implement, but it would require that the programmer run the garbage collection function occasionally, to free Strings that have a reference count of zero (such as the String made by myObject.toString() in the above example). But I don't know if this is the best way to do things.

What do you use when you need to do work with strings? Just plain old character arrays, like in C, or do you make your own String wrapper, or something else?

First off, a smart pointer is just a pointer encapulated in a class that will allocate and deallocate the pointer in its constructor and destructor functions.

However, I think newing a new String class in this case is not that best way to do it. First off, any good String class needs to have a good copy constructor and operator=. You DON'T want the compiler to copy the class bitwise because when one of the classes is destroyed, so is the only copy of the data.

With a working copy constructor, the original code works perfectly (except in dual heap-space situations, but I won't get into that) and is great. When the function returns, it has a temporary scoped String class containing its own copy of the data. It is destroyed on its own and no one needs to worry about deleteing anything.

Just my $.02

- Splat

This topic is closed to new replies.

Advertisement