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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:47:37+00:00 2026-06-12T16:47:37+00:00

I have a array of somestruct_t I’m adding to but don’t know in advance

  • 0

I have a array of somestruct_t I’m adding to but don’t know in advance how many elements it will need. So I malloc 5 slots of space to start with but I need to encapsulate it in another struct that saves the array boundary and whats used so far so I don’t overflow. I’ll realloc if it looks like it will overflow.

typedef struct {
  somestruct_t[] *data;
  uint32_t max_size;
  uint32_t used;
} something_box_t;

Is this the idiomatic way to do this?

  • 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-12T16:47:38+00:00Added an answer on June 12, 2026 at 4:47 pm

    One thing that’s done in C sometimes is to use a zero length array at the end of a struct and to allocate extra space for a variable number of elements. I’m not sure if zero length arrays are legal in C (they’re not in C++) but it’s commonly supported.

    struct something_box_t {
      uint32_t max_size;
      uint32_t used;
      somestruct_t data[];
    };
    
    something_box_t *box = malloc(sizeof(something_box_t) + N*sizeof(somestruct_t));
    box.max_size = N;
    box.used = 0;
    
    box.data[0] = ...;
    box.data[1] = ...;
    

    Of course, if you can switch to C++ you won’t have to hack together a solution on your own. You could just do std::vector<somestruct_t> box; and have a solution far easier to use, as performant if not more performant, and more reliable than what you’re likely to write in C.

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

Sidebar

Related Questions

I have array of strings, String[] data and it's 10 elements has value P
An array is defined of assumed elements like I have array like String[] strArray
i have array in php, and i need work with this array in jQuery.
I have array of objects. I want to write method wich i will use
I have array of objects: var aoo = [{},{},{},....{},{},{}]; I need a optimized function
I have array $arr=array(a=>array(b=>123)) and I need to do with it something like this:
I have array of 0's and 1's. And I need a function which return
I have a two dimensional array that I need to load data into. I
I have an array of structs, and I have some functions that will be
I have array populated with elements and I want to display elements of that

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.