free (): invalid next size (fast) when trying to free memory

4

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;
    
asked by anonymous 03.12.2015 / 02:43

2 answers

1

Try it out

tVector *create(int n) {
    tVector *newVector = malloc(sizeof *newVector);
    if (!newVector) {
        fprintf(stderr, "error in the Malloc process for newVector.\n");
        exit(EXIT_FILURE);
    }

    // newVector->capacityOfElements nao esta atribuido             <=====
    newVector->data = malloc(n * sizeof *newVector->data);
    //                      ^^^ usa n                               <=====

    if (!newVector->data) {
        fprintf(stderr, "error in the Malloc process for newVector->data.\n");
        exit(EXIT_FAILURE);
    }
    newVector->capacityOfElements = n;
    newVector->numberOfElements = 0;
    return newVector;
}

To allocate memory does

tVector *ponteiro = create(42); // exits on error

and then to free the memory

free(ponteiro->data);
free(ponteiro);

"invalid next size" errors usually indicate errors in memory management: make free() to blocks not obtained by malloc() ; write beyond the block boundary; assume (wrongly) that pointers and int s are the same size; etc.

    
03.12.2015 / 16:35
0
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
  int capacityOfElements; //capacidade do vetor
  int numberOfElements;   //número de elementos presentes no vetor
  int *data;              //elementos do vetor
} tVector;

static tVector* create(int n)
{
   tVector* newVector = malloc(sizeof(tVector));                               

   if (!newVector)
   {
      printf("error in the Malloc process for newVector\n");
      return NULL;
   }

   // no original:
   // newVector->data = malloc(newVector->capacityOfElements *sizeof(int));
   // pode nao alocar memoria e retornar NULL, porque capacityOfElements ainda nao foi inicializado

   newVector->data = malloc(n*sizeof(int));       
   if (!newVector->data)
   {
      printf("error in the Malloc process for newVector->data\n");
      free(newVector);
      return NULL;
   }

   newVector->capacityOfElements = n;
   newVector->numberOfElements = 0;    

   return newVector;
}

static void destroy(tVector* v)
{
    if (!v) return;
    free(v->data);
    free(v);
}

int main(void)
{
    printf("criando vetor de 10 elementos\n");
    tVector* v = create(10);

    if (!v)
    {
       printf("erro ao tentar criar vetor de 10 elementos\n");
       exit(1);
    }

    printf("destruindo vetor de 10 elementos\n");
    destroy(v);
}
    
11.09.2016 / 17:42