How to free memory?

i have this code:

typedef struct XbeeData {
int lenght;
uint8_t *data;
};

struct XbeeData XData;

void ReciveXbee (struct XbeeData *XData){
XData->lenght = getlenght();
XData->data = (uint8_t *)malloc(getlenght()*8);

for (int i=0; i<getlenght(); i++)
{
XData->data* = getdata(i);*

  • }*
    }
    void loop() {
  • ReciveXbee(&XData); *
    }
    [/quote]
    And i try much options but didn’t works any.
    thanks!!

I suppose that the reason you used a pointer in XbeeData is that the length of the data returned is unknown. You'd need to free the space you allocated within the loop() function (e.g. free(XData->data) after each ReciveXbee call.

But I guess I'd check to make sure that every malloc is matched with a free throughout the program.

Thanks AlphaBeta i found a solution:

typedef struct XbeeData {
int lenght;
uint8_t *data;
};

struct XbeeData XData;

void ReciveXbee (struct XbeeData *XData){
XData->lenght = getlenght();
XData->data = (uint8_t *)malloc(getlenght()*8);

for (int i=0; i<getlenght(); i++)
{
XData->data = getdata(i);

  • }*
    }
    void loop() {
    ** free(XData.data);**
  • ReciveXbee(&XData);*
    }
    [/quote]

It seems to me what you're doing there isn't right. You are now free-ing memory that hasn't be allocated yet. Which is illegal.

You should use free after you have used the data from the struct and no longer need it.