String variable appears to become corrupted after assignment

C

CarlWSummers

I'm doing something wrong. I'm parsing a string into rational numbers
(int/int) by looking for spaces in the string, assinging a substring of
the original string to a temporary variable, and trimming that
substring off the original string, perhaps this is more clear in code.
In any event, what I have works fine for strings like "23 1 23452 34"
and even "1/2 2/3 3/4 4/5" but if the string looks like "1/2 -2/3 3/4
-4/5" the original string becomes corrupted after the trimming.

I'm using VC 2003, but I'm rather sure this is my fault, and not the
compiler's.

#include <string>
using namespace std;

int main()
{
string line = "1/2 -2/3 3/4 -4/5";
string temp;
int index = line.find(' ',index);
temp = line.substr(0,index);
unsigned int size = temp.size();
string debug = line.substr(size);
line.assign(debug);
//line = debug;
}

After either line.assign(debug) or line = debug, line becomes
"ji¼è[Aã" or "Z".
If I put a watch on line it appears that there's a _Bx object with two
variables _Buf and _Ptr. _Ptr contains (...or points?) to the right
string, but _Buf contains garbage (different than above). I thought
that using an = with an object type resulted in a direct copy of the
object (versus, say, Java where the left side is just made to point to
the same object), but surely the assign function would make a copy even
if the = didn't.
The steps are a bit expanded, and I've been messing with it for a while
so it's not terribly clean. In any event, I'm a bit new to C++ and I
don't see what it is I'm doing wrong.

Thank you for your time,
Carl Summers
 
S

Sunil Varma

I'm doing something wrong. I'm parsing a string into rational numbers
(int/int) by looking for spaces in the string, assinging a substring of
the original string to a temporary variable, and trimming that
substring off the original string, perhaps this is more clear in code.
In any event, what I have works fine for strings like "23 1 23452 34"
and even "1/2 2/3 3/4 4/5" but if the string looks like "1/2 -2/3 3/4
-4/5" the original string becomes corrupted after the trimming.

I'm using VC 2003, but I'm rather sure this is my fault, and not the
compiler's.

#include <string>
using namespace std;

int main()
{
string line = "1/2 -2/3 3/4 -4/5";
string temp;
int index = line.find(' ',index);

In the above line index is not initialized.
It can be some garbage value.
So try with line.find(' ') or line.find(' ',0)
You will get the correct value.
temp = line.substr(0,index);
unsigned int size = temp.size();
string debug = line.substr(size);
line.assign(debug);
//line = debug;
}

After either line.assign(debug) or line = debug, line becomes
"ji¼è[Aã" or "Z".
If I put a watch on line it appears that there's a _Bx object with two
variables _Buf and _Ptr. _Ptr contains (...or points?) to the right
string, but _Buf contains garbage (different than above). I thought
that using an = with an object type resulted in a direct copy of the
object (versus, say, Java where the left side is just made to point to
the same object), but surely the assign function would make a copy even
if the = didn't.
The steps are a bit expanded, and I've been messing with it for a while
so it's not terribly clean. In any event, I'm a bit new to C++ and I
don't see what it is I'm doing wrong.

Thank you for your time,
Carl Summers

Regards
Sunil Varma
 
C

CarlWSummers

I used old code in the first post, doh! Indeed, as written above it
won't get past that line. However, I still get garbage in line after
either assignment statement after making your suggested change.
temp contains the correct value.

Here's the code I'm using now.

int main()
{
string line = "1/2 -2/3 3/4 -4/5";
string temp;
int index = line.find(' '); //Index = 3, as expected
temp = line.substr(0,index); //temp = "1/2", as expected
unsigned int size = temp.size(); //sixe = 3, as expected
string debug = line.substr(size); //debug = " -2/3 3/4 -4/5" as
expected
line = ""; //After this, line = "ji¼è[Aã", not expected
//line.clear(); //No change in line
line.assign(debug); //No change in line
line = debug; //no change in line
}

While writing this I tried adding a cout << line; at the end there,
it's displayed properly on the screen so perhaps this is a problem with
the VS IDE? In any event it's still not operating correctly in my main
program, but perhaps there's an unrelated problem.

Thanks again for your time!
Carl Summers
 
S

Sunil Varma

I used old code in the first post, doh! Indeed, as written above it
won't get past that line. However, I still get garbage in line after
either assignment statement after making your suggested change.
temp contains the correct value.

Here's the code I'm using now.

int main()
{
string line = "1/2 -2/3 3/4 -4/5";
string temp;
int index = line.find(' '); //Index = 3, as expected
temp = line.substr(0,index); //temp = "1/2", as expected
unsigned int size = temp.size(); //sixe = 3, as expected
string debug = line.substr(size); //debug = " -2/3 3/4 -4/5" as
expected
line = ""; //After this, line = "ji¼è[Aã", not expected
//line.clear(); //No change in line
line.assign(debug); //No change in line
line = debug; //no change in line
}

While writing this I tried adding a cout << line; at the end there,
it's displayed properly on the screen so perhaps this is a problem with
the VS IDE? In any event it's still not operating correctly in my main
program, but perhaps there's an unrelated problem.

Thanks again for your time!
Carl Summers

I find the above code working fine in VS 2005 and gcc 3.2.2
line value is set to "" after the statement
line = "";
in the above compilers.


Regards
Sunil Varma
 
R

Ravi Nakidi

Hi Carl Summers,


U are doing small mistake. I hope U already find this. Anyway I'm
showing the mistake where u did.

in line 3 int index = line.find(' ', index);

what is the value of second index which is inside the find() function.
make it that holds some value. like this.

#include<iostream>
#include <string>
using namespace std;


int main()
{
string line = "1/2 -2/3 3/4 -4/5";
string temp;
int index = 0;
index = line.find(' ',index);
/*or

int index = line.find(' ',0);
*/
temp = line.substr(0,index);
unsigned int size = temp.size();
string debug = line.substr(size);
line.assign(debug);

cout<<line;
//line = debug;
//cout<<line;


}
 
P

Paul

< However, I still get garbage in line after either assignment statement
after making your suggested change.

Don't use the debugger to view or verify your strings.

A debugger is not guaranteed to show you the internals of the string class
as you would like to see it. A classic case is exactly your compiler,
Visual Studio 2003.

The std::string class is written where the data is not "visible" with the
compiler, unless you dig deep down into the string class. Some
implementations use various optimizations to the string class so that the
actual data is not readily available to the debugger, and you get what you
believe is garbage when you attempt to view the results in the debugger.

Instead, verify the results the old-fashioned way: either use trace
statements, log files, or the tried and true "cout".

- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top