<vector> code, perfect on vc++6, broken on g++3.2.2

A

Ahmad

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;
}
 
A

Aggro

Ahmad said:
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
 
A

Ahmad

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 said:
Ahmad said:
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
 
L

Leor Zolman

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
(e-mail address removed)
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
 
P

Paul

Ahmad said:
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.
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 <vector>
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
 
J

John Harrison

Ahmad said:
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
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top