Link to home
Start Free TrialLog in
Avatar of skundu
skundu

asked on

STL (URGENT Please) question...

Hi...
This is a easy question about Standard Template Library(STL). I have used "vector" class of type "char *" in my program. I have appended below my complete program (run in g++ compliler). Here v1 is a vector of type "char *". I have pushed (using the method push_back) in v1 a variable "vtop" (of type "char *"). Initially the variable "vtop" contains "EECS171". Now, when I print th econtent of the vector v1, I get that its only content is "EECS171". That is alright...
But now I assign (after I pushed_back to the vector v1) a new string of char * to the variable vtop. Say the new char string is "EECS370". Now when I print the content of the vector v1, surprisingly I find that its only content is "EECS370" (NOT "EECS171" !!!). I wonder who the vector v1 gets changed when I only modified the content of the variable "vtop"...
Here is the program...
If you have any idea of why this is happening, pls let me know....
Thanks a lot...
sunanda
My complete program:                                                                      
#include<iostream.h>
#include<string.h>                      #include<vector>                                
using namespace std;                                                            
                                                                                 
void printVector(const vector<char *> &vec);                                    
main()                                                                          
{                                                                                
vector<char *> v1; //Stores POSTFIX string                                      
                                                                                 
char *vtop;
vtop = new char [20];
strcpy (vtop, "EECS171");                                              
v1.push_back(vtop);                                                              
strcpy(vtop, "EECS370");  //This above line surprisingly changes the vector v1 !!!!!                                        
                                                                     
cout<<"v1 is: ";                                                    
printVector(v1);                                                    
                                                                     
}                                                                    
                                                                     
void printVector(const vector<char *> &vec)                          
{                                                                    
vector<char *>::const_iterator p1;                                  
for (p1=vec.begin(); p1 != vec.end(); ++p1)                          
cout<<*p1<<' ';                                                      
cout<<endl;                                                          
}                                                                    
                                                                                                                                               
Avatar of yowkee
yowkee

There is no surprising. You store the char* into a vector, then what the container contain is a pointer which point to an char array.

eg, vtop (0x400000a0) --> "EECS171"

   store vtop to v1, so
   v[0] = 0x400000a0

   modify, vtop -->  "EECS370"

   now, 0x400000a0 point to "EECS370",
so it *v[0] will print "EECS370".

For a better choice and less confusion, use vector<string> instead of vector<char *> .
yowkee's explanation is a little hard to undetstand (for me at least), but it is correct.  You are storing a pointer to a string, not the actuall string.  If the string stored in that location changes, then the pointer to the string points to that changed string.

His solution is 1 of many posibilities, but is probably the best one.  In fact you should usually avoid using the C-style "char *" strings wherever possible.  Your should probably switch to the STL string class or another string class for storing all strings.  This will make your program safer, more efficient and easier to write and maintain.

yowkee, it seems to me that you have answered the question.  You should post an official answer.
ASKER CERTIFIED SOLUTION
Avatar of yowkee
yowkee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> is there a feature to post previous comment as answer
No, but it doesn't matter.  The whole question history is saved in the database.
Thanks :)
Avatar of skundu

ASKER

Hi...
Thank you for your answers... I thought that the char * is stored in the vector's own memory space, actually that is not...
So I should try with string class...
Thanks again...
sunanda
I hate to confuse you...but hopefully this will straighten you out.

The "char *" is stored in the vector.  That "char *" is not a string, although programmers somethimes call it a string.  It is a pointer to a string.   So the vector<> does not store the string, it stores a pointer to a string that is stored elsewhere.  The problem with that is that the "elsewhere" might be a place that is volatile.  i.e. that elsewhere place may change its contents.  i.e. change the string it stores.  the char * pointer wil continue to point to this place, but now this place might store a new string.  For example.

char Str[] = "abcde"; // This really is a string.
char *StrPtr = Str; // This is just a pointer to the string elsewhere.

strcpy(str,"12345");  // This changes Str, but it also then appearss to change StrPtr, because StrPtr points to the string stored in Str.
Avatar of skundu

ASKER

Hi Nietod,

Thank you very much for your above explanation. I greatly appreciate it. It is now clear to me where I made mistake.......
I hope to get rid of my problem by using string class object,
instead of using char*....
Thanks a lot...
sunanda