Path: utzoo!attcan!uunet!mcsun!ukc!icdoc!qmw-cs!liam From: liam@cs.qmw.ac.uk (William Roberts) Newsgroups: comp.unix.aux Subject: Re: stat(), lstat() again. Message-ID: <2808@sequent.cs.qmw.ac.uk> Date: 17 Sep 90 10:12:44 GMT References: <3422@dftsrv.gsfc.nasa.gov> <4038@ursa-major.SPDCC.COM> Organization: Computer Science Dept, QMW, University of London, UK. Lines: 37 In <4038@ursa-major.SPDCC.COM> dyer@spdcc.COM (Steve Dyer) writes: >In article <3422@dftsrv.gsfc.nasa.gov> jim@jagmac2.gsfc.nasa.gov (Jim Jagielski) writes: >]Well... if I do the following, lstat() doesn't work: >] struct stat *sbuf; >] lstat("/unix", sbuf); >]But this DOES: >] struct stat sbuf; >] lstat("/unix", &sbuf); >] >]It looks like in case #1, sbuf is pointing somewhere dangerous and when lstat >]is called, memory is destroyed... >OK, so where's the bug? You've not understood the C language. >Just where do you think sbuf is pointing to in the first example? >If you don't assign a value to a variable, how can you expect to >use its value as something meaningful? Or, slightly less harshly, you have some further nuances to learn about how to read the UNIX section2 & 3 manual pages. In particular, when you see pointers being used you must ask yourself "Who is allocating the memory for this?" - in the case of pointers passed as arguments, the answers is ALWAYS that the caller allocates the memory since C uses call by value. When you see pointers to pointer as arguments, or pointers as return values then there is a possibility that the called routine will do the memory allocation (e.g. getpwuid). True system calls ALWAYS use pointers (because they are small and easy to pass into the kernel) and ALWAYS require the user process to provide the memory. -- William Roberts ARPA: liam@cs.qmw.ac.uk Queen Mary & Westfield College UUCP: liam@qmw-cs.UUCP Mile End Road AppleLink: UK0087 LONDON, E1 4NS, UK Tel: 071-975 5250 (Fax: 081-980 6533)