You ask malloc for an amount of memory, so you already know the size.
Having said that, malloc will have internal housekeeping to track memory blocks and sizes, but there's no way to access that housekeeping data.
If you want a way to keep the allocated amount next to the pointer, take a look at Sean Barrett's stretchy buffer
implementation, in which the capacity and element count of the array are stored right before the array.
When doing this: int BmemSize = sizeof((char*)Bmem) * 50000; <--- (=400000)
, what you're computing is [m]sizeof(char*) * 50000[\m], which in a 64bit architecture will result in 8 * 50000. This [m]sizeof[\m] won't return the size of the allocation.
You can write past the end of the array because at the OS level, memory allocation granularity is at the page size (e.g. 4KB, 2MB, 1GB), so even if malloc gives you a 2KB block of memory, you can write past the end of the array without a memory protection fault.
Casey does a more detailed explanation in Handmade Hero while implementing the memory arena.