Memory Leaks related question

R

ramif

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

James Kuyper

ramif said:
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.
 
R

ramif

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

Richard

ramif said:
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.
 
M

Malcolm McLean

ramif said:
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.
 
J

James Kuyper

ramif said:
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;
}
 
R

ramif

ramif said:
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;
}
 
J

James Kuyper

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.
 
C

CBFalconer

ramif wrote: *** and top-posted. Fixed ***
ramif said:
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)
 
P

pete

ramif said:
Ok then, is their a way to solve the memory leak problem with respect to
my program (shown below)?
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 */
 
G

give_me_chance

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>

Thanx for useful links.its very useful for new person like me.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top