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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:51:05+00:00 2026-06-16T17:51:05+00:00

I am writing a program in C++ that will be used with Windows Embedded

  • 0

I am writing a program in C++ that will be used with Windows Embedded Compact 7. I have heard that it is best not to dynamically allocate arrays when writing embedded code. I will be keeping track of between 0 and 50 objects, so I am initially allocating 50 objects.

Object objectList[50];
int activeObjectIndex[50];
static const int INVALID_INDEX = -1;
int activeObjectCount=0;

activeObjectCount tells me how many objects I am actually using, and activeObjectIndex tells me which objects I am using. If the 0th, 7th, and 10th objects were being used I would want activeObjectIndex = [0,7,10,-1,-1,-1,...,-1]; and activeObjectCount=3;
As different objects become active or inactive I would like activeObjectIndex list to remain ordered.

Currently I am just sorting the activeObjectIndex at the end of each loop that the values might change in.

First, is there a better way to keep track of objects (that may or may not be active) in an embedded system than what I am doing? If not, is there an algorithm I can use to keep the objects sorted each time I add or remove and active object? Or should I just periodically do a bubble sort or something to keep them in order?

  • 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-16T17:51:06+00:00Added an answer on June 16, 2026 at 5:51 pm

    You have a hard question, where the answer requires quite a bit of knowledge about your system. Without that knowledge, no answer I can give would be complete. However, 15 years of embedded design has taught me the following:

    1. You are correct, you generally don’t want to allocate objects during runtime. Preallocate all the objects, and move them to active/inactive queues.
    2. Keeping things sorted is generally hard. Perhaps you don’t need to. You don’t mention it, but I’ll bet you really just need to keep your Objects in “used” and “free” pools, and you’re using the index to quickly find/delete Objects.

    I propose the following solution. Change your object to the following:

    class Object {
      Object *mNext, *mPrevious;
    public:
      Object() : mNext(this), mPrevious(this) { /* etc. */ }
    
      void insertAfterInList(Object *p2) {
        mNext->mPrev = p2;
        p2->mNext = mNext;
        mNext = p2;
        p2->mPrev = this;
      }
    
      void removeFromList() {
        mPrev->mNext = mNext;
        mNext->mPrev = mPrev;
        mNext = mPrev = this;
      }
    
      Object* getNext() {
        return mNext;
      }
    
      bool hasObjects() {
        return mNext != this;
      }
    };
    

    And use your Objects:

    #define NUM_OBJECTS (50)
    Object gObjects[NUM_OBJECTS], gFree, gUsed;
    
    void InitObjects() {
      for(int i = 0; i < NUM_OBJECTS; ++i) {
        gFree.insertAfter(&mObjects[i]);
      }
    }
    
    Object* GetNewObject() {
      assert(mFree.hasObjects());
      Object obj = mFree->getNext();
      obj->removeFromList();
      gUsed.insertAfter(obj);
      return obj;
    }
    
    void ReleaseObject(Object *obj) {
      obj->removeFromList();
      mFree.insertAfter(obj);
    }
    

    Edited to fix a small glitch. Should work now, although not tested. 🙂

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

Sidebar

Related Questions

I'm writing a Java program that will be used on both Windows and Mac.
I am writing a program that will eventually be used to have one child
I am contemplating writing a program that will move some newly created dirs to
I'm a writing a program that will determine the number of lines, characters, and
I'm writing a program that sends an array through a function (upperhess) that will
I'm writing a simple program that will run entirely client-side. (Desktop programming? do people
I am writing an HTML program that will be accessed by over 500 people
I'm writing a program in C# that will need to store a few Data
I am writing a program, for homework, that will add 2 8-bit binary numbers.
Im writing a program that should read input via stdin, so I have the

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.