stat(), lstat() again.
Jim Jagielski
jim at jagmac2.gsfc.nasa.gov
Mon Sep 17 21:17:31 AEST 1990
In article <4038 at ursa-major.SPDCC.COM> dyer at ursa-major.spdcc.COM (Steve Dyer) writes:
>In article <3422 at dftsrv.gsfc.nasa.gov> jim at 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 <std/disclaimer.h>
=:^)
Jim Jagielski NASA/GSFC, Code 711.1
jim at 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."
More information about the Comp.unix.aux
mailing list