views:

403

answers:

3

I get a seg fault for the simple program below. It seems to be related to the destructor match_results.

#include <iostream>
#include <vector>
#include <string>
#include <boost/regex.hpp>

using namespace std;

int main(int argc, char *argv)
{
    boost::regex re;
    boost::cmatch matches;

    boost::regex_match("abc", matches, re.assign("(a)bc"));

    return 0;
}

edit: I am using boost 1.39

+4  A: 

boost::regex is one of the few components of boost that doesn't exist solely in header files...there is a library module.

It is likely that the library you are using was built with different settings than your application.

Edit: Found an example scenario with this known boost bug, where boost must be built with the same -malign-double flag as your application.

This is one of several possible scenarios where your boost library will not have binary compatibility with your application.

Shmoopty
A: 

Which version of boost are you using?

I compiled the above example with boost 1.36 and I don't get any seg faults.

If you have multiple boost libraries make sure that at runtime you're picking up the correct version.

Boost regex requires to be compiled against library -lboost_regex-gcc_whatever-is-your- version

In my case:

g++ -c -Wall -I /include/boost-1_36_0 -o main.o main.cpp
g++ -Wall -I /include/boost-1_36_0 -L/lib/boost-1_36_0 -lboost_regex-gcc33-mt main.o -o x

to execute:

LD_LIBRARY_PATH=/lib/boost-1_36_0 ./x

You would point to the location of boost include/libs on your system, note the version of gcc and m(ulti) t(hreaded) in library name - it depends on what you have compiled, just look in your boost lib directory and pick one version of regex library from there.

stefanB
A: 

You are using temporary variable from which you want to obtain matches. I think, that your problem will resolved, if instead "abc" you will use following:

string a("abc);
regex_match(a, matches, re.assign("(a)bc"));
Alex Ott