Dangling pointer?

#Moved to General C++ Programming
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?
Last edited on
I don't see any pointers, dangling or otherwise, in this code.

(This is Letter(int X,int Y,const char* Content) from the Letter class)
Sorry, I don't know what that refers to.

I'm assuming lines is std::vector<std::string> lines;

What I do see is a misuse of substr().
The second parameter (which is optional) is the length of the substring.

substr(start, end)
would be correctly written as something like:
substr(start, end - start + 1)

See the reference page: http://www.cplusplus.com/reference/string/string/substr/

Is it possible to use strcpy() to assign the vector lines elements?

No. Or at least it's a horrible mixture of C and C++. Either on its own is fine, but mingle them and something will get corrupted or go badly wrong. I think you're looking at the wrong solution to the wrong problem. Either that or you didn't give enough information.


Also I should add, this line worried me:
while(buffer[sentence_end+1] == buffer[sentence_end]) sentence_end++;
there's potential to access an out-of-range element here.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 std::string buffer = Content;
  size_t sentence_start = 0;
  size_t sentence_end = buffer.find_first_of("?!.",sentence_start);

  // what if here, sentence_end == std::string::npos
  // what if below, increments result in sentence_end >= buffer.size()
  while(buffer[sentence_end+1] == buffer[sentence_end]) sentence_end++;

  // be aware that the second argument to substr is not a position, but a count
  // substr(pos, count) returns string [pos, pos+count) not [pos, count)
  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);
  }
Thanks for the replies, the:
while(buffer[sentence_end+1] == buffer[sentence_end]) sentence_end++;
Is only to not end sentences like: "I wonder..." at the first dot. It shouldn't go over the size of the buffer, but it sure has an error when it reaches npos. Thanks for pointing that out.

I said it's a pointer because the crash is repported by the debbuger as being caused by the removal of the string vector.

It might just be caused by trying to aquire an element from buffer that doesn't exist.. I'll try it.
Thanks again.
Just a brief comment. The while(buffer[sentence_end+1] ... etc. that I mentioned isn't a serious problem, though it pushes at the boundaries. It can access a character past the end of the string, but that is usually containing a null terminator, and it will go no further because that clearly isn't the same as the end-of-sentence character.

As for the other problems, I suggest using a very simple program to test your code, trying to combine it with other classes and possibly complicated code may leave you uncertain as to which part is the cause of the problem

In my code for example, I did something like this:
1
2
3
4
5
    std::ifstream fin("test.txt");
    std::string buffer;
    char ch;
    while ( fin.get(ch) )  
        buffer += ch;


That rather crude code allowed me to load some text into the buffer in a simple and dependable way.

After that, any errors must arise from the sentence splitting code.

I also wrote the contents of the vector to an output file too, in order to evaluate the accuracy and validity of the results

Topic archived. No new replies allowed.