473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

String variable appears to become corrupted after assignment

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

Mar 17 '06 #1
5 2887

Ca**********@gmail.com wrote:
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

Mar 17 '06 #2
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

Mar 17 '06 #3

Ca**********@gmail.com wrote:
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

Mar 17 '06 #4
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;
}

Mar 17 '06 #5

<Ca**********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
< 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
Mar 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kamilche | last post by:
I was looking for a way to speed up detecting invalid characters in my TCP string, and thought of yet another use for the translate function! If you were to 'translate out' the bad characters, and...
11
by: Lord Khaos | last post by:
If I am trying to find an expression, foo, I can do something like this: rExp = /foo/gi; if(results.search(rExp) > -1){ and all work fine. however, if I want my search term to be a...
9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
12
by: Robert Mens | last post by:
Hi, how is it possible to check if an \n appears in a string to process the data before the \n and then stack up the following data until another \n appears? I need this for something with...
10
by: slurper | last post by:
if i do this char *mystring="mystring"; later i reassign to mystring like this mystring="replacewithnewstring"; i'm not a c expert, but i suppose i need to get rid of the dynamically...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.