Monday, August 27, 2012

C++ regex source string cannot be a temporary

regex is new to C++11 and was available via Boost before then.  In playing around with it, I could not figure out why the following worked sometimes, but not all the time:

// Bad code
smatch sm1;
regex_search(string("helloworld.jpg"), sm1,

cout << sm1[1] << endl;

Then, thanks to I learned that smatch retains just pointers to the source string "helloworld.jpg" and not actual substrings.  So when the temporary object string("helloworld.jpg") gets released, the smatch pointers are left pointing to released (and thus undefined) memory.

The correct code is:

// Good code
smatch sm1;
string s1("helloworld.jpg")
regex_search(s1, sm1, regex("(.*)jpg"));
cout << sm1[1] << endl;

Now, in real code you wouldn't pass a hard-coded temporary object, but during debug/development you might to test out how regex handles different scenarios, which is what I was trying to do when I ran into this.

No comments: