Dangling pointer?

I think I have a dangling pointer here, causing SIGSEGV Segmentation Fault crashes (usually appears when the same variable is removed from hash twice because it has two or more pointers going into the same thing).

Does anybody have a better way of assigning elements to a vector?
(This is Letter(int X,int Y,const char* Content) from the Letter class)
(I make multiple temporary instances of the class that are removed right after.)

1
2
3
4
5
6
7
8
9
10
11
  std::string buffer = Content;
  size_t sentence_start = 0;
  size_t sentence_end = buffer.find_first_of("?!.",sentence_start);
  while(buffer[sentence_end+1] == buffer[sentence_end]) sentence_end++;

  while(sentence_end != std::string::npos)
  {
      lines.push_back(buffer.substr(sentence_start, sentence_end+1));
      sentence_start = buffer.find_first_not_of(" \t\n", sentence_end+1);
      sentence_end = buffer.find_first_of("?!.",sentence_start);
  }


Is it possible to use strcpy() to assign the vector lines elements?
If not, do you have any other solution to avoid dangling pointers?
You don't use pointers. You do use indices. (std::string has members operator[] and at(). The second throws if you do use bad index. You do use the first. It does not throw, so crash is uncontrollable.)

Line 4. What prevents out of range error from occurring?
Topic archived. No new replies allowed.