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 7873433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:30:48+00:00 2026-06-03T02:30:48+00:00

A buffer that holds binary chunks of data is supposed to be copied to

  • 0

A buffer that holds binary chunks of data is supposed to be copied to an array of structs (each struct represents a chunk), each chunk in the buffer is 20 bytes, first 4 bytes hold the hash value, then 8 bytes for an offset info, then 8 for a size:

thats the struct definition:

typedef struct{
  khint_t hash; // 4                                                                                                                                                               
  long int offset_start; // 8                                                                                                                                                      
  size_t size; // 8                                                                                                                                                                
} header_offset, *offset_p;

and below is the code that’s supposed to do whats mentioned:

  offset_p *offsets;
  size_t s= HEADER_OFFSET_SIZE;

  header_offset t_offsets[n_files];                                                                                       

  for (i=0; i< n_files; i++){
    memcpy(&t_offsets[i].hash, buff, sizeof(khint_t));
    buff+= sizeof(khint_t);
    memcpy(&t_offsets[i].offset_start, buff, sizeof(long int));
    buff+= sizeof(long int);
    memcpy(&t_offsets[i].size, buff, sizeof(size_t));
    buff+= sizeof(size_t);

    printf("hash read: %p\n", t_offsets[i].hash);
    printf("offset start read: %p\n", t_offsets[i].offset_start);
    printf("offset size read: %p\n", t_offsets[i].size);
  }

  memmove(offsets, t_offsets, sizeof(header_offset)*n_files);

  buff-= s*n_files;
  free(buff);                                                                                                                                                                  

  return offsets;

I was struggling copying the chunks directly into a header_p* so i decided to have a temporary struct array thats copied from the buffer, then gets copied to a header_p*, I’d appreciate it even more if you could provide me with a way to do it without using a temporary struct array.

The printfs print the right data, although when calling this function, the array of the pointers returned does not hold the right data, or the same data that was printed within the loop.

I’d like to know, without further code, whether its the way im using pointers that causes the array of offset_p’s not hold the right values.

  • 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-03T02:30:49+00:00Added an answer on June 3, 2026 at 2:30 am

    No memory has been allocated for offsets, memmove() does not allocate the memory for you:


    header_offset* offsets = malloc(sizeof(header_offset)*n_files);

    Given that your are allocating memory to be returned the use of t_offsets is unrequired: just populate offsets directly.

    EDIT:

    To return a header_offset*[] as commented by alk:

    header_offset** offsets = malloc(sizeof(header_offset*) * n_files);
    for (i=0; i< n_files; i++){
        *(offsets + i) = malloc(sizeof(header_offset));
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a simple byte buffer that stores its data in a char
A z buffer is just a 3d array that shows what object should be
I've got a char* buffer to hold a file that i read in binary
I have a char array buffer that I am using to store characters that
I have a simple reference counted class that holds a memory buffer. It looks
I have a collection class that holds lots of different types of data in
I'd like to program a buffer that is time constrained. It means that I
I have a direct buffer holding Integers that are already sorted (i.e. 1,1,3,3,3,3,7,7,....). Most
I have a buffer in class 'bufferClass' that will generate a signal to tell
I was certain that if you bind a buffer via glBindBuffer() , you can

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.