R
Romeo Colacitti
I have a need to make a custom quasi-memory allocator, and I remembered
a simple ons in K&R2. Looking at the code for it now, I think I notice
a "fault" in the design, and I was wondering if people would back me up
on this.
The design basically uses a pool of memory, allocated as a character
array. Pointers into the array are retured by the allocated function.
Isn't this very dangerous, as a char has very lenient memory alignment
requirements, which would allow the compiler to allocate the array
starting at a location that might not be a multiple of an address
required for other type. Also, pointers in the middle of the arrat
(after the first allocation) will certainly be unaligned for some
types.
My fix, is to use an array of ints (as they match the natural size for
my syste), and then use a pointer into this array of ints. When
returning the pointer value, I could cast it to (void *).
Isn't this much safer than the char array versoin?
Can someone correct me on this, or verify my line of thoughts? thx
a simple ons in K&R2. Looking at the code for it now, I think I notice
a "fault" in the design, and I was wondering if people would back me up
on this.
The design basically uses a pool of memory, allocated as a character
array. Pointers into the array are retured by the allocated function.
Isn't this very dangerous, as a char has very lenient memory alignment
requirements, which would allow the compiler to allocate the array
starting at a location that might not be a multiple of an address
required for other type. Also, pointers in the middle of the arrat
(after the first allocation) will certainly be unaligned for some
types.
My fix, is to use an array of ints (as they match the natural size for
my syste), and then use a pointer into this array of ints. When
returning the pointer value, I could cast it to (void *).
Isn't this much safer than the char array versoin?
Can someone correct me on this, or verify my line of thoughts? thx