Another malloc question

Chip Salzenberg chip at ateng.com
Thu Oct 26 08:42:28 AEST 1989


According to rhg at cpsolv.UUCP (Richard H. Gumpertz):
>Suppose a system has one data type "X" that must be aligned on a 32-byte
>boundary, but all other objects can be aligned on a 2-byte boundary.
>The current standard seems to prohibit malloc from special-casing requests
>for less than size X and returning only even alignment.

This feature serves a useful purpose.  Suppose you have pointers to large
structures, and you need more than one special pointer value.  NULL isn't
enough.  You can get a properly aligned structure pointer by calling
malloc(1); use this pointer as the magic cookie.  You can't dereference such
a pointer, but you can assign it and compare against it.

Such a ploy can save you some memory, since you don't have to declare a
static structure just to use its unique address as a magic cookie.

[I hope that this strategy doesn't run afoul some obscure restriction...]
-- 
You may redistribute this article only to those who may freely do likewise.
Chip Salzenberg at A T Engineering;  <chip at ateng.com> or <uunet!ateng!chip>
"'Why do we post to Usenet?'  Naturally, the answer is, 'To get a response.'"
                        -- Brad "Flame Me" Templeton



More information about the Comp.std.c mailing list