Memory Leaks related question

Discussion in 'C Programming' started by ramif, Dec 9, 2007.

  1. ramif

    ramif Guest

    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;
    }
     
    ramif, Dec 9, 2007
    #1
    1. Advertising

  2. ramif said:

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


    Yes.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 9, 2007
    #2
    1. Advertising

  3. ramif

    James Kuyper Guest

    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;
    > }


    Well, yes, obviously. You've done at two memory allocations, and didn't
    bother free()ing either one. Of course you have a memory leak.

    You also didn't bother checking whether the first allocation succeeded.
    If either allocation fails, the behavior of your code is undefined,
    because the behavior defined by the standard for strcpy() only has
    meaning if the second argument points to a null-terminated string, and
    the first argument points to a writable array long enough to hold a copy
    of that string, including it's terminating null character. With only a
    few exceptions, the standard does not normally require standard library
    function to check for null pointer arguments and handle them safely.
     
    James Kuyper, Dec 9, 2007
    #3
  4. ramif

    ramif Guest

    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;
    > }
     
    ramif, Dec 9, 2007
    #4
  5. ramif

    Richard Guest

    ramif <> writes:

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


    See below.

    >
    >
    > #include <string.h>
    > #include <stdlib.h>
    >
    >
    > char * foo()
    > {
    > char *str = malloc(200);
    >
    > if (str != NULL)
    > strcpy(str, "qwerty");
    >
    > return str;


    you return this str to main() but then forget to call free.

    > }
    >
    >
    >
    > int main(int argc, char *argv[])
    > {
    > char * myString = malloc(200);
    >
    > /******* Will a memory leak occures here? *******/
    > strcpy(myString, foo());


    Not only do you forget to free the return value from foo() you also
    forget to free the memory you created for myString.

    >
    > return 0;
    > }


    When the program exits the memory might well be returned by the death of
    the process. Or it might not.

    Use free.
     
    Richard, Dec 9, 2007
    #5
  6. "ramif" <> wrote in message
    > Ok then, is their a way to solve the memory leak problem with respect to
    > my program (shown below)?
    >
    >> #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;
    >> }


    foo() is fine. Very frequently you need to allocate a buffer or a structure
    in one function and return it to caller.
    However caller should either return the pointer to an even higher level or
    have a matching call to free().
    If your case this means storing the retunr value of foo() in a temporary,
    using it, then freeing it.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Dec 9, 2007
    #6
  7. ramif

    James Kuyper Guest

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

    ....

    Make sure that every call to malloc() with a non-null return value has a
    corresponding call to free():

    #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);
    if(myString==NULL)
    return EXIT_FAILURE;
    // From this point on, do not return without
    // first calling free(myString).

    int retval = EXIT_SUCCESS;
    char *foostring = foo();

    if(foostring)
    {
    strcpy(myString, foo());
    free(foostring);
    }
    else
    retval = EXIT_FAILURE;

    free(myString);

    return retval;
    }
     
    James Kuyper, Dec 9, 2007
    #7
  8. ramif

    ramif Guest

    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;
    > }



    Here I've decided to keep foo()'s returned value in memory and assign it
    to myString.

    My question is: Is that assign statement acceptable in C language?

    This question maybe trivial (and stupid) for you C experts, but it is
    crucial for me to understand these "low-level" programming techniques
    since I have always programmed using high level languages such as Java,
    C#, Haskell, Basic, Pascal...

    Thankx

    #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;

    myString = foo(); /*** is this line legal in ANSI C? ***/
    printf("%s", myString);

    free(myString);
    return 0;
    }
     
    ramif, Dec 9, 2007
    #8
  9. ramif

    James Kuyper Guest

    ramif wrote:
    ....
    > Here I've decided to keep foo()'s returned value in memory and assign it
    > to myString.
    >
    > My question is: Is that assign statement acceptable in C language?
    >
    > This question maybe trivial (and stupid) for you C experts, but it is


    This question is indeed trivial; that doesn't make it stupid. If you've
    just started learning C, asking trivial questions may be an essential
    part of your learning process. The stupid thing would be to not ask
    questions that you need the answer to.

    > crucial for me to understand these "low-level" programming techniques
    > since I have always programmed using high level languages such as Java,
    > C#, Haskell, Basic, Pascal...
    >
    > Thankx
    >
    > #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;
    >
    > myString = foo(); /*** is this line legal in ANSI C? ***/


    Yes.

    > printf("%s", myString);


    If myString is null at this point, that could have undefined behavior.
     
    James Kuyper, Dec 9, 2007
    #9
  10. ramif

    CBFalconer Guest

    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 from http://www.teranews.com
     
    CBFalconer, Dec 9, 2007
    #10
  11. ramif

    pete Guest

    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
     
    pete, Dec 10, 2007
    #11
  12. On Dec 10, 4:26 am, CBFalconer <> 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.
     
    give_me_chance, Dec 10, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason

    memory leaks question

    Jason, Dec 11, 2003, in forum: C++
    Replies:
    2
    Views:
    415
    Karl Heinz Buchegger
    Dec 11, 2003
  2. Christian Seberino
    Replies:
    4
    Views:
    491
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Oct 9, 2003
  3. Maxwell Hammer
    Replies:
    7
    Views:
    644
    Peter Hansen
    Jun 18, 2005
  4. Sven C. Koehler
    Replies:
    6
    Views:
    111
    Nobuyoshi Nakada
    Aug 20, 2008
  5. Replies:
    4
    Views:
    127
Loading...

Share This Page