C
C. J. Clegg
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?
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?