When I try to delete the structure that I got from Queue.It will throw runtime heap error.
here is my sample.
/* "Message_content" structure hold the content of message and body. */
typedef struct msgContent
{
char *message;
char *body;
struct msgContent *nextptr;
}Message_content;
static Message_content *frontptr;
static Message_content *rearptr;
Message_content* msgContent;
msgContent = QueueDelete();
free(msgContent); //**error happens while calling this function**.
Here is my QueueDelete() function in c.
static Message_content* QueueDelete()
{
Message_content *tempMsgContent = NULL;
if(frontptr == NULL)
{
}
else if(frontptr==rearptr)
{
tempMsgContent = frontptr;
tempMsgContent->nextptr = NULL;
frontptr = rearptr = NULL;
}
else
{
tempMsgContent = frontptr;
frontptr = frontptr->nextptr;
tempMsgContent->nextptr = NULL; // rectify the problem said by philong
}
return tempMsgContent;
}
EDIT Add Queue Insert Function which will give meaning to my code.
here is my Queue Insert function in c
static int QueueInsert(char *message ,char *body)
{
Message_content *tempMsgContent = (Message_content*) malloc(sizeof(Message_content*));
if(rearptr == NULL && frontptr == NULL)
{
rearptr = tempMsgContent;
rearptr->message = message;
rearptr->body = body;
rearptr->nextptr=NULL;
frontptr = rearptr;
return 1;
}
rearptr->nextptr = tempMsgContent;
rearptr = rearptr->nextptr;
rearptr->body = body;
rearptr->message = message;
rearptr->nextptr = NULL;
return 1;
}