1

I am trying to create an XML parser in C++. I am currently using cygwin and gcc to compile and gdb to debug. I have this piece of code:

const size_t mDataSize = mData.size();  
...  
size_t ltPos = mData.find_first_of('<', pos);  
if (ltPos==mData.npos) {  
...  

mData is declared as private const std::string & within the class and holds the XML file content. After debugging with gdb I found the following:

(gdb) print pos  
$12 = 636  
(gdb) print mDataSize  
$13 = 2692  
(gdb) n  
141             size_t ltPos = mData.find_first_of('<', pos);  
(gdb) print ltPos  
$14 = 114  
(gdb) print pos  
$15 = 636  
(gdb) n  
143             if (ltPos==mData.npos)  
(gdb) print ltPos  
$16 = 4294967295  
(gdb) print mData[636]  
$17 = (const char &) @0xb2b2a8: 10 '\n'  
(gdb) print mData[637]  
$18 = (const char &) @0xb2b2a9: 32 ' '  
(gdb) print mData[638]  
$19 = (const char &) @0xb2b2aa: 32 ' '  
(gdb) print mData[639]  
$20 = (const char &) @0xb2b2ab: 60 '<'  

I was expecting 639 as result of calling find_first_of, but I am getting 4294967295 (which is -1 in a signed 32-bit int and matches std::string::npos). Can someone justify this behaviour? Or tell me how to workaround this?

Mat
  • 202,337
  • 40
  • 393
  • 406
Charlie
  • 152
  • 1
  • 12
  • 2
    Can you find a shorter string that produces this behavior? And post it, along with minimal and complete code? As it stands, I can't reproduce the error. – Beta Oct 08 '11 at 13:11
  • 1
    My bet is the error is in code you haven't shown. A minimal, complete, compilable example that replicates the error would help. – David Schwartz Oct 08 '11 at 13:16
  • By the way, you can just say `find` if you're looking for a single character. `find_first_of` is for finding *any* of a given collection of characters. No difference, but it's easier to read and reason about, I guess. – Kerrek SB Oct 08 '11 at 13:18
  • 1
    @Charlie - As an aside, if you are looking for a single char, the `find` function would be a more obvious candidate to use than `find_first_of`. – Bo Persson Oct 08 '11 at 13:20
  • @Charlie: what's at line 142? According to your gdb output, `ltpos` is 636 at line 141 and then -1 at line 143. Is there code at line 142? Is line 141 within a loop or other construct? – Carlos Oct 09 '11 at 21:27
  • @Carlos: line 142 is a blank line, for this reason the gdb is jumping from 141 to 143 directly. – Charlie Oct 10 '11 at 08:20

1 Answers1

0

So mData is declared as a reference? If so it doesn't really hold the content it holds a reference to the content. Is the thing to which mData refers still in existence at the time you're calling find_first_of?

  • Yes, mData is declared as const std::string & within the class. The value of this member is set in the constructor. This reference is pointing to a std::string declared in the main method of this app as a local variable, which is holding the XML content. The app is using only one thread, so during the parsing process, there is no chance to change the XML content (because inside the parser the mData is declared const). Also, as you can see the gdb is showing that the data is still there. – Charlie Oct 10 '11 at 08:28