Patrck122
Patrck122

Reputation: 41

storing a c++ string into a char* vector

I'm trying to store this string into a vector of pointers. This is just the starting point of the code. Eventually the vector will store words the user types in and either add it to the vector if the vector doesn't have it or show the word is already in the vector.

I tried to use strcpy() but then it told me to use strcpy_s(). So I did now and now it just crashes every time with no error. Can someone give me some insight into what is going on here.

vector<char*> wordBank;

string str;
cin >> str;

wordBank[0] = new char[str.length() + 1];
strcpy_s(wordBank[0], str.length() + 1 , str.c_str() );

cout << wordBank[0];

delete[] wordBank[0];

Upvotes: 1

Views: 2895

Answers (4)

Shiu Chun Ming
Shiu Chun Ming

Reputation: 227

I would not consider vector<char*> wordBank; this c++ code, but rather C code that happens to use some C++ features.

The standard library in C++ can make your life easier. You should use std::vector<std::string> instead.

vector<string> wordBank;
wordBank.push_back(str);

It avoid all the pointer stuff, so you need not to do memory management (which is a good reason get rid of the pointers).

For strcpy_s, it's a safer version of strcpy, because you have to explicitly specify the size of the target buffer, which can avoid buffer overflows during copies.

However, strcpy_s is non-standard and MS specific, NEVER use strcpy_s unless your just want your code compiled on MSVS. Use std::copy instead.

Upvotes: 3

Ramon
Ramon

Reputation: 1299

vector<char*> wordBank; constructs an empty vector. As such, wordBank[0], which uses operator[], has undefined behavior since you're accessing out of bounds.

Returns a reference to the element at specified location pos. No bounds checking is performed.

You can use push_back to add a new element, as such:

wordBank.push_back(new char[str.length + 1]);

Of course the most sensible thing is to just use use a vector of strings

vector<string> wordBank;
wordBank.push_back(str);

You're trying to manually manage memory for your strings, while the std::string class was designed to do that for you.

Also, from what you are describing your use case to be, you may wish to check out std::map and/or std::set. Here's a tutorial.

Upvotes: 0

Nikos C.
Nikos C.

Reputation: 51840

You don't need char* elements in the vector. You can use string instead, and append your strings to the vector with push_back(), which will allocate the needed space:

vector<string> wordBank;
string str;

cin >> str;
wordBank.push_back(str);
cout << wordBank[0];

This will free you from the burden of having to use delete every time you want to remove a string from the vector. Basically, you should never have to use delete on anything, and to accomplish that, you should avoid allocating memory with new. In this case, that means avoiding the use of new char[/*...*/], which in turn means you should use string to store strings.

Upvotes: 0

Zakir
Zakir

Reputation: 2382

The default size of a vector is 0

Hence this line

vector<char*> wordBank;

just defines a 0 sized vector of character pointers

As people have mentioned in comments you can go with either of these 2 options:-

  vector<char*> wordBank(1);

OR

wordBank.push_back(...);

Upvotes: 1

Related Questions