K
Keith Thompson
Kenneth Brody said:[...]cr88192 said:Keith Thompson said:[...]
How do you portably guarantee proper alignment?
if grabbed from malloc, it is aligned to whatever malloc's alignment [...]
ifdef powers, ifdef fixes everything...
Hardly. If mmap isn't supported, you still have to find some
alternative.
on windows, I use malloc.
But wasn't the original question along the lines of "how can one
write a replacement for (not a wrapper around) malloc()"?
(Okay, it was actaully more along the lines of "which standard
library functions could be implemented by an end user, rather
than the implementor".)
Yes. Here's the original post, by (e-mail address removed):
| One interesting thing to come out of the recent "Alignment" thread is
| that it is impossible to write a portable replacement for malloc in
| "user space" (not sure what the right term is - I mean an ordinary
| programmer, not an implementor) - even a naive method using a large
| array isn't guaranteed to work if there's no way of having a variable
| of strictest alignment. Oh, for the sake of the pedants, let's
| discount
| void *my_malloc(size_t size) { return 0; }
|
|
| Of course, for most standard library functions, say something like
| strlen, it's perfectly possible to provide a completely equivalent
| implementation yourself.
|
| So as an academic exercise, which other standard library functions
| share the same property as malloc, that the ordinary programmer is
| powerless to write an equivalent function without dipping into non-
| Standard implementation details?
Obviously using mmap and using malloc (!) are not responsive to the
original question.
Thread topics do drift, and that's ok, but we shouldn't let them drift
away from *both* the topic of the newsgroup and relevance to the
original question.