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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:16:21+00:00 2026-05-30T15:16:21+00:00

I tried to replace a void* member of a struct with a flexible array

  • 0

I tried to replace a void* member of a struct with a flexible array member using the more accepted idiom:

typedef struct Entry {
    int counter;
    //void* block2; // This used to be what I had
    unsigned char block[1];
}

I then add entries into a continuous memory block:

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

as such:

int number = 0;

int AddEntry(void *data) {
   Entry *entry;
   entry = malloc(sizeof(Entry) + ((SECTOR_SIZE-1) * sizeof(unsigned char));
   entry->counter = 1;
   memcpy(entry->block, data, SECTOR_SIZE);

   // make sure number doesn't overflow space, etc...
   memcpy(&memPtr[number], entry, sizeof(Entry) + ((SECTOR_SIZE-1) * sizeof(unsigned char));
   number++;
   return 0;
}

The problem is unpacking this data once I need it. For example, if I do:

void * returnBlock(int i) {
    Entry * entry  = &memPtr[i];
    printf("Entry counter is %d\n", entry->counter); // returns 1, reliably
    return entry->block; // Gives me gibberish but not if I uncomment void* block2.
}

Is there a reason this could be? I don’t necessarily think I’m stomping on stuff anywhere, and it used to work with the void* approach. The weird thing is that if I put a dummy void* back into the struct, it works. It doesn’t work if I put in a dummy int.

Edit: actually, it also fails if number in AddEntry is not 0. What am I stepping on, if anything?

  • 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-30T15:16:23+00:00Added an answer on May 30, 2026 at 3:16 pm

    Your problem is here:

    &memPtr[number]
    

    Since memPtr is a void * pointer, this isn’t actually allowed in C at all. Some compilers do allow arithmetic on void * pointers as a language extension – however they treat it as if it were a char * pointer.

    That means that &memPtr[number] is likely indexing only number bytes into your memory block – so the second Entry structure copied in will overlap the first one, and so on.

    Your allocation line appears to be assuming 1024 bytes per Entry (if someSize is a number of Entry structures), so you probably want something like:

    ((char *)memPtr + number * 1024)
    

    (and similar in the returnBlock() function).

    However, if you do this you will notice that there is no point in using the flexible array member – because you’re creating a contiguous array of these structures, and don’t have a separate index, you have to assume each one is a fixed size. This means that you might as well make each one a fixed size:

    typedef struct Entry {
        int counter;
        unsigned char block[1024 - sizeof counter];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried converting the double value into a string and using the Replace() method
I tried to replace my Flash video with a jQuery cycle. The cycle works,
I have following text in a file 23456789 When I tried to replace the
I've tried to write a string replace function in C, which works on a
How can I replace all line-endings in big file (>100MB)? I have tried to
I started a different thread for this, I tried solving it using the help
I have a custom object and NSMutableArray as instance member. I fill the array
I have a function which takes three arguments void replace(const string, const string, string*)
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of

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.