 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ahmad Guest
|
Posted: Fri Mar 05, 2004 11:15 pm Post subject: <vector> code, perfect on vc++6, broken on g++3.2.2 |
|
|
Hi all,
I have written a simple c++ app, as I'm still learning c++. The
thing works flawlessly on VC++6, but just doesn't work on g++. The
transliterate function which causes the problem is supposed to search
for some characters (2 or 1) and replace them with their equivalent
Arabic characters from an array vector<string>. I have been debugging
it on Linux for like 6 hours, and it's really boring that it works on
vc++.
Anyway, tons of thank yous to anyone that can help me with this one:
PS: I noticed:
arabic.size() returns 0??
if I replace it with a number like 6, it just crashes?!
declarations
------------
std::vector<std::string> arabic; //Array for arabic letters
std::vector<std::string> english; //Array for english letters
std::vector<std::string> comments; //Array for comments (ignored)
code
--------
std::string arabize::transliterate(std::string user_ip){
std::cout << "In arabize Transliterator" << std::endl;
std::string CharOrTwo;
std::string text_out; //Holds the Arabic processed text
bool MatchFound=false;
if (user_ip.length() < 1) { std::cout << "Write some text first!" <<
std::endl; return "";}
for(int i=0; i < user_ip.length() ; ++i){
//Loops over all input characters
if (i < (user_ip.length()-1)){ //If not at last char
CharOrTwo = user_ip.at(i);
CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
MatchFound=true;
text_out += arabic[ctr]; //Put their Arabic equivalent
}
if (MatchFound) {MatchFound=false; i++;continue;}//i++ bec we
worked on 2 chars
}
//Next 2 chars didn't match, or we're at last char.
//So, let's match this char we are at
CharOrTwo = user_ip.at(i);
for(int ctr = 0; ctr < arabic.size(); ctr++) //Again is it in
dictionary
if (CharOrTwo == english[ctr] ){
MatchFound=true;
text_out += arabic[ctr]; //Put their Arabic equivalent
}
if (MatchFound) {MatchFound=false;continue;}
}//Done iterating over all input chars
#ifdef _DEBUG
std::cout << "Input:" << user_ip << std::endl <<
"Output:" << text_out << std::endl;
#endif
return text_out;
}
|
|
| Back to top |
|
 |
Aggro Guest
|
Posted: Fri Mar 05, 2004 11:34 pm Post subject: Re: <vector> code, perfect on vc++6, broken on g++3.2.2 |
|
|
Ahmad wrote:
| Quote: | I have written a simple c++ app, as I'm still learning c++. The
thing works flawlessly on VC++6, but just doesn't work on g++. The
transliterate function which causes the problem is supposed to search
for some characters (2 or 1) and replace them with their equivalent
Arabic characters from an array vector<string>. I have been debugging
it on Linux for like 6 hours, and it's really boring that it works on
vc++.
|
Please follow the guidelines from the faq:
[5.8] How do I post a question about code that doesn't work correctly?
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
|
|
| Back to top |
|
 |
Ahmad Guest
|
Posted: Sat Mar 06, 2004 3:58 pm Post subject: Re: <vector> code, perfect on vc++6, broken on g++3.2.2 |
|
|
OK, sorry if I was not very clear. Anyway, after playing with gdb, I
think the problem is clearer (though still strange)
CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
After the for loop, ctr has strange values of 100,000+ though it
should be from 0 to 5??
any help
Aggro
| Quote: | Ahmad wrote:
I have written a simple c++ app, as I'm still learning c++. The
thing works flawlessly on VC++6, but just doesn't work on g++. The
transliterate function which causes the problem is supposed to search
for some characters (2 or 1) and replace them with their equivalent
Arabic characters from an array vector<string>. I have been debugging
it on Linux for like 6 hours, and it's really boring that it works on
vc++.
Please follow the guidelines from the faq:
[5.8] How do I post a question about code that doesn't work correctly?
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
|
|
|
| Back to top |
|
 |
Leor Zolman Guest
|
Posted: Sat Mar 06, 2004 4:10 pm Post subject: Re: <vector> code, perfect on vc++6, broken on g++3.2.2 |
|
|
On 6 Mar 2004 07:58:06 -0800, [email]eng_ak (AT) link (DOT) net[/email] (Ahmad) wrote:
| Quote: | OK, sorry if I was not very clear. Anyway, after playing with gdb, I
think the problem is clearer (though still strange)
CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
After the for loop, ctr has strange values of 100,000+ though it
should be from 0 to 5??
any help
|
You haven't posted all your code, so I can't tell for sure, but I suspect
you may be getting bitten by the "scope of a variable defined in a for
loop" issue.
In VC6, saying something like:
for (int i = 0; i < whatever; ++i)
{
// whatever
}
is the same as saying:
int i;
for (i = 0; i < whatever; ++i)
{ // whatever
}
IOW, i remains in scope after the end of the loop. gcc probably does it
/right/, which is to say, the first version above is "as if" you wrote:
{
int i;
for (i = 0; i < whatever; ++i)
{ // whatever
}
}
So perhaps you've got a version of your loop variable, "ctr", visible at
the scope outside of the for loop? A global perchance? Just grasping at
straws here...
-leor
Leor Zolman
BD Software
[email]leor (AT) bdsoft (DOT) com[/email]
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
|
|
| Back to top |
|
 |
Paul Guest
|
Posted: Sat Mar 06, 2004 4:18 pm Post subject: Re: <vector> code, perfect on vc++6, broken on g++3.2.2 |
|
|
"Ahmad" <eng_ak (AT) link (DOT) net> wrote
| Quote: | OK, sorry if I was not very clear. Anyway, after playing with gdb, I
think the problem is clearer (though still strange)
CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
After the for loop, ctr has strange values of 100,000+ though it
should be from 0 to 5??
any help
|
I guess you went ahead and ignored the posting guidelines. Please post a
*complete*, *compilable*, example that demonstrates the problem.
| Quote: | have been debugging
it on Linux for like 6 hours, and it's really boring that it
works on vc++.
|
This really doesn't mean anything, that it "works on vc++". There is a
whole bunch of invalid code that I can come up that "works on vc++", but
doesn't take away from the fact that the code could exhibit undefined
behavior when compiled with another compiler.
I see nowhere in the code where you call push_back(), resize(), or construct
the vector objects that would appropriately size them.
#include
int main()
{
std::vector<int> IV;
IV[0] = 10; // Illegal access
std::vector<int> IV2;
IV2.push_back(10); //OK
}
IV has no element 0, since no room for element 0 was created, so you have an
illegal memory access.
You are just lucky that it worked in Visual C++ *if* the code you posted is
all you are doing with the vector. Also, how do you know it is a problem
with vector, and not something else you are doing in the program that may
have corrupted memory?
This is why you should post *minimal*, *compilable* code that duplicates the
problem.
Paul
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sat Mar 06, 2004 4:39 pm Post subject: Re: <vector> code, perfect on vc++6, broken on g++3.2.2 |
|
|
"Ahmad" <eng_ak (AT) link (DOT) net> wrote
| Quote: | OK, sorry if I was not very clear. Anyway, after playing with gdb, I
think the problem is clearer (though still strange)
CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
After the for loop, ctr has strange values of 100,000+ though it
should be from 0 to 5??
any help
|
No still not very clear, please follow the guidelines in the FAQ
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
You will get fast accurate help if you follow these guidelines, if you don't
you will get guesses at best.
john
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|