String memory problem

Posted on 1998-04-17
Last Modified: 2010-04-15

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.

Question by:singhtaj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

piano_boxer earned 50 total points
ID: 1249465
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)))

LVL 84

Expert Comment

ID: 1249466
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)?

Author Comment

ID: 1249467
Good Idea!!!



Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Console based application in Linux 1 105
UPD maximums on Red Hat 6 130
IIS Log files on Exchange 2013 server 6 330
C++ :Change value from  DisableCMD registry 4 79
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question