Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!mcgill-vision!snorkelwacker!bu.edu!xylogics!merk!alliant!linus!think!barmar From: barmar@think.com (Barry Margolin) Newsgroups: comp.protocols.nfs Subject: Re: bug in sun-3/os4.1 rpc/xdr? Message-ID: <41587@think.Think.COM> Date: 15 Aug 90 23:04:39 GMT References: <90225.174053BACON@MTUS5.BITNET> Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 17 In article <90225.174053BACON@MTUS5.BITNET> BACON@MTUS5.BITNET (Jeffery Bacon) writes: >int *bla_1(oog) int *oog; { int erg = 5; printf("%d\n",*oog);return(&erg); } This is invalid C code. You are returning the address of an automatic variable outside the extent of the declaration. By the time the caller of bla_1 uses the returned pointer the stack frame has probably been overwritten. The "erg" variable must either have static duration (either declare it globally, or give it the "static" storage class specifier), or it must be allocated in the heap (i.e. with malloc()). In this case, malloc() is probably the wrong choice because you will never be able to free it. -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar