-
October 12th, 2003, 04:43 AM
#16
thanks everyone
but one (and most importent thing) I don't understand
should I deallocate, and if so where?
I use by reference (see the function declaration)
#inclue <string>
void BuildString(string &str
{
//string manipulation here
}
int main()
{
string myStr;
BuildString(MyString);
}
thanks
-
October 12th, 2003, 05:24 AM
#17
An STL string manages itself so you don't need to do anything. When the main() function returns, it calls the destructors of any variables allocated on the stack. The string will be deleted for you. This happens any time a variable loses "scope" ... see below:
[code]#include <string>
int main(int argc, char *argv[])
{
int x = 0;
char text[100];
// introduce new scope
{
// new scope
std::string myString;
myString = "some text";
}
// poof! myString is gone.
}
-rick
-
October 12th, 2003, 12:46 PM
#18
Originally posted by fransn
Vicondin:"The code was:
code:--------------------------------------------------------------------------------
string myStr;
BuildString(MyString);
"
Yes but that's not the code I'm talking about. See the & there in the function declaration?
void BuildString(string &str)
{
}
Fransn: The original question was
where does myStr is deallocated
You are talking about the wrong code, then, since the code you are referencing with the following statement:
See the & there in the function declaration
has nothing to do with the life of myStr.
"str", which is of type "reference to string" goes out of scope when BuildString returns - it's scope is the duration of BuildString. That is not to say that the destructor for the object that str is a reference to is called at this point.
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
October 18th, 2003, 10:51 AM
#19
Dear Vicodin.
please run this program and send me the money if you still have doubts.
#include <string>
typedef std::string string;
void ChangeString(string & strData2)
{
strData2 = "This ain't worth my time";
}
int main(int argc, char* argv[])
{
string strData = " hoyhoy";
ChangeString(strData);
printf("%s\r\n",strData.c_str());
if(stricmp(strData.c_str(),"This ain't worth my time") == 0)
printf("vicodin owes fransn $50\r\n");
else
printf("fransn owes vicodin $50\r\n");
return 0;
}
-
October 19th, 2003, 12:45 AM
#20
Fransn:
I don't know what you don't get about the OP's question, and I fail to see the purpose of your little program there.
The OP posted (in a nutshell):
well I can use std:string so the code will be:
Code:
#inclue <string>
void BuildString(string &str)
{
}
int main()
{
string myStr;
BuildString(MyString);
}
my question where does myStr is deallocated?
1. myStr is constructed within main, because that is where it is declared.
2. One can only refer to the variable myStr, which is of type std::string, from within main.
3. Even though a reference to myStr is passed into BuildString, the string itself is not referred to as myStr in that function. It is called "str".
4. One can only refer to the variable str, which is of type "reference to std::string" from within BuildString.
5. myStr goes out of scope when the function main exits. The memory associated with the variable is released.
This (#5) answers the OP's question - "where does myStr is deallocated?".
Your answer:
no. It is passed in by reference and will get changed by reference and never leave scope
besides being ambiguous, is partially incorrect. Your statement "it is passed in by reference and will get changed by reference" is correct, but has nothing to do with the OP's question. Your statement "and never leave scope" is incorrect because everything, in fact, has a scope.
I can't believe I'm arguing such basics...
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
October 20th, 2003, 03:32 PM
#21
???
????!!!???
Have I said that sizeof(Teste) is 6?
???
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|