Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7756157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:44:54+00:00 2026-06-01T12:44:54+00:00

So I’m currently working on a Berkeley database through C, and I ran into

  • 0

So I’m currently working on a Berkeley database through C, and I ran into a little problem concerning allowing duplicate data. If I don’t allow duplicate entries through DB->set_flags and I iterate through the items in the database with a cursor, everything works just fine. The moment I add the DB->set_flags line in my program, and change nothing else, I get a segmentation fault and I don’t know how to fix it, considering it works just fine without duplicate entries. The code is as follows:

DB *dates_db;
db_create(&dates_db, NULL, 0);
dates_db->set_flags(dates_db, DB_DUP);
dates_db->open(dates_db, NULL, "da.idx", NULL, DB_BTREE, 0, 0664);

DBT key, data;
memset(&key, 0, sizeof(key)); 
memset(&data, 0, sizeof(data));

DBC *DBpointer;
dates_db->cursor(dates_db, NULL, &DBpointer, 0);

while(DBpointer->c_get(DBpointer, &key, &data, DB_NEXT) != DB_NOTFOUND)
{
    printf("The key is: %s\nThe data is: %s\n", (char *)key.data, (char *)data.data);
}

Can anyone please tell me what I need to add? I’ve checked the sourceforge page that has all the information about Berkeley DB and as far as I know, I only need to add the DB->set_flags line to allow duplicate entries, but could there be something I’ve missed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:44:55+00:00Added an answer on June 1, 2026 at 12:44 pm

    For a start, you should always check return values when a function can fail.

    Specifically, db_create, set_flags, open, cursor all return an error indication.

    If, as you seem to indicate in a comment, it’s the c_get causing the SIGSEGV, you may want to ensure that the cursor was indeed created properly.

    Changing:

    DBC *DBpointer;
    dates_db->cursor(dates_db, NULL, &DBpointer, 0);
    

    to:

    DBC *DBpointer = (DBC*)0xdeadbeef;
    int rc = dates_db->cursor(dates_db, NULL, &DBpointer, 0);
    printf ("DEBUG: %d %p\n", rc, DBpointer);
    fflush (stdio); // and possibly also: fsync (fileno (stdio));
    

    would be a good start.

    Better would be to go all the way and use something like:

    #define CHKERR(x) if(rc!=0){printf("%s err=%d\n",x,rc);fflush(stdout);exit(1);}
    int rc;
    DB *dates_db;
    
    rc = db_create(&dates_db, NULL, 0);
    CHKERR("create");
    
    rc = dates_db->set_flags(dates_db, DB_DUP);
    CHKERR("set_flags");
    
    rc = dates_db->open(dates_db, NULL, "da.idx", NULL, DB_BTREE, 0, 0664);
    CHKERR("open");
    
    DBT key, data;
    memset(&key, 0, sizeof(key)); 
    memset(&data, 0, sizeof(data));
    
    DBC *DBpointer;
    rc = dates_db->cursor(dates_db, NULL, &DBpointer, 0);
    CHKERR("cursor");
    
    while((rc = DBpointer->c_get(DBpointer, &key, &data, DB_NEXT)) != DB_NOTFOUND)
    {
        CHKERR("c_get");
        printf("The key is: %s\nThe data is: %s\n", (char *)key.data, (char *)data.data);
    }
    

    Based on a further comment that you’re getting error 22 on the open call, that’s EINVAL (on my system at least) which means one of your parameters is incorrect.

    Based on web searches, there appear to be a few variations on the open call, ranging between five and seven arguments. Some Oracle BDB doco states that it takes five argument (no database pointer and not transaction pointer) but the sample code in the same document (and other Oracle doco) has your seven-argument form.

    It may be worthwhile looking into your BDB header files to see which one you should be using.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I have a reasonable size flat file database of text documents mostly saved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.