LWN.net Logo

multiple returns in C

multiple returns in C

Posted Jan 28, 2010 10:04 UTC (Thu) by dgm (subscriber, #49227)
In reply to: multiple returns in C by thedevil
Parent article: LCA: Static analysis with GCC plugins

Another idiom I often use is separating resource allocation from actual computation:

int foobar1 (some_type * some_pointer)
{
  int result = foo (some_pointer);
  if (result < 0)
    result = bar (some_pointer, result);
  return result;
}

int foobar()
{
  int result = -1;
  void * some_pointer = malloc (sizeof (some_type));
  if (some_pointer) {
    result = foobar1 (some_pointer);
    free (some_pointer);
  }
  return result;
}


(Log in to post comments)

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds