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.