Returning automatic pointers always bad?
- From: C. J. Clegg <answer.in.newsgroup@xxxxxxxxx>
- Date: Fri, 27 Feb 2009 22:14:59 -0500
Consider the following:
char *foo( void )
{
char *bar = (char *)NULL;
bar = malloc( 40 );
return bar;
}
int main( void )
{
char *foobar = (char *)NULL;
foobar = foo();
doSomethingWith( foobar );
free( foobar ); // safe even if foobar is NULL
foobar = (char *)NULL;
return 0;
}
Now, foo( ) is returning an automatic pointer, which as we all know
goes out of scope as soon as foo( ) returns. In this case, that
should be OK, right?... because that pointer is assigned to another
pointer (foobar in main()) which continues to point to valid heap
memory even if bar goes out of scope.
Right?
.
- Follow-Ups:
- Re: Returning automatic pointers always bad?
- From: Kaz Kylheku
- Re: Returning automatic pointers always bad?
- From: nick_keighley_nospam
- Re: Returning automatic pointers always bad?
- From: Keith Thompson
- Re: Returning automatic pointers always bad?
- From: pete
- Re: Returning automatic pointers always bad?
- From: Ben Bacarisse
- Re: Returning automatic pointers always bad?
- From: Richard Heathfield
- Re: Returning automatic pointers always bad?
- Prev by Date: Re: header
- Next by Date: Re: Must a function pointer have a prototype in C
- Previous by thread: header
- Next by thread: Re: Returning automatic pointers always bad?
- Index(es):
Relevant Pages
|