Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

String memory problem

Posted on 1998-04-17
3
Medium Priority
?
216 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:singhtaj
3 Comments
 
LVL 4

Accepted Solution

by:
piano_boxer earned 200 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)))
{
    free(pStr);
    [.......]
}


0
 
LVL 85

Expert Comment

by:ozo
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)?
0
 

Author Comment

by:singhtaj
ID: 1249467
Good Idea!!!

Thanks

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

824 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