Common malloc/free practice violates ANSI standard ?
Michael Meissner
meissner at twohot.rtp.dg.com
Wed Oct 18 00:15:18 AEST 1989
In article <1279 at virtech.UUCP> cpcahil at virtech.UUCP (Conor P. Cahill) writes:
| In article <1989Oct14.043811.669 at anucsd.oz>, bdm at anucsd.oz (Brendan McKay) writes:
| > No doubt this problem was unintentional. It could easily be fixed by a
| > sentence reading something like
| > "The pointer returned if the allocation succeeds is such that, if it is cast
| > to a pointer to any type of object and then that pointer is cast to type
| > void*, the original value is recovered."
|
| That is the meaning of the "suitably aligned so that it may be
| assigned to a pointer to any type of object and then used to access such an
| object..." (Section 4.10.3).
|
| An assignment of a void* pointer that is an invalid address for an OBJ*
| will usually cause a core drop at the point of dereference and may cause
| a core drop at the assignment. It *should* not modify the value of the
| pointer.
In implementations where there exist different pointer formats, the
conversion from void * to some pointer may in fact modify the value to
the new format. Both pointers would in fact be pointing to the same
spot in memory, but have different formats.
To give a concrete example, on the Data General MV computers, there
exist three types of pointers:
1) Bit pointers which take two 32 bit double-words (words are 16
bits to preserve upward migration from the 16 bit Eclipse),
one of which is a word pointer, and the other is a
non-negative offset from the word pointer to the desired bit.
The MV C compiler does not use this form, except for the
builtin bit functions bitset and bitvalue.
2) Byte pointers which take one 32 bit double-word. The segment
bits occupy the top three bits, and are usually all one's, due
to normal user code going in segment (ring) 7. The next 28
bits are the word address, and the bottom bit tells which byte
within the 16-bit word is being accessed.
3) Word pointers which take one 32 bit double-word. The top bit
is an indirection bit, only if the instruction specified
indirection, otherwise it is unused. The C compiler assumes
for pointers this is 0. The next three bits are the segment
of the pointer, and the remaining 28 bits are the word
address.
To get from a char * or void * pointer to a word pointer, the byte
pointer is shifted right one bit position, losing which particular
byte is pointing to. One of the checking modes of the compiler,
checks to see if indeed the bottom bit is 0 before doing the
conversion. The malloc library routine guarantees that it returns a
pointer that is suitably aligned (it aligns to a double word
boundary).
--
Michael Meissner, Data General. If compiles where much
Uucp: ...!mcnc!rti!xyzzy!meissner faster, when would we
Internet: meissner at dg-rtp.DG.COM have time for netnews?
More information about the Comp.lang.c
mailing list