-1

I am using c++03, and trying to figure out which container to use. The hardware is MSP430F5324 with total of 6k of RAM (only 5k is available to me), and 64k flash. I am trying to store combination of derived classes, for example:

class OptNode {
 public:
    virtual ~OptNode();
    explicit OptNode(uint16_t option_no) : option(option_no), _next(0) {}
 protected:
    uint16_t option;
    unsigned length;
};

// --- Use heap memory for the option
class OptNodeDynamic : public OptNode {
 public:
    // --- assignment operators and copy constructor are not defined
    OptNodeDynamic(const OptNodeDynamic &cSource);
    OptNodeDynamic& operator= (const OptNodeDynamic &cSource);

    OptNodeDynamic(uint16_t option_no, uint8_t* option_data, size_t length);
    ~OptNodeDynamic();
 private:
    uint8_t* data;
};

// --- Use the c string for the option
class OptNodeCstr : public OptNode {
 public:
    OptNodeCstr(uint16_t option_no, const char* option_data);
 private:
    const char* data;
};

// ----- OptNodeDynamic() ------------------------------------------------------
OptNodeDynamic::OptNodeDynamic(uint16_t option_no,
                                     uint8_t* option_data,
                                     size_t length) : OptNode(option_no) {
    //
    option = option_no;
    data = new uint8_t[length];
    std::memcpy(data, option_data, length);
}

// ----- ~OptNodeDynamic() -----------------------------------------------------
OptNodeDynamic::~OptNodeDynamic() {
    if (data != 0 && length != 0) {
        delete data;
    }
}

// ----- OptNodeCstr() ---------------------------------------------------------
OptNodeCstr::OptNodeCstr(uint16_t option_no,
                               const char* option_data)
                  : OptNode(option_no),
                    data(option_data) {
    //
    length = 0;
    for (const char* itr_ptr = data; *itr_ptr != '\0'; ++itr_ptr, ++length) {
        /* no code */
    }
    return;
}

The container should allocate only what it needs for each of the objects. I should be able to add objects of type B or C in any order and as many as needed. Is there such container? Or do I instantiate each object using new operator and pass the pointer to a container, such as list.

Edit: I added hardware description and changed class from example to the actual. I am thinking that the system will need from 0 to 8 instances during data upload process, which happens at most once per hour. Other times, I can use that heap memory for something else.

user1135541
  • 1,781
  • 3
  • 19
  • 41
  • 4
    With only 5k of memory to play with you should think in different directions than a general container. You don't have roomfor the runtime library that the standard containers depend on. I.e. you simply can't use standard containers, except `std::array`. E.g. place the B and C instances in separate arrays. That can yield simple code and efficient indexing. – Cheers and hth. - Alf Jul 28 '15 at 16:06
  • 3
    what exactly is the purpose of such container? just to hold a row of elements? should it be polymorphic? should it arrange them in some sort? your question is very ambigous.. – David Haim Jul 28 '15 at 16:07
  • 3
    With 5k ram, there is imho no room for c++ dynanmic memory management at all. I would stick to good old c.. – Nikolay Jul 28 '15 at 16:11
  • Regarding "`new` operator", you don't have room for the dynamic allocation machinery, sorry. If you meant a "`new` expression" then that's something else that doesn't necessarily involve dynamic allocation. But I think it's unlikely that you had anything other than dynamic allocation in mind, because then you'd have written that. – Cheers and hth. - Alf Jul 28 '15 at 16:12
  • You need to give more details about your execution environment, how you would use the objects and how many of them there would be. Try show classes that actually represent the classes you would actually be using. Does your system have 5K of memory total (as some people are assuming) or do you also have a separate and much larger program (flash) memory? What is the CPU you're targeting? – Ross Ridge Jul 28 '15 at 17:46
  • I added some more details, but as far as usage, need to be able to add objects one by one. When deleting, all of the objects will be deleted all at once. Need to iterate through objects and get the data out to the serial port stream. – user1135541 Jul 28 '15 at 18:40

3 Answers3

3

None.

You do not have enough memory on your platform for the C++ runtime library. You should not and cannot use any C++ container.

Frankly I'd stick with good ol' C for this. As for what code you actually write to achieve your goal, well that depends on your goal. :)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

There is a significant overhead when allocating individual objects with new (up to several tenths of bytes per call to new).

If you want to save memory, the best thing to do is to pre-allocate three chunks of memory (pools) for storing the instances of A, B and C, and overload the new and delete operators for A,B and C.

You will find examples in the answer of the following question:

Using operator new and operator delete with a custom memory pool/allocator

Community
  • 1
  • 1
BrunoLevy
  • 2,495
  • 17
  • 30
1

Preallocating all the objects at init time and then trying to use the pointers throughout the life cycle of the process will be better idea.

Ritesh
  • 1,809
  • 1
  • 14
  • 16