G
Gordon Burditt
Another method is to supply the size of the target in
Why? Not for just carrying the block size in the pointer.
If incrementing p modifies only the address, and leaves alone the
block size, and decrementing q modifies only the address, and leaves
alone the block size, then the above will still work, without needing
an offset.
Passing q (not q - 10) directly to free() invokes undefined behavior,
and the fact that q contains a block size that is not accurate for
the address in q doesn't make the problem any worse. It's already
as bad as it gets.
You don't need an offset field.
Run-time bounds checking does require the offset field, but it
isn't a requirement for passing the block size in the pointer.
Gordon L. Burditt
You'd need an offset along with the base address and size. For
Why? Not for just carrying the block size in the pointer.
example, the following is valid:
char *p = malloc(100);
char *q = p + 10;
free(q - 10);
If the information needed by free() is going to be stored in the
pointer, then q has to retain enough information to get back to the
base address of the allocated object.
If incrementing p modifies only the address, and leaves alone the
block size, and decrementing q modifies only the address, and leaves
alone the block size, then the above will still work, without needing
an offset.
Passing q (not q - 10) directly to free() invokes undefined behavior,
and the fact that q contains a block size that is not accurate for
the address in q doesn't make the problem any worse. It's already
as bad as it gets.
Either the "address" field of q
points to the base of the allocated object, with an offset of 10, or
the "address" field points directly to p[10] and the offset is -10;
the latter scheme might make for more efficient code.
You don't need an offset field.
This also gives you the possibility of run-time bounds checking.
Run-time bounds checking does require the offset field, but it
isn't a requirement for passing the block size in the pointer.
Gordon L. Burditt