[QUESTION]

Discussion in 'C Programming' started by parjit, Aug 24, 2010.

  1. parjit

    parjit Guest

    Does the following code create a memory leak?

    void main(){
    char *something;

    something=do_it("This is a test");
    free(something);

    }

    char *do_it(char *str){
    return(strdup(str));

    }

    I know that this example is idiotic, but it gets my question across, I
    hope.
    :)
     
    parjit, Aug 24, 2010
    #1
    1. Advertising

  2. parjit

    Seebs Guest

    On 2010-08-24, parjit <> wrote:
    > Does the following code create a memory leak?


    Maybe!

    .... but so far as I know, only because it invokes undefined behavior
    repeatedly.

    > void main(){


    Wrong.

    > char *something;
    >
    > something=do_it("This is a test");
    > free(something);
    > }


    > char *do_it(char *str){
    > return(strdup(str));
    > }


    You haven't included <string.h>, and in any event, strdup() isn't
    standard C.

    > I know that this example is idiotic, but it gets my question across, I
    > hope.
    >:)


    Not at all. Try putting the question into words, such as:
    If I allocate something in a function, and return the pointer,
    and free it in another function, does that create a memory leak?

    That would allow people to be more sure of what you were trying to do.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Aug 24, 2010
    #2
    1. Advertising

  3. parjit

    parjit Guest

    Seebs wrote:
    > Not at all. Try putting the question into words, such as:
    > If I allocate something in a function, and return the pointer,

    and free
    > it in another function, does that create a memory leak?
    >
    > That would allow people to be more sure of what you were trying to do.


    Actually my question was not from a negative experience, but as a result
    of a discussion with someone. I argued that tho it was a bad programming
    practice, it didn't necessarily create a memory leak by virtue of its
    syntax. I'm still not sure if I was right or not.
     
    parjit, Aug 25, 2010
    #3
  4. parjit

    Seebs Guest

    On 2010-08-25, parjit <> wrote:
    > Seebs wrote:
    >> Not at all. Try putting the question into words, such as:
    >> If I allocate something in a function, and return the pointer,

    > and free
    >> it in another function, does that create a memory leak?


    >> That would allow people to be more sure of what you were trying to do.


    > Actually my question was not from a negative experience, but as a result
    > of a discussion with someone. I argued that tho it was a bad programming
    > practice, it didn't necessarily create a memory leak by virtue of its
    > syntax. I'm still not sure if I was right or not.


    I don't see what you think is a bad programming practice.

    EXPLAIN your question, don't just expect us to guess it from your
    sample program. What was the discussion? What do you think is "bad"
    about it? What on earth do you mean by "by virtue of its syntax"? Syntax
    is not what creates memory leaks. And there is no memory leak in that
    code.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Aug 25, 2010
    #4
  5. parjit

    parjit Guest

    Seebs wrote:
    > EXPLAIN your question, don't just expect us to guess it from your sample
    > program. What was the discussion? What do you think is "bad" about it?
    > What on earth do you mean by "by virtue of its syntax"? Syntax is not
    > what creates memory leaks. And there is no memory leak in that code.


    Sorry I thought I made it clear... it was the return() statement I was
    referring to.
     
    parjit, Aug 25, 2010
    #5
  6. parjit

    Seebs Guest

    On 2010-08-25, parjit <> wrote:
    > Seebs wrote:
    >> EXPLAIN your question, don't just expect us to guess it from your sample
    >> program. What was the discussion? What do you think is "bad" about it?
    >> What on earth do you mean by "by virtue of its syntax"? Syntax is not
    >> what creates memory leaks. And there is no memory leak in that code.


    > Sorry I thought I made it clear... it was the return() statement I was
    > referring to.


    What about it?

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Aug 25, 2010
    #6
  7. parjit

    Mark Bluemel Guest

    parjit wrote:
    > Does the following code create a memory leak?


    No - why do you think it would?

    > void main(){
    > char *something;
    >
    > something=do_it("This is a test");
    > free(something);
    >
    > }
    >
    > char *do_it(char *str){
    > return(strdup(str));
    >
    > }
    >
    > I know that this example is idiotic, but it gets my question across, I
    > hope.


    No it doesn't - I strongly suggest you read
    <http://www.catb.org/esr/faqs/smart-questions.html>
     
    Mark Bluemel, Aug 26, 2010
    #7
  8. On 25 Aug, 20:05, parjit <> wrote:
    > Seebs wrote:


    > > EXPLAIN your question, don't just expect us to guess it from your sample
    > > program.  What was the discussion?  What do you think is "bad" about it?
    > >  What on earth do you mean by "by virtue of its syntax"?  Syntax is not
    > > what creates memory leaks.  And there is no memory leak in that code.

    >
    > Sorry I thought I made it clear... it was the return() statement I was
    > referring to.


    how were we supposed to guess you thought the return was a problem?
    Did you mention "return" in the text of any of your previous posts?

    Ok, so what do you think is wrong with the return statement?

    As it happens I don't like the brackets
    return (strdup (str));

    I'd code it
    return strdup (str);

    but that has no affect on the semantics (meaning) of the code
     
    Nick Keighley, Aug 26, 2010
    #8
  9. On 24 Aug, 21:32, parjit <> wrote:

    > Does the following code create a memory leak?


    The number of errors in your code reaches Schildt-like proportions.

    > void main(){


    main returns an int and an empty parameter list is indicated with
    void. I hate your layout.

    >     char *something;
    >
    >     something=do_it("This is a test");


    is your space bar broken? Function do_it() used before declared.
    >     free(something);


    free called without a prototype in scope. No return from main().

    > }
    >
    > char *do_it(char *str){
    >     return(strdup(str));


    unnecessary brackets. No prototype in scope for strdup()

    > }
    >
    > I know that this example is idiotic,


    oh yes!

    > but it gets my question across, I hope.


    no. We know you are worried about a memory leak but have no clue why.

    Your code should have looked something like this--

    #include <stdlib.h> /* free() */
    #include "some_header_for_strdup"

    char *do_it (char *str)
    {
    return strdup (str);
    }

    int main (void)
    {
    char *something;
    something = do_it ("This is a test");
    free (something);
    return 0;
    }
     
    Nick Keighley, Aug 26, 2010
    #9
    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. sean
    Replies:
    1
    Views:
    598
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    664
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    951
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    427
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    855
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page