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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:43:12+00:00 2026-05-26T09:43:12+00:00

I have a regular array of structs in C, in a program that runs

  • 0

I have a regular array of structs in C, in a program that runs every second and updates all the data in the structs. When a condition is met, one of the elements gets cleared and is used as a free slot for new element (in this case timers) that might come in at any point.

What I do is just to parse all the elements of the array looking for active elements requiring updates. But even if the amount of elements is small (<2000), I feel this is wasting time going through the inactive ones. Is there a way I can keep the array gap-free so I just need to iterate though the number of currently allocated elements?

  • 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-26T09:43:12+00:00Added an answer on May 26, 2026 at 9:43 am

    Assuming the specific order of the elements does not matter, it can be done very easily.

    If you have your array A and the number of active elements N, you can then add an element E like this:

    A[N++] = E;
    

    and remove the element at index I like this:

    A[I] = A[--N];
    

    So how does this work? Well, it’s fairly simple. We want the array to only store active elements, so we may assume that the array is like that when we start doing either of these things.

    Adding an element will always put it at the end, and since all elements currently in the array, as well as the newly added element, will be active, we can safely add one to the end.

    Removing an element is done by moving the last element to take over the array index of the element we want to remove. Thus, A[0..I-1] is active, as well as A[I+1..N], and by moving A[N] to A[I], the entire range A[0..N-1] is active (A[N] is not active, because it no longer exists – we moved it to A[I], and that’s why we decrease N by 1).

    If you’re removing elements while iterating over them to update them, note that you can only increment your loop counter after processing an element which doesn’t get removed, since otherwise, you would never process the moved elements.

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

Sidebar

Related Questions

I need a regular expression that identify special links. I have an array with
By means of a regular expression and Greasemonkey I have an array of results
I have a regular expression that looks through html content for some keywords that
I have a regular expression in which I'm trying to extract every group of
I have 5 functions that check user input against regular expressions...these regular expressions have
I have an array of bytes which comes on the regular interval on the
I have an array of strings that I want to try and match to
I have an array of Foo objects. How do I remove the second element
Regular expressions have never been one of my strong points, and this one has
I have attempted to create a function that will replace multiple regular expression 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.