What should "*nft = *(struct need_to_free *) ntf->addr;" do?

Ray Spalding cc100aa at prism.gatech.EDU
Fri Aug 24 05:20:15 AEST 1990


In article <1518 at athen.sinix.UUCP> pete at sinix.UUCP (Pete Delany) writes:
>    I just made a 100DM bet that the code found in heap_kmem.c
>    check_need_to_free() is correct where it copies a structure pointed to by
>    a structure onto the first structure with structure assignment:
>    {
>	struct need_to_free {
>		struct need_to_free	*addr;
>		int			nbytes;
>		} *ntf = &kmem_info.need_to_free_list;
>
>	*ntf = *(struct need_to_free *) ntf->addr;
>    }
> [...]
>    Ckecking out the ANSII Oct 31 '88 spec says:
>
>	"If the value being stored in an object is accessed from another 
>	object that overlaps in any way the storage of the first object, 
>	then the overlap shall be exact and the two objects shall have
>	qualified or unqualified versions of a compatible type; 
>	otherwise the behavior is undefined"

It seems to me that the code is just fine, except:
(1) As was pointed out in another thread today, there's no guarantee
that "kmem_info.need_to_free_list" is represented the same as "struct
need_to_free" unless it is defined AS a "struct need_to_free".  That
is, distinct structs defined with similar source code may not in fact
have the same layout in memory.  But, one can probably get away with it
in this case most times.
(2) The cast (struct need_to_free *) seems redundant-- isn't that the
declared type of ntf->addr?  Couldn't that line just read
"*ntf = *ntf->addr;"?

Your reference to the standard seems irrelevant here.  The value being
stored does not overlap the object being stored into at all-- the two
objects are in two different, non-overlapping areas of memory (unless
the pointers have been corrupted).  What the code is doing is just
using the value of an object in computing it's own new value, not
unlike "i = i + 1".  (We'd write that differently, of course).
-- 
Ray Spalding, Technical Services, Office of Information Technology
Georgia Institute of Technology, Atlanta Georgia, 30332-0275
uucp:     ...!{allegra,amd,hplabs,ut-ngp}!gatech!prism!cc100aa
Internet: cc100aa at prism.gatech.edu



More information about the Comp.lang.c mailing list