Could someone help me see where I’m wrong in my queue allocation

Asked

Viewed 30 times

-1

#include <stdio.h>
#include <stdlib.h>

typedef struct _fila {
   int ini;
   int fim;
   int tam;
   int *vetor;
} Fila;

Fila* criar_fila (int tamanho) {
   Fila* fila = (Fila *) malloc (tamanho*sizeof(int));
   fila->ini = 0;
   fila->fim = 0;
   fila->tam = tamanho;
   fila->vetor[fila->tam];
   
   return fila;
}

void destruir_fila (Fila *f) {
   free(f->vetor);
   free(f);
}

void enqueue (Fila *f, int elemento) {
    if (f->fim < f->tam){
        f->vetor[f->fim] = elemento;
        f->fim++;
    }
}

int dequeue (Fila *f) {
   int elemento = f->vetor[f->ini];
   f->ini++;
   return elemento;
}       

int main () {

   /*Inicializando a estrutura de dados fila!*/  
   Fila *fila = criar_fila (10);

   /*Tentando inserir o elemento 1 na fila!*/ 
   int elem = 1; 
   enqueue (fila, elem);

   /*Tentando inserir o elemento 2 na fila!*/ 
   elem = 2; 
   enqueue (fila, elem);

   /*Tentando inserir o elemento 3 na fila!*/ 
   elem = 3; 
   enqueue (fila, elem);

   /*Tentando inserir o elemento 4 na fila!*/ 
   elem = 4; 
   enqueue (fila, elem);

   /*Tentando retirar e imprimir o elemento 1 na fila!*/ 
   printf("%d\n", dequeue (fila));

   /*Tentando retirar e imprimir o elemento 2 na fila!*/ 
   printf("%d\n", dequeue (fila));

   destruir_fila (fila);

   return 0;
}

I’m returning queue, in the method to create queue(int size), but it’s compiling but from Segmentation fault.

  • 1

    In function criar_fila you do fila->vetor[fila->tam], but what should it do? If the vector is also a pointer, it should not allocate its memory as well?

1 answer

1

The problem occurs in two places, within its function criar_fila.

The first is the fact that you are making the memory allocation sufficient for a certain amount of ints, but not for its structure Fila:

Fila* fila = (Fila *) malloc (tamanho*sizeof(int));

When the right thing would be:

Fila* fila = (Fila *) malloc (sizeof(Fila));

See that I removed the variable tamanho also of the memory allocation of its structure Fila. How is using only a structure with a vector inside do not need to allocate enough memory for 10 (ten) structures, only one.

The second problem occurs in using your vector from a pointer. See that in the function destruir_fila you try to call free in its vector; but at no time malloc was called to allocate memory and allow your pointer to point to:

Fila* criar_fila (int tamanho) {
   Fila* fila = (Fila *) malloc (sizeof(Fila));
   fila->ini = 0;
   fila->fim = 0;
   fila->tam = tamanho;
   fila->vetor[fila->tam];
   
   return fila;
}

In this case, you can initialize your pointer with the malloc and the size of the vector:

Fila* criar_fila (int tamanho) {
   Fila* fila = (Fila *) malloc (sizeof(Fila));
   fila->ini = 0;
   fila->fim = 0;
   fila->tam = tamanho;
   fila->vetor = malloc (tamanho * sizeof(int));
   
   return fila;
}

The result of code execution is now:

1
2
  • 1

    "do not need to allocate enough memory for 10 (ten) structures, just one" technique also known as Rest Assurance Memory Allocation Pattern (https://desciclopedia.org/wiki/Gambi_Design_Patterns#Rest_assurance_memory_allocation_pattern)

Browser other questions tagged

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