0

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.

Hasan alattar
  • 347
  • 3
  • 19
  • "because its for embedded system and the current IDE/toolchain im using doesnt support std:vectors." I _strongly_ advise you to get a better IDE/toolchain. – TypeIA Feb 05 '19 at 13:42
  • Looks like a dupe of [this](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) and [this](https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new) – NathanOliver Feb 05 '19 at 13:43
  • 1
    Also, if your implementation does not support vector you could always write your own to encapsulate the memory management. `EELS` should not be responsible for it. – NathanOliver Feb 05 '19 at 13:45
  • 3
    why not `new` / `delete` ? – 463035818_is_not_an_ai Feb 05 '19 at 13:47
  • @TypeIA im using Atmel studio and I'm not expert enough to play with compilers or STL libraries. i know a memory managment library should be there but to be honest im not that expert. i mean even if i had vector library it would be my first time to use it (with few tutorials i can survive). i know that the right way is using memory managment library but I'm asking this question because i know i want to do something simple IMHO and it doesn't worth it to go through all that path in the mean time. – Hasan alattar Feb 06 '19 at 07:13

1 Answers1

0

What happens when _slot_counter == EELS_MAX_SLOTS. So I think you should change the if statement

if (_slot_counter > EELS_MAX_SLOTS)
    return NULL;

to

if (_slot_counter >= EELS_MAX_SLOTS)
    return 0; // return type is uint8_t, not a pointer
Loc Tran
  • 1,170
  • 7
  • 15
  • what will change i think compiler will make it 0 anyway. – Hasan alattar Feb 06 '19 at 06:59
  • Agreed. But sometime make yourself confuse when you read source code. NULL should use for pionter only. Anyway, the if condition is more important. Should be >= – Loc Tran Feb 06 '19 at 13:47
  • i put > because 0 is reserved for 0. so its not like array index. but i should be careful using index-1 in all the functions.. its stupid i admit. – Hasan alattar Feb 06 '19 at 13:51
  • 1
    ok. if you fixed it, there is no memory leak. your destructor free memory from malloc. vector or another container is not so important. – Loc Tran Feb 06 '19 at 15:26