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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:07:21+00:00 2026-05-15T20:07:21+00:00

I am trying to represent a HD page in a struct. The code below

  • 0

I am trying to represent a HD page in a struct. The code below does not work, because page_size is not a constant.

int page_size = 4096;

struct page {

  char data[page_size];

  /* some methods */
}

Since I am building and randomly accessing arrays of pages, it would be nice if I could treat a page as a fixed length struct of size page_size. If possible, I would like to read page_size from a configuration file at the start of the program, or get it from the OS (e.g., GetDiskFreeSpace() on Windows). Obviously, there are only a handful of realistic page sizes, but I can’t think of a way to exploit that.

Any help is appreciated.

Frank

  • 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-15T20:07:22+00:00Added an answer on May 15, 2026 at 8:07 pm

    Nope. As you’ve found out, you can’t. The compiler needs to know the size of its data structures, reliably and constantly, to do its work.

    I’m assuming that the page data is not the only content of your struct. You probably have some header / management information about your page in there along with the text.

    The common solution would be for your struct to contain a char * pointing at the actual text. That text could then be in a chunk of memory malloc()‘d or calloc()‘d to whatever size you need. Alas, this means you’ll have to remember to move the external text around along with your struct whenever you do operations that move the struct around, and memory manage the text when you get rid of the struct… the usual C headaches.


    EDIT

    There are sensible “standard” ways to do this kind of thing. Here is a suggestion:

    typedef char *page_ptr;
    int page_size = discover_page_size();
    int max_page_count = discover_max_page_count();
    int pages_stored = 0;
    page_ptr *page_pointers = calloc(max_page_count, sizeof(page_ptr));
    char *pages = calloc(max_page_count * page_size, sizeof(char));
    page_pointers[0] = pages;
    int i;
    for (i=1; i<max_page_count; i++) {
        page_pointers[i] = page_pointers[i-1] + page_size;
    }
    

    Now you can treat page_pointers as an array of pages, doing stuff like

    read(filedesc, page_pointers[page_count++], page_size);
    

    or

    memmove(page_pointers[69], page_pointers[42], page_size);
    

    You have an overhead of one pointer per page (surely acceptable), and your pages are physically contiguous in memory. What more could you ask? 🙂

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

Sidebar

Related Questions

I am having trouble trying to represent the below SQL (which returns the results
I'm trying to format various numbers on my page. These numbers either represent a
I'm trying to set up a DataGridView to represent a page out of a
I am trying to represent to genetic variation data in a database for my
I'm trying to represent a two-dimensional coordinate grid with a two-dimensional array. Problem is,
I'm trying to represent the result of an MD5 hash in the shortest possible
I am trying to represent a graph using disjoint union and record. The following
I am trying to make a class that will represent two different behaivours at
I'm trying to find a library for iPhone app that can represent a network
I've been trying to make a generic class to represent a range of values

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.