0

Im curious about an explanation for the following example:

void                                         
release(ptr *foo) {
     /* ... */
}

int
main(int argc, char **argv)
{
    goto cleanup;
    ptr *foo = someFunctionAllocatingMemory();

cleanup:
    release(foo);

    return 0;
}

What will be the value of foo in the release function? Running that example on an ARM using gcc for compilation gave me NULL. Is that consistent for every architecture / compiler? Can someone explain what is going on here?

fliX
  • 773
  • 8
  • 24
  • Could you please explain what you mean by "gave me NULL"? – Chris Culter Jan 24 '15 at 10:03
  • 3
    Since you skipped the initialisation it will just be an uninitialised pointer and you will get undefined behaviour if you try to reference it. – Paul R Jan 24 '15 at 10:04
  • 1
    The lifetime of the `foo` object starts at the opening of the block where it is declared but the initialization is done when `foo` is declared, so in your case `foo` is not initialized when `release` is called. – ouah Jan 24 '15 at 10:09

0 Answers0