So making your own malloc backed by a static char array is undefined behavior?
So making your own malloc backed by a static char array is undefined behavior?
The "effective type" (this is a term defined in the standard) of the char array elements would be "char", whereas the memory returned from malloc() is considered to be an object that initially has no effective type. I don't know of any way to take the char array and "erase" its effective type so that it can be used generically, like the value returned from malloc().
This is a problem!
Non-allocated memory (stack char) cannot be used like that. I'm sure you know alloc(), and malloc() is fast for small sizes.
Of course, what do you take me for? :)
> I'm sure you know alloc()
Do you mean alloca? It has a lot of problems, and is generally prohibited at Google.
> and malloc() is fast for small sizes.
Not nearly fast enough for my purposes.
> Yes, alloca. (Don't use it.)
> Just make a fast allocator that uses heap instead of the stack. You only need to malloc once and it can be used for any type since like you pointed out, it's effective type can be changed.
There is also the case where you have some raw data from a file or network, that you want to re-interpret as a struct. That is always dangerous with endianness and struct padding, but it is a very common practice. You could always memcpy from a char array to a struct, but that can waste memory.
At least that's the rationale for why we don't use it at Google.
Yep, blowing the stack will do that.
We implement our own allocators all the time. If you can't even do such a basic thing legally, then the rules are obvious nonsense.