0
#include <iostream>
#include <vector>
#include <string>
#include <cstring>

using namespace std;

int main() {
  //initialising the char array and vector
  char sentence[30] = {};
  vector<string> words{"The", "only", "thing", "to", "fear", "is", "fear", "itself"};

//For loop iterates through the vector and adds the converted strings to the char array
  for(int i = 0; i < words.size(); i++){
//if statements check if there is overflow and breaks the loop if there is
    /*if(strlen(sentence) >= 30){
      cout << "stop" << endl;//comment out later
      break;
    }*/
//strcat method adds the strings from words to sentence one at a time
    strcat(sentence, words[i].c_str());
    /*if(strlen(sentence) >= 30){
      cout << "stop" << endl;//comment out later
      break;
    }*/
//strcat method adds a space in between each word in the cstring
    strcat(sentence, " ");
  }
//couts the full sentence and the length of sentence
  cout << sentence << " " << strlen(sentence) << endl;
  cout << sentence[29] << endl;

  return 0;
}

I commented out my if statements that break if the array goes above 30 elements but now its returning 38. When I try accessing the elements above what the array can hold, it still gives me an error. Shouldn't the compiler throw an error as soon as the number of elements in the array goa above 30? I am pretty new to C++ so I am not sure if this is something with the language itself or something on my end. any help is appreciated.

  • 1
    It is your responsibility (in code) not to write past the end of an array. C++ will not monitor this, as that kind of overhead can make good code run slower. – Drew Dormann Sep 18 '21 at 22:31
  • 1
    C++ doesn't have any kind of bounds checking. Going out of bounds leads to *undefined behavior*. – Some programmer dude Sep 18 '21 at 22:36
  • For more info about undefined behavior, see: https://stackoverflow.com/a/4105123/131930 – Jeremy Friesner Sep 18 '21 at 22:36
  • 1
    Besides that, unless this if ro a school (or similar) assignment or exercise, you should always use `std::string` for all kinds of strings. Which kind of makes the issue moot since it is dynamic and expands as needed. – Some programmer dude Sep 18 '21 at 22:36
  • Writing past the end of the array causes "undefined behavior" which means the compiler can do whatever it wants, typically what it will do is just write over whatever is in memory after the array which may have been unallocated or may have been some data from your program. – jwezorek Sep 18 '21 at 22:37
  • yeah you should not use functions like `strcat` if you are writing C++. – jwezorek Sep 18 '21 at 22:38
  • @jwezorek What else would I use to add the strings to the array? – Charles Simmons Sep 18 '21 at 22:44
  • 1
    use `std::string` for everything. You can concatenate two strings with the addition operator e.g. `std::string str = s1 + s2;` where s1 and s2 are strings works. Just read to docs on the string class. – jwezorek Sep 18 '21 at 22:47

1 Answers1

0

Indexing an array with index that is greater then size ot the array is undefined operation. You'll never know what will be an output. For example in your case, if you try to access char c = sentence[37], this is undefined operation. Means that char c could be whatever is read from memory location of sentence + 37 * sizeof(char) (address of the 37th element). My advice is to use vector, and use at() method when indexing it. Method at() will throw out_of_range exception if you try to access element outside space reserved for that vector.

Dzemo997
  • 328
  • 2
  • 5