Click here to Skip to main content
15,889,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
#include<stdlib.h>
#include<ctype.h>  //include the necessary header files
struct queue
{
	int data;
	queue*next;	   //Declare the structure for queue
}*front=NULL,*rear=NULL,*temp;//create the pointer for the queue stack
int enqueue(int);
int display();
void deletelement();
void intialize();	//Initialize the functions
int main(int argc, char* argv[])
{		
	int choice;
	int element;
		
	while(1)
	{
	printf("\nEnter the choice \n1-Enqueue\n2-Display\n3-Dequeue\n4-Initialize\n5-Exit\n");
	scanf("%d",&choice);//Get the choice from the user
	
//	temp=new queue();
	//temp1=(queue*)malloc(sizeof(struct queue));
	switch(choice)
	{

	case 1:
		printf("Enter the data to be insert in to the queue");
		scanf("%d",&element);
	/*	if(isalpha(element))	//Get the element from the user
			{
				printf("Sorry you enter the wrong input");
			}
		else*/
		enqueue(element);		//Invoke the enqueue function
		break;
	case 2:
		display();				//Invoke the display function
		break;
	case 3:
		deletelement();			//Invoke the delete function
		break;
	case 4:
		intialize();
		break;
	case 5:
		exit(0);

	}

	}
		return 0;
}
int enqueue(int insertelement)
{

	temp=(queue*)malloc(sizeof(struct queue)*1024);
//realloc(temp,sizeof(queue));
	temp->data=insertelement;
	temp->next=NULL;
	if(front==NULL)
	{
		front=temp;
	}
	else
		rear->next=temp;
	rear=temp;
	//free(temp);
	return 0;
}
int display()
{
	
	temp=(queue*)malloc(sizeof(struct queue)*1024);
	//realloc(temp,sizeof(queue));
	temp=front;
	printf("i am ready\n");
	while(temp!=NULL)
	{
		printf("%d\t",temp->data);
		temp=temp->next;
	}
		free(temp);
	return 0;
}
void deletelement()
{
	temp=front;
	if(temp!=NULL)
	{
		front=front->next;
		free(temp);
	}
	else
		printf("\n The queue is already empty\n The Delete opearation can't be performed\n");
}

void intialize()
{
	
		//front=NULL;
		//rear=NULL;
		free(temp);
		
		free(front);
		
		free(rear);
		printf("\nThe queue has been reintialized\n");
		
		printf("hai");

}</ctype.h></stdlib.h></malloc.h></stdlib.h></conio.h></stdio.h>



this is my program.. once the vm mermory allocated..
but while i try to reintialize that queue then also the memory not yet comes low..
its still on increasing only..
Posted
Updated 23-Jun-11 0:24am
v2

Just because your allocated memory does not drop, does not mean that your app is not deleting memory, not for these small amounts. The task manager does not show you accurately how much memory is in use. VS will show you memory leaks when you stop debugging, is it showing any ? I'm not going to read all this code, have you set breakpoints to make sure that the right number of items are freed ? What reason do you have to learn C, which has some uses, but is mostly not used today because it does not support OO, and no framework for programming on any platform that is supported, uses it.
 
Share this answer
 
Comments
@BangIndia 23-Jun-11 6:34am    
Its my task... it have some powerful features for handling the networks.
so only i am studying that c....................
everything may be use in anywhere.
so we won't evaluate anything less.
R. Erasmus 23-Jun-11 6:46am    
Now thats just a dumb statement to make. "mostly not used today". Just because Code Project doesn't really cater for the C specialist doesn't mean that its obsolete. Check out http://www.devtopics.com/most-popular-programming-languages/ for a quick summary of its popularity. Mostly by embedded software engineers.
CPallini 23-Jun-11 7:50am    
Please don't down-vote based on a different philosophic point of view.
R. Erasmus 23-Jun-11 8:01am    
I tell you what... Make the statement public. See who votes what. I can vote as I please. Its my vote.
CPallini 23-Jun-11 8:12am    
Did you notice 'please'? :-D
It was just a suggestion, of course voting is up to you. Downvoting such a way, however, doesn't make your point stronger.

aimdharma wrote:
int display()
{

temp=(queue*)malloc(sizeof(struct queue)*1024);
//realloc(temp,sizeof(queue));
temp=front;
printf("i am ready\n");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
free(temp);
return 0;
}


This function is broken, since inner (useless) allocated memory is never released.
At the end of the loop, temp is NULL.

Remove both the
C
temp=(queue*)malloc(sizeof(struct queue)*1024);

and the
C
free(temp);

line.
 
Share this answer
 
Comments
@BangIndia 23-Jun-11 6:42am    
i accept the solution.. but i want to clear all the used memory..
i create memory in enqueue process.. i want to clear that memory...
CPallini 23-Jun-11 7:54am    
deletelement functions looks broken too. Cannot you use a STL container instead of creating your own (or, maybe, isn't time to read a good programming book, covering such topic?)?
@BangIndia 23-Jun-11 7:57am    
ok.......... i can't understand what you are trying to say to me..
any way thanks..
CPallini 23-Jun-11 8:01am    
If you can move to C++ then you may use a 'ready to use' container (like your queue) class provided by its standard library. C++ container classes are both robust and efficient.
On the other hand, if you still wish to develop your own container, using the C language, then you have to study a good book about, since your current implementation is poor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900