Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


String memory problem

Posted on 1998-04-17
Medium Priority
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 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)))

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

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

715 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