If by nothing you mean that it doesn't clone the string, then yes. It will just push the pointer into the vector.
Use strdup, or new + strdup to clone the string.
Main Topics
Browse All TopicsI want to insert a char * into a std::vector<char *>. I use vec.push_back(charPtr) but it does nothing. Does anyone know how to do this ?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi francis77
If u can show us the code where std::vector<char*> isn't working well, then we can tell u where is the fault.
FYI: As per evilrix said, u need the manage the memory the char * points to.
This vector is only storing the addresses of string but not the actual string. So if u don't have valid string present as the time when u are iterating vector, then how will u get those string. U r having the addresses of the string inside vector but at those address if there is no valid string, then it's the situation of 'dangling pointer'.
To add to above comments:
It normally is poor design to storing pointers in a container with copy semantics like std::vector. If you would clear the container, the memory the pointers were pointing to never was freed (worse if you have stored the pointers into a second location) or you have to free each single pointer item before clearing.
There are only two valid exceptions to that, function pointers and baseclass pointers for virtual use. None of them applies here. The best is you follow the advice of evilrix and use std::string for storing text, what is the C++ way.
I'll support the objection given by Subrat2009 though I don't share the argumentation.
In C++ a std::string always is a valid alternative to a char pointer whether it is explicitly asked for or not. But surely the question was answered in the thread, so there is no reason for deleting the question or taking the Autor's comment as an answer (if that was requested by the asker).
>> I'll support the objection given by Subrat2009 though I don't share the argumentation.
Sometimes the asker doesn't know there is a better/safer way. Storing a char* in a vector is nearly always a road to disaster. Firstly, there is the lifetime of the memory the char * points to. Who owns it? Who deletes it? Also, making code exception safe with STL containers containing heap allocated memory that is not managed using a reference counted smart pointer (of which there are none in standard C++ suitable for use in an STL container) it messy.
The 'safe' (and most C++ correct) way to push a char * into a vector is shown here as the very first answer (by encapsulating it in a string).
http:#25495458
Other than that, it seems the asker never explained why they had issues with this (my very first posted asked for clarification).
The vector was in a class but was not initialized correctly, when I take it out of the class it is. If I initialize in the constructor, it works but when another function within the class tries to add to the vector, it throws an exception. This is where the problem resides. I have included some class definitions I have made. It is called like :
Database<MySQL> d;
d = new Database<MySQL>();
Are the variables modified in the MySQL class retained in the d variable ?
>>>> The 'safe' (and most C++ correct) way to push a char * into a vector is shown here as the very first answer (by encapsulating it in a string).
That statement is not true. By encapsulating a char * into a string the pointer itself wasn't stored but only the text the pointer was pointing to.
So, the first answer is only the solution if the purpose was to store the text and not the pointer.
A later comment of the Asker "I was able to add a char * and a string to the vector" seems to indicate that both the answers of evilrix and SubRat2009 were valid solutions for the asker.
>>>> The vector was in a class but was not initialized correctly
That is not possible.
If you create an instance of MYSQL class for all class members - including the 'row' member - the constructor was called which would create an empty container. As the member was private and there was no public interface which exports the row vector it only could be filled by (non-static) member functions of class MYSQL. An initialization is not necessary in no case.
>>>> MYSQL *getConnectionHandlePtr() { return _connectionHandlePtr; };
That is a very dangerous construct. There are only a few valid class designs which would have a pointer to another instance of the class itself, e. g. nodes of a tree container. IMO, you have too much pointer members and therefore the crashes. For example, why should anyone want to store a Log* in a database instance? If you have one logfile for all database instances you don't need the pointer but should have a singleton instance of the Log which could be accessed without pointer. Or, if each MYSQL instance has its own Log, it could have a Log member and not a pointer.
Business Accounts
Answer for Membership
by: evilrixPosted on 2009-10-05 at 07:20:11ID: 25495458
>> I use vec.push_back(charPtr) but it does nothing
eference/s tring/stri ng/
What do you mean it does nothing?
Incidentally, you are better off using a string otherwise you have to manage the memory the char * points to.
http://www.cplusplus.com/r
string s = "hello world";
vector<string> v;
v.push_back(s);