String memory problem

Hi,

If I have:

char msgbuf[1024];

and msgbuf contains something lets say "--ABCDEF";

Now in my program I do the following:

 while((msgbuf[0] != '-') &&(msgbuf[1] != '-') &&(strcmp((strdup(msgbuf+2)),newboundvalue)))

/*Where newboundvalue is "ABCDEF"  */

Now my question is :
strdup() returns a pointer to a new string that is a  dupli-
     cate  of the string pointed to by s1.The space for the new string is obtained using malloc(3C). (Source : Man pages)
Now how do I free  this memory ??? Because when I run purify
it says there is a memory leak due to the strdup command.

Any help will be appreciated.

Thanks
singhtajAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
piano_boxerConnect With a Mentor Commented:
First you need to store the pointer returnet from strdup

Modify the while-line to something like this:

char* pStr;
while((msgbuf[0] != '-') &&(msgbuf[1] != '-') &&(strcmp((pStr = strdup(msgbuf+2)),newboundvalue)))
{
    free(pStr);
    [.......]
}


0
 
ozoCommented:
Why are you using strdup at all if you're not going to be keeping the new string?
Why not just strcmp(msgbuf+2,newboundvalue)?
0
 
singhtajAuthor Commented:
Good Idea!!!

Thanks

0
All Courses

From novice to tech pro — start learning today.