zalzon said:
What happens to the pointer then?
I'm confused as to when you free and when not to free. I thought
anytime you create a pointer, you have to remember to free it.
You are confused between the concept of a pointer and the value it
contains.
Every object in C++ exists someplace. Sometimes that is in automatically
allocated storage (as a local variable), sometimes statically allocated (a global
or explicit static), sometimes dynamically allocated (new or malloc), some times
it's part of a larger object.
The pointer you used most likely was a auto variable (local to a function). It
takes care of itself.
The question is "What about the value you placed in it?"
This value is a statically allocated character array. It lives for the life of the program.
There's no need to do anything with it.
Had you allocated it with malloc or new, then you'd have to deal with it. However
the fact that it is in the pointer is MEANINGLESS. The pointer is a piece of scratch
paper that you remember the value on. It's HOW the thing you put into it that matters.
Further, at this point, I recommend you get out of the habit of the incorrect notion that
char* is a string type. It is, as I said before, a pointer to a single character. You'll
avoid lots of stupid allocation mistakes if you use the std::string type for strings. This
is C++ after all. The whole char* fiasco is a weak carry over from C.
-Ron