free function

This is a discussion on free function within the C Programming forums, part of the General Programming Boards category; hello friends I was writing a small program on stacks in which i used a pointer to an array to ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    free function

    hello friends I was writing a small program on stacks in which i used a pointer to an array to point the elements.Now I have used malloc and realloc function to allocate memory spaces and free function to deallocate. By using free function I want to deallocate one element of the array for pop() function.But the compile error shows that - "invalid conversion from int to void*".
    So whether free function cannot be used to deallocate single element of a dynamically allocated array by pointer, although i found that we can free individual element of an array declared explicitly without dynamic memory allocation.This was the program although I can't say if this program is totally logically correct.

    SO what is the possible way in this program to use free() function for an individual element??
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void push(int x);
    void pop();
    int top=-1;
    int *stack;
    int main()
    {
    	int resp;
    	int val;
    	while(1){
    		printf("\n\nFOR PUSH PRESS 1 OR FOR POP PRESS 2\n");
    		scanf("%d",&resp);
    		switch(resp)
    		{
    			case 1:
    				printf("\nVALUE TO BE PUSHED IN STACK:\t");
    				scanf("%d",&val);
    				push(val);
    				break;
    			case 2:
    				pop();
    				break;
    			default:
    				printf("\nINVALID RESPONSE");
    				break;
    		}
    		label:
    		printf("\n\nDo you want to continue again:\n");
    		printf("\nPRESS 1 FOR YES or 0 FOR NO\n");
    		scanf("%d",&resp);
    		if(resp==1)
    			;
    		else if(resp==0)
    			break;
    		else{ printf("\nINVALID RESPONSE");
    			goto label;
    		}
    	}
    	return 0;
    }
    void push(int x)
    {
    	if(top==-1)
    		top=1;
    	else
    		top++;
    	if(top==1)
    		stack=(int*)malloc(sizeof(int));
    	else
    		stack=(int*)realloc(stack,top * sizeof(int));
    	stack[top-1]=x;
    	return;
    }
    void pop()
    {
    	if(top==-1)
    	{
    		printf("\nSTACK IS EMPTY-CANNOT DELETE\n");
    		return;
    	}
    	else
    	{
    		top--;
    		free(stack[top]);
    	}
    }
    .

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The reason for that error is that you use "free(stack[top]);" which is not a pointer but the content at index "top" of the array stack. However, even if you did pass the address to free you can not free a single element of an array, free will need the base of the array since that is what you are using when you allocate memory.

    To do what you are aiming for here you need to create a linked list, or other similar data structure with individually allocated members with links to each other.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by Subsonics View Post
    The reason for that error is that you use "free(stack[top]);" which is not a pointer but the content at index "top" of the array stack. However, even if you did pass the address to free you can not free a single element of an array, free will need the base of the array since that is what you are using when you allocate memory.

    To do what you are aiming for here you need to create a linked list, or other similar data structure with individually allocated members with links to each other.
    Thanks for your answer, so here i do need to implement linked list.
    So free cannot be used to free an individual element of a block allocated using malloc or calloc or realloc. But its possible for an array,right?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You mean realloc the entire array to size -1 to perform a pop on the stack? Yeah I guess that should work too.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Actually i want to reallocate 1 more space incase it is not empty for push,and deallocate 1 space for pop.Although linked list seems to be the option left, but i have done it just using a simple array keeping enough space for the elements.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I understand that but that is what you are already doing in your push function. No need to mention that.

    Another option is to have a max size of your stack and keep track of the amount of elements in it somewhere, (index 0 for example).

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by rakeshkool27 View Post
    Thanks for your answer, so here i do need to implement linked list.
    So free cannot be used to free an individual element of a block allocated using malloc or calloc or realloc. But its possible for an array,right?
    NO.
    free can never be used for anything except the address that was obtained from calloc, malloc or realloc.
    You can never free anything from the middle or end of an array, you can only free the whole thing (see previous sentence).

    In your case instead of a linked-list you could look up and use a circular buffer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21