Which is the Best C++ Container to use

时间:2015-07-28 16:03:45

标签: c++

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.

3 个答案:

答案 0 :(得分:2)

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. :)

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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.