[QUESTION]

P

parjit

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.
:)
 
S

Seebs

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

and in any event said:
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
 
P

parjit

Seebs said:
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.
 
S

Seebs

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
 
P

parjit

Seebs said:
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.
 
N

Nick Keighley

Seebs wrote:

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
 
N

Nick Keighley

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top