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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:35:05+00:00 2026-06-09T17:35:05+00:00

I have two functions for allocating and deallocating timers. Allocate timer allocates a timer

  • 0

I have two functions for allocating and deallocating timers.

Allocate timer allocates a timer and returns an int to the timer that has been allocated

int allocate_timer(void)
{
  int count = 0;
  int allocated = 0;
    /*Loop to find the first timer that is not allocated*/
    for(count = 0; count< ARRAY_SIZE; count++)
    {
            if(allocated_timers[count] == '0')
            {
                    /*When the next timer available timer is found it is set to allocated and timer is set to zero*/
                    allocated_timers[count] = '1';
                    timers[count] = 0;
                    break;
            }

            else if(allocated > ARRAY_SIZE - 1)
            {
                printf("No timers available\n");
                exit(0);

            }

            else
            {
                allocated++;
            }

    }
    /*Position of the allocated timer is returned*/
    return count;
}

Deallocating timer, takes in an int to the position that is going to be deallocated

void deallocate_one_timer(int position)
{       
    if(TIMER_ALLOCATED == allocated_timers[position])
    {
            allocated_timers[position] = '0';
            timers[position] = 0;
    }
}

I can’t see away to make them anymore robust than they already they are.
Any advice on how to make them better?

  • 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-09T17:35:07+00:00Added an answer on June 9, 2026 at 5:35 pm

    The variable allocated is always equal to count (so could be removed), and IMO it’s potentially confusing to use '0' and '1' as the values in the allocated_timers array. Usually it would be 0 and 1.

    Neither of them affects the robustness of the code as it is, but the easier the code is to understand, the more robust it is against future modification.

    When you have two “parallel” arrays, like you do here where each timer has an entry in timers and a corresponding entry in allocated_timers, it is worth considering whether it would be better to have a single array of a struct with two members (in this case perhaps named value and allocated). Sometimes it isn’t better, but often it aids understanding of the code because readers don’t have to discover and remember that these two arrays are closely related.

    deallocate_one_timer could be made slightly more robust against erroneous use by callers, if it checked that position is within the range 0 to ARRAY_SIZE before using it as an array index. I’m not saying that functions have a responsibility to do those checks, but they sometimes help diagnose bugs elsewhere. You can use assert for non-essential checks like this. assert has two benefits. First it self-documents that the check isn’t this function’s responsibility to handle, merely that you’re checking someone else did what they should have. Second, you can easily disable all asserts in non-debug builds of your program if you need to make it smaller or faster.

    Similarly it might be helpful to exit with an error message if a timer is deallocated that is not currently allocated, because that’s likely to indicate a potential problem. Whoever deallocates it twice might do so either side of somebody else allocating it, which means that somebody else suddenly finds they no longer have exclusive use of their timer.

    Finally, you set timers[index] to 0 both on allocate and on deallocate. Nothing particularly wrong with that except that it confuses the issue of which function is actually responsible for ensuring that a newly-allocated timer has the correct initial value. The deallocate function could do nothing, or it could set the timer to a value that’s impossible for an allocated timer to hold (maybe -1, assuming timers go up from 0), so that when debugging you can know immediately that if you’re using a timer whose value is -1, something has gone wrong.

    Finally finally, this code (obviously) isn’t thread-safe, which I suppose is a kind of non-robustness. There’s no shame in writing code that can’t be used in multi-threaded programs, especially with embedded systems that may not even have the capacity to create threads. Just so long as it’s a deliberate decision, and is documented.

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

Sidebar

Related Questions

I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have two functions that caluclate and adjust the height and width of elements
I have two functions that have different enough logic but pretty much the same
I have two functions that are supposed to encrypt and decrypt a string but
I have two functions that are very similar and i'd like to make a
I have two functions here say animation1 and load1 they work like that: function
I have two functions: double fullFingerPrinting(string location1, string location2, int nGrams) double AllSubstrings(string location1,
I have two functions that access the internet on the APP start. I've tried
I have two functions that I want to run on different threads (because they're
I have two functions that I have overloaded for my debug class: template<class IteratorT>

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.