Returning automatic pointers always bad?




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?

.



Relevant Pages

  • Re: Insert with response
    ... FooBar, there's no way and no need to put them in synch. ... column in the foo table to 250 calumns in the bar table. ... set statistics time off ...
    (microsoft.public.sqlserver.programming)
  • Re: Factory or Inheritance for Initialisation?
    ... Class Bar doesnt need the MaxCount, so it must set the base ... Problem is MaxCount needs to be set for the two types Foo Bar. ... application initialization time, I need two FooBar classes setting up, ...
    (comp.object)
  • Re: Factory or Inheritance for Initialisation?
    ... Originally the inheritance tree used template method to define the ... As far as FooBar is concerned, Foo and Bar have the same ...
    (comp.object)
  • Re: 2 Newbie questions
    ... When I have an Class FooBar implementing the interfaces Foo and Bar ... void doSomething ... running instance myself when the user doubleclicks on a file ... ...
    (comp.lang.java.programmer)
  • Re: Returning automatic pointers always bad?
    ... char *foo(void) ... char *bar = NULL; ... doSomethingWith(foobar); ...
    (comp.lang.c)