Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > [QUESTION]

Reply
Thread Tools

[QUESTION]

 
 
parjit
Guest
Posts: n/a
 
      08-24-2010
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.

 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      08-24-2010
On 2010-08-24, parjit <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
parjit
Guest
Posts: n/a
 
      08-25-2010
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.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      08-25-2010
On 2010-08-25, parjit <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
parjit
Guest
Posts: n/a
 
      08-25-2010
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.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      08-25-2010
On 2010-08-25, parjit <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      08-26-2010
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>
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-26-2010
On 25 Aug, 20:05, parjit <(E-Mail Removed)> 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

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-26-2010
On 24 Aug, 21:32, parjit <(E-Mail Removed)> 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;
}




 
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




Advertisments