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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:54:40+00:00 2026-05-30T20:54:40+00:00

I currently have a struct typedef struct Entry { int counter; void *block; }

  • 0

I currently have a struct

typedef struct Entry {
    int counter;
    void *block;
} Entry;

and a mmap’ed block of memory

void *memPtr = mmap(NULL, someSize*1024, PROT_READ|PROT_WRITE, 
                        MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);

which I then add entries to the first part all the time (which is stupid, but that part’s a bit irrelevant) as such:

int AddEntry(void *data) {
   Entry entry;
   entry.counter = 1;
   entry.block = malloc(sizeof(char *) * SECTOR_SIZE);
   memcpy(entry.block, data, SECTOR_SIZE);
   memcpy(&memPtr[0], &entry, sizeof(Entry));
   return 0;
}

The problem here is that Entry is only 16 bytes long because block is a void pointer. What’s the best way to ensure that block is actually the size of sizeof(char *) * SECTOR_SIZE and actually has enough space for data, and how would one load data into entry.block?

Thanks!

  • 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-05-30T20:54:41+00:00Added an answer on May 30, 2026 at 8:54 pm

    If I’m reading the question correctly, you’re asking how to get a contiguous block of memory that can be accessed with a (smaller) Entry *

    The most common way is to define your structure like:

    typedef struct Entry {
        int counter;
        unsigned char block[1];
    }
    

    Depending on the behavior of your compiler you can also define block to be a zero length array.

    You can then malloc the space that you require for the structure and the SECTOR_SIZE:

    int AddEntry(void *data) {
       Entry *entry;
       entry = malloc(sizeof(Entry) + ((SECTOR_SIZE-1) * sizeof(unsigned char));
       // obligitory NULL checks assumed here

    // fill in structure
    entry->counter = 1;
    memcpy(entry->block, data, SECTOR_SIZE);
    memcpy(&memPtr[0], &entry, sizeof(Entry) + ((SECTOR_SIZE-1) * sizeof(unsigned char));
    return 0;
    }

    This will also allow you to access, say, the 78th byte of block by:
    entry->block[77]

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

Sidebar

Related Questions

So I have a structure of unknown size as follows: typedef struct a{ int
OK, I hope I explain this one correctly. I have a struct: typedef struct
I have following method in the c code. void add(int number) { Node node1;
I have a struct: typedef struct { Qt::Key qKey; QString strFormType; } KeyPair; Now
I've a weird problem with malloc. I have this typedefs: typedef struct buffer {
I have a struct call 'A', which has an attribute 'i', like this: typedef
I have seen the following code , /* stack.c */ typedef struct Stack *StackPtr;
I currently have the following code. struct qnode { char *path; struct qnode *next;
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have speakers set up both in my office and in my living

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.