1

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];
Patrck122
  • 41
  • 1
  • 4
  • 2
    You never preserved any space in your `wordBank` vector, though accessing `wordBank[0]`. Try that code with `vector wordBank(1);` – user0042 Jul 26 '17 at 00:58
  • 1
    Or use `wordBank.push_back(new char[str.length() + 1]);` instead. – songyuanyao Jul 26 '17 at 01:02
  • 5
    I suppose we can't talk you into a nice `vector` of `string`, can we? – user4581301 Jul 26 '17 at 01:03
  • Why doesn't cin >> str; work for me? "No operator ">>" matches these operands. – Zebrafish Jul 26 '17 at 01:06
  • Why do I have to include for my compiler to recognise std::string::operator>> if I can create a string object just fine, meaning I already have the class definition? – Zebrafish Jul 26 '17 at 01:19
  • 1
    @TitoneMaurice It seems you get the actual implementation in ``, but additional stuff like `operator<<` overloads are in ``. See [here](https://stackoverflow.com/questions/2418841/differences-among-including-xstring-cstring-string-and-wstring-in-c) – Ramon Jul 26 '17 at 01:31

4 Answers4

3

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.

Shiu Chun Ming
  • 227
  • 2
  • 13
1

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(...);
Zakir
  • 2,222
  • 21
  • 31
0

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.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
0

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.

Ramon
  • 1,169
  • 11
  • 25