st1 = (void *)malloc(5000) + 5000;
completely invalid too? You can't (I think it's undefined) increment a void pointer.
st1 = (void *)malloc(5000) + 5000;
completely invalid too? You can't (I think it's undefined) increment a void pointer.
My C is a bit rusty, but how else are you meant to allocate data of different types on the heap?
C++, however, does not allow this. So, if for some reason you're calling malloc rather than new in C++, you need to cast.
First, as someone else already pointed out, it can lead to an LP64 bug if you miss the declaration for malloc(), since int and caddr_t are not necessarily the same thing.
But the bigger problem is that casting to void* basically tells the compiler "stop all further type checking". That's not a big deal if the token "malloc" on that line never changes, but, in real codebases, allocators get changed all the time. If you change from foo_alloc() to bar_alloc(), the compiler will rightly freak out when you try to assign a foo* to a bar. But that (void) construction explicitly prevents that check from occurring, thus turning what should be a compile-time error into a runtime error.
edit: Though free()ing it would not work so well until you move the pointer back to the beginning of the 5000 bytes.