Tag Info

New answers tagged

6

There's a key difference between managing memory as a resource (when you don't care what's inside and shouldn't even look), and using memory to do something else (when the contents are the whole point). ... memory administration is often better accomplished ... The quote is talking about managing memory addressing and maps, where you're treating memory ...


4

A typical use case (in hosted user-code applications) to cast some pointer to an intptr_t (from <stdint.h> standard C99 or C11 header) is to compute some hash code on that pointer: uint32_t hash_of_foo_ptr(struct foo_st*foo) { return (uint32_t) (((intptr_t)foo)*613) ^ ((intptr)foo%51043)); } For historical reasons (e.g. Linux came before C99), ...


2

Of course malloc() could return a fully typed pointer. The problem is, of what type? If it returned an int*, it would make you happy, but it would not make me happy, because I want a char* instead. Clearly, we could not both have it our way. So, void* is a good baseline which does not take anyone's side.


9

Because it can't. How would it return a fully typed pointer? C doesn't have templates/generics, and it doesn't allow function overloading; let alone overloading by return type only. So there's no mechanism to have a malloc that knew what type you wanted. I've read here that in older C compilers the type of pointer returned by these functions was char* ...



Top 50 recent answers are included