Victor said:
Consider I want to write my own version of standard malloc, calloc,
realloc, free. How can I portably check if they work correctly? The most
remarkable will be the test for right justification of each C type
dynamicly allocated.
You can't check alignment (which I assume you mean by "justification")
portably. This is one of the impediments to writing the *alloc family
in standard C. You may be able to use standard C constructs to test
alignment on a particular platform, perhaps by converting the retured
pointer to an integer type and testing divisibility (with the modulus
operator or a bitmask).
#include <stdio.h>
int aligned(void *p, int boundary) {
return !((long)p % boundary);
}
int main() {
double d;
char c;
int x;
void *pointers[] = {&d, &c, &x, 0}, **p = pointers;
int alignments[] = {32, 16, 8, 4, 2, 0}, *i;
for (; *p; p++)
for (i = alignments; *i; i++)
printf("%p is %saligned on a %d-byte boundary\n",
*p, (aligned(*p, *i) ? "" : "not "), *i);
return 0;
}
Of course, you only need to check that the pointer returned by your
implementation of malloc is correctly aligned for /any/ type, i.e. it
must satisfy most stringent alignment requirements on the target
platform.
Jeremy.