Thread: having trouble with my freeArr() method

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    having trouble with my freeArr() method

    My queue and test file all compile fine and run fine. but when i try to free everything in my queue not gets freed. other than that everything enqueue's fine and dequeue's fine and prints fine.

    queue.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "queue.h"
    
    void init ( Queue * qq)
    {
    	qq->cap = 26;
    	qq->arr = malloc( sizeof(int) * qq->cap);
    	qq->front = 0;
    	qq->back = 0;
    	qq->elements = 0;
     
    }
    void enqueue( Queue * qq, int num)
    {
    	if(qq->cap == qq->elements)
    	{
    		qq->arr = realloc(qq->arr, sizeof(int) * qq->cap);
    		qq->arr[qq->back++] = num;
    		qq->elements++;
    	}
    	else
    	{
    		qq->arr[ qq->back++ ] = num;
    		qq->elements++;
    	}
    }
    
    int dequeue( Queue * qq)
    {
    	int i;
    
    	if(empty(qq))
    	{
    		printf("ITS EMPTY!!\n");
    	}
    	else
    	{
    
    		qq->elements--;
    		qq->arr[qq->front] = 0;
    		for(i=0; i<qq->elements; i++)
    		{
    			qq->arr[i] = qq->arr[i+1];
    		}
    	}
    	return qq->arr[i];
    }
    
    int empty( Queue * qq)
    {
    	int test = 0;
    
    	if(qq->arr == NULL)
    	{
    		test = 1;
    	}
    	return test;
    }	
    
    int length(Queue * qq)
    {
    	return qq->elements;
    }
    
    void printArr(Queue * qq)
    {
    	int i;
    	
    	for(i=0; i<qq->elements; i++)
    	{
    		printf("%d ", qq->arr[i]);
    	} 
    }
    
    void freeArr( Queue * qq)
    {
    
    	free(qq->arr);
    }
    test.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "queue.h"
    
    int main()
    {
    
    	int choose = 1;
    	int num;
    	char nl;
    	Queue q;
    	init(&q);
    
    	printf("welcome to the queue ........\n");
    while ( choose != 0 )
    {
    	printf("1. enqueue\n");
    	printf("2. dequeue\n");
    	printf("3. print\n");
    	printf("4. Check if queue is empty\n");
    	printf("5. get length\n");
    	printf("6. free queue\n");
    	printf("0. Exit\n");
    	
    	scanf("%d%c", &choose, &nl);
    
    		switch(choose)
    		{
    			case 1:
    				printf("\n");
    				printf("Enter a number to enqueue:");
    				scanf("%d", &num);
    				enqueue(&q, num);
    				break;
    			case 2:
    				dequeue(&q);
    				break;
    			case 3:
    				printArr(&q);
    				printf("\n");
    				break;
    			case 4:
    				empty(&q);
    				break;
    			case 5:
    				length(&q);
    				break;
    			case 6:
    				freeArr(&q);
    				break;
    			case 0: 
    				break;
    		}
    }
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    freeArr() should be recursive. You need to call free() in each individual malloc()'d item.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    ok thanx

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess you still have to work on the logic of the queue.
    e.g.
    enqueue() : you check if the queue is full and if it is you realloc with the exact same size as it has been and then write past the end off the array.
    dequeue(): you check for empty() that returns true if arr is NULL but you never release arr ( set arr to NULL ) in that function.

    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out how method was called?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 10:43 AM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  3. accessing a variable from another method
    By larne in forum C++ Programming
    Replies: 14
    Last Post: 01-16-2009, 04:24 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM