I have a few questions about strings (char array, not the C++ class), and the way C++ automatically assigns (and deletes!) memory, but first I want to make sure my assumptions are right:
When you declare a variable in a limited scope, it is deleted from memory as soon as the scope ends. So, when you declare a variable in a function, and return the contents of that variable from the function the actual variable is destroyed from memory, and the contents are sent through memory to the other variable it should be stored in.
The questions:
1. But when you declare a string like this:
it would also be destroyed at the end of the scope. However, when you return this, and place the pointer to the string in another variable, would it still be destroyed? And if it's destroyed, would it still be possible to copy the memory following the pointer that is returned to another string pointer?Code:char *cString = "Some ugly 25 char string\0"
2. When you return a constant string, and set a pointer to the start of that string like this:
would that work? Will it work for non-constant strings, and should I delete Hark after using it? If not, how does C++ know it should delete it, and how do I know C++ knows?Code:char *function() { return "Whee! Nifty constant string!"; } int main() { char *Hark; Hark=function(); cout << Hark << "\n"; }
That's quite a few questions, and I'm totally clueless on this subject, so if anyone could help me out here, that'd be really cool! If someone knows of an on-line document about C++'s automatic memory that'd be even better though.