im trying to create a class which has malloc.
- the class has internal struct.
the user will have pointer to the struct but he may not know about the struct or even care about it.
he must save the pointer and some functions will require the address of that struct.
so in the header of the library i did the following:
#define EELS_MAX_SLOTS 5
class EELS
{
typedef struct{
//struct difinition ...
}ee_slot_t;
public:
EELS();
uint8_t CreateSlot(uint16_t begin_addr, uint16_t length, uint8_t data_length);
~EELS();
protected:
private:
void* _slot_arr[EELS_MAX_SLOTS];
uint8_t _slot_counter;
};
and the code in the execution file:
// default constructor
EELS::EELS()
{
_slot_counter =0;
} //EELS
uint8_t EELS::CreateSlot(uint16_t begin_addr, uint16_t length, uint8_t data_length){
if (_slot_counter > EELS_MAX_SLOTS)
return NULL;
ee_slot_t* slot_p;
slot_p = malloc(sizeof(ee_slot_t))
if (!slot_p)
return NULL;
slot_p->begining = begin_addr;
slot_p->length = length;
slot_p->counter = 0; // TODO...init...
slot_p->position = 0; // TODO...init...
_slot_arr[_slot_counter] = (void*)slot_p;
_slot_counter++;
return _slot_counter;
}
// default destructor
EELS::~EELS()
{
for (int i=0; i<_slot_counter; i++)
{
free((ee_slot_t*)_slot_arr[i]);
}
}
as you can see im returning index of pointers array.. so (1-6) in this case and I'm saving the real address inside that pointers array.
but from what you see. is this safe? the free method and malloc.. there is some mistake or memory leakage?
why not vector?
because its for embedded system and the current IDE/toolchain im using doesnt support std:vectors.