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

  • Home
  • SEARCH
  • 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 8104117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:44:35+00:00 2026-06-05T23:44:35+00:00

Got a seg fault from my memcpy that gdb can’t give me anything else

  • 0

Got a seg fault from my memcpy that gdb can’t give me anything else on (at least beyond the simple manner that I know how to use gdb…). This thing is deeply imbedded in some code using the Berkely DB; I have taken out the only lines that should be of any use.

  void *databuf;
  int smallest;
  databuf=malloc(2*sizeof(int));
  memset(databuf,0,2*sizeof(int));



  /* This next line comes from the DB structures; key.data is a void*... */
  smallest=*(int *)key.data;

  memcpy(databuf,(void *)smallest,sizeof(int));

To confirm the variable smallest is correct, I can run gdb and get

(gdb) print smallest
$1 = 120321

The error I recieve (in gdb) is the useless

Program received signal SIGSEGV, Segmentation fault.
0x08048efa in main (argc=4, argv=0xbffff344) at index_DB-1.1.c:128
128       memcpy(databuf,(void *)smallest,sizeof(int));
(gdb) backtrace
#0  0x08048efa in main (argc=4, argv=0xbffff344) at index_DB-1.1.c:128

The reason I am doing this is mostly because I am bastardizing the Berkley DB tutorial, but also later I want to do

memcpy(databuf+len,(void *)largest,sizeof(int));

i.e. have a void pointer databuf with first byes the smallest integer and second bytes the largest integer. What am I missing?

  • 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-05T23:44:38+00:00Added an answer on June 5, 2026 at 11:44 pm

    In this step, you are interpreting the value in smallest as a pointer:

    memcpy(databuf,(void *)smallest,sizeof(int));
    

    Since that value is almost certainly not a valid pointer, this is causing your segfault. Instead, you likely want:

    memcpy(databuf, &smallest, sizeof smallest);
    

    Unless you need smallest for some other reason though, you can just copy directly from key.data to to databuf:

    memcpy(databuf, key.data, sizeof(int));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just got a seg fault in overloading the assignment operator for a class
I've got a simple class called object that I'm having a problem with. Theres
got a simple web site, in html. I do a simple function to move
got a problem, can't get my head around this jquery and would appreciate your
got some problems. Built an applet that has to be used step-by-step. After each
I've got 3 files that relate to this problem. file.h, file.C and user.C. file.h
Ok, so I have a 2D Array that is initialised with values from a
So I've got a bunch of worker threads doing simple curl class, each worker
Got a style conflict between StyleCop and Resharper. Stylecop 1303 says that const vars
Got a complex SELECT query, from which I would like to insert all rows

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.