I have this struct
in a data structure and need to free the memory it uses:
typedef struct
{
int capacityOfElements; //capacidade do vetor
int numberOfElements; //número de elementos presentes no vetor
int *data; //elementos do vetor
}tVector;
For this I am using the free()
function where theoretically, I would first release V->data
and dps V
to free all memory used. However the free(V->data)
call gives the following error:
Error in './V': free (): invalid next size (fast): 0x0000000001d82030
Aborted (core dumped)
Does anyone know how to fix this?
tVector* create(int n)
{
tVector* newVector = malloc(sizeof(tVector));
if(!newVector)
printf("error in the Malloc process for newVector.\n");
newVector->data = malloc(newVector->capacityOfElements *sizeof(int));
if(!(newVector->data))
printf("error in the Malloc process for newVector->data.\n");
newVector->capacityOfElements=n;
newVector->numberOfElements=0;
return newVector;