Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Memory Leaks related question

Reply
Thread Tools

Memory Leaks related question

 
 
pete
Guest
Posts: n/a
 
      12-10-2007
ramif wrote:
>
> Ok then, is their a way to solve the memory leak problem with respect to
> my program (shown below)?
>
> ramif wrote:
> >
> > Will there be a memory leak at strcpy(myString, foo()) or not???
> >
> >
> >
> > #include <string.h>
> > #include <stdlib.h>
> >
> >
> > char * foo()
> > {
> > char *str = malloc(200);
> >
> > if (str != NULL)
> > strcpy(str, "qwerty");
> >
> > return str;
> > }
> >
> >
> >
> > int main(int argc, char *argv[])
> > {
> > char * myString = malloc(200);
> >
> > /******* Will a memory leak occures here? *******/
> > strcpy(myString, foo());
> >
> > return 0;
> > }


/* BEGIN new.c */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char *foo(void)
{
char *str = malloc(200);

if (str != NULL) {
strcpy(str, "qwerty");
}
return str;
}

int main(void)
{
char *myString = foo();

if (myString != NULL) {
puts(myString);
free(myString);
}
return 0;
}

/* END new.c */

--
pete
 
Reply With Quote
 
 
 
 
give_me_chance
Guest
Posts: n/a
 
      12-10-2007
On Dec 10, 4:26 am, CBFalconer <(E-Mail Removed)> wrote:
> ramif wrote: *** and top-posted. Fixed ***
> > ramif wrote:

>
> >> Will there be a memory leak at strcpy(myString, foo()) or not???

>
> >> #include <string.h>
> >> #include <stdlib.h>

>
> >> char * foo() {
> >> char *str = malloc(200);

>
> >> if (str != NULL)
> >> strcpy(str, "qwerty");
> >> return str;
> >> }

>
> >> int main(int argc, char *argv[]) {
> >> char * myString = malloc(200);

>
> >> /******* Will a memory leak occures here? *******/
> >> strcpy(myString, foo());
> >> return 0;
> >> }

>
> > Ok then, is their a way to solve the memory leak problem with
> > respect to my program (shown below)?

>
> Yes. Revise main:
>
> int main(int argc, char *argv[]) {
> char *p, *myString = malloc(200);
>
> strcpy(myString, (p = foo()));
> free(p); free(myString);
> return 0;
>
> }
>
> Please do not top-post. Your answer belongs after (or intermixed
> with) the quoted material to which you reply, after snipping all
> irrelevant material. I fixed this one. See the following links:
>
> <http://www.catb.org/~esr/faqs/smart-questions.html>
> <http://www.caliburn.nl/topposting.html>
> <http://www.netmeister.org/news/learn2quote.html>
> <http://cfaj.freeshell.org/google/> (taming google)
> <http://members.fortunecity.com/nnqweb/> (newusers)
>
> --
> Merry Christmas, Happy Hanukah, Happy New Year
> Joyeux Noel, Bonne Annee.
> Chuck F (cbfalconer at maineline dot net)
> <http://cbfalconer.home.att.net>
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com- Hide quoted text -
>
> - Show quoted text -


Thanx for useful links.its very useful for new person like me.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard question: Any ideas why this code leaks memory? Sven C. Koehler Ruby 6 08-20-2008 08:31 PM
How should multiple (related) projects be arranged (structured) and configured so that they can share code, have a related package structure and enable proper unittesting, and ensuring no namespace collisions ToddLMorgan@gmail.com Python 14 04-21-2006 04:03 PM
How should threads be terminated? (related to 'Help with thread related tracebacks') Maxwell Hammer Python 7 06-18-2005 04:20 PM
memory leaks question Jason C++ 2 12-11-2003 05:44 PM
C extension question about avoiding memory leaks with the object returned... Christian Seberino Python 4 10-09-2003 04:25 AM



Advertisments