Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!rutgers!uwm.edu!lll-winken!ames!dftsrv!jagmac2.gsfc.nasa.gov!jim From: jim@jagmac2.gsfc.nasa.gov (Jim Jagielski) Newsgroups: comp.unix.aux Subject: Re: stat(), lstat() again. Message-ID: <3430@dftsrv.gsfc.nasa.gov> Date: 17 Sep 90 11:17:31 GMT References: <3422@dftsrv.gsfc.nasa.gov> <4038@ursa-major.SPDCC.COM> Sender: news@dftsrv.gsfc.nasa.gov Reply-To: jim@jagmac2.gsfc.nasa.gov (Jim Jagielski) Organization: NASA Goddard Space Flight Center Lines: 57 In article <4038@ursa-major.SPDCC.COM> dyer@ursa-major.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? > In the 1st case, before the call, sbuf is set to NULL, thus ensuring that it points to nothing, so my comment that it is pointing somewhere dangerous is wrong (this setting of sbuf IS done in my program but was NOT included in my posting... sorry). The following WILL work (although it may NOT be portable and is NOT mentioned in K&R): int *pint; pint = NULL; *pint = 10; Now some C compilers (such as GreenHills and Vax-C) will accept lstat("/unix", sbuf) and some won't. ALL will accept lstat("/unix",&sbuf) (assuming, of course, that sbuf is defined correctly, 'natch). As was mentioned in a mail message to me, some compilers may push a pointer to the struct in both cases, (although this does NOT adhere to the X3J11 standard, which says that when a structure is passed, the function gets an IMAGE of the structure). Therefore, in the compilers that DO accept this, the function is either getting a pointer or else the original structure. (observe that lstat expects a pointer to a struct) In any case, the second method (passing &sbuf) IS portable and is standard. This is NOT, however, picked up by lint... PS: This program was a port from VaxC, which ran it with no problem. It was and old piece of code which I did very quick-and-dry... PPS: I understand C quite well thank you... that is, when my brain isn't mush (which lately seems QUITE often :) -- ======================================================================= #include =:^) Jim Jagielski NASA/GSFC, Code 711.1 jim@jagmac2.gsfc.nasa.gov Greenbelt, MD 20771 "Kilimanjaro is a pretty tricky climb. Most of it's up, until you reach the very, very top, and then it tends to slope away rather sharply."