free(): invalid next size (fast) while trying to free up memory

Asked

Viewed 1,128 times

4

I have this struct in a data structure and need to free up memory with it used:

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 function free() where theoretically it would release first V->data and dps V to release all memory used. But the call free(V->data) gives the following error:

Error in `./V': free(): invalid next size (fast): 0x0000000001d82030
Aborted (core dumped)

someone knows how to fix it?


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;
  • 1

    You instate tVector using malloc?

  • yes, here follows the function in which malloc use

  • 1

    Have you ever tried to free the memory of each one individually ?

  • 1

    newVector->capacityOfElements no value is set when you do the malloc()

2 answers

1

Try it like this

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 makes

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

and then to free the memory

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

"Invalid next size" errors usually indicate memory management errors: do free() to blocks not obtained by malloc(); write beyond the block boundary; assume (wrongly) which pointers and ints have the same size; etc.

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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.