0

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;
    }
Karai
  • 298
  • 2
  • 6
  • 14
  • incompletely specified -- what's frontptr for instance? – necromancer Nov 21 '11 at 09:58
  • 2
    You do set `frontptr` and `rearptr` to `NULL` before manipulating the pointers? – Some programmer dude Nov 21 '11 at 10:13
  • @pileborg: I initialized frontptr and rearptr as static variables.so they will be initialized as NULL. – Karai Nov 21 '11 at 10:15
  • 2
    You cannot guarantee it. They're supposed to be initialized as NULL, but it's always good practice to initialize them yourself. For example, you could have a problem with tour `QueueInsert` if`rearptr` is NULL and `frontptr`is not NULL. – Victor Nov 21 '11 at 10:26
  • I have just remembered the existence of a marvellous piece of software, it's called valgrind and it might help you. – Victor Nov 21 '11 at 10:30

1 Answers1

3

This might be your problem:

tempMsgContent = frontptr;
tempMsgContent->nextptr = NULL;
frontptr = frontptr->nextptr;

You set the nextptr of tempMsgContent to NULL, which is good, but frontptr and tempMsgContent still point to the same memory location!

Set the nextptr to NULL after changing the frontptr.

Edit Checking through your QueueInsert code, I see this line:

Message_content *tempMsgContent  = (Message_content*) malloc(sizeof(Message_content*));

What that does is allocate memory enough for a pointer (sizeof(Message_Content*))! You should use sizeof(Message_Content) without the asterisk. This will certainly mess upp your heap!

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621