Skip to main content
GameDev.net gamedev.net
🔒 Locked

std string problem

Started by codehunter13 Nov 30, 2005 at 10:50 AM 8 replies 1.6k views
Original Post
codehunter13
codehunter13
Hi all, I use the following function:

void test(std::string& str)
{
    str = "aa"
}

The assignement works as long as str is empty. When str is not empty then is str ??? after the assignement. thx in advance
TyroWorks
TyroWorks
Unless I'm missing something, str, and whatever string is being passed, would become "aa" because the string is being passed by reference (std::string& str).
codehunter13
codehunter13
yes that work as long as the string is empty when calling this function.If str is not empty when calling the function and i do the assignement, str = ???.
TyroWorks
TyroWorks
It should overwrite whatever is in the memory address of the string being passed to the function. For example:

void test(std::string& str)
{
str = "aa";
}
std:string name = "bb";
test(name);

The name string would contain the value "aa" due to the fact that it's memory address (passed by reference - &) was used for the str string variable. By modifying the contents of str, the contents of whatever string is passed to the function will be modified at the same time.
Moomin
Moomin
Firstly, the line 'str = "aa"' should be
str = "aa";
Apart from that it works fine for me. 'str' should equal to "aa" even if it originally had been assigned a value.
Drew_Benton
Drew_Benton
Quote:
Original post by codehunter13
yes that work as long as the string is empty when calling this function.If str is not empty when calling the function and i do the assignement, str = ???.


Wait, are you talking about in the deugger when you are debugging? If that's the same, that seems normal, because the variable went out of scope after the assigment if you set a breakpoint on that line. To get around that with Visual Studio:
void test(std::string& str){    str = "aa";    str = str; // <--- Breakpoint here to see the current contents}
DarthBill
DarthBill
If you happen to be using Visual Studio.NET or .NET 2003, there is a problem in the debugger where it will not always display the actual contents of a string. Try writing your string to a file or to the screen - it may be that your code is working fine but the debugger is not.
codehunter13
codehunter13
no it's not due to the debugger because i can see it works when str is empty
DarthBill
DarthBill
That's fine, but I had a similar problem where the debugger worked fine for a little while but would die at seemingly random times.
Anon Mike
Anon Mike
Quote:
Original post by codehunter13
no it's not due to the debugger because i can see it works when str is empty

That's not necessarily relevant. In MSVC at least std::string can store the string in a couple different ways. Some versions of the debugger can't properly display strings if they're stored in some ways.

Try doing OutputDebugString(str.c_str()) and see what you get that way.
-Mike

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.