Segmentation fault error in simple program. Please help
695543Apr 9 2009 — edited Apr 15 2009Hello!
I'm getting a Segmentation fault error on Linux and have no idea why. Please, could someone help me?
<code>
#include <db_cxx.h>
#include <iostream>
#include <string>
using namespace std;
int main() {
try {
Db db(NULL, 0);
u_int32_t oFlags = DB_CREATE;
db.open(NULL,"test.db",NULL,DB_BTREE,oFlags,0);
string keystring("klucz");
string datastring("wartosc");
Dbt key((void*)keystring.c_str(),sizeof(keystring.c_str())+1);
Dbt data(&datastring,sizeof(datastring));
int ret;
ret = db.put(NULL, &key, &data, DB_NOOVERWRITE);
if (ret == DB_KEYEXIST) {
cout << "Key exist." << endl;
}
Dbc *cursorp;
db.cursor(NULL,&cursorp,0);
Dbt Key_;
Dbt Elem_;
while ((ret = cursorp->get(&Key_, &Elem_, DB_NEXT)) == 0) {
string str =(string )Elem_.get_data();
cout << *str << endl;
}
if(cursorp != NULL)
cursorp->close();
db.close(0);
} catch(DbException &e) {
} catch(exception &e) {
cout << e.what();
}
return 0;
}
</code>