Just to let you know the only function that I found related to this topics was malloc_size() which return the all size of the block you have reserved which include the housekeeping information(which as far as I could notice it varies its size) and therefore it is not useful for me.
More precisely: NO C standard library function to obtain allocated (by malloc) memory block size via pointer. May be any C implementations include such extented heap managementfunctions (it's true), but it's not a portable code, avoid this feature.
Sometimes it may be useful to design new codes (from the beginning) with user-defined memory allocator (kind of malloc wrapper). It's not a common way and as usually you can't use such wrappers with legacy codes.
No need to cast malloc returned pointer in C because of (void*) to (type*) conversion is legal in C (but not in C++.
> How is that a bad thing?
Because casting suppresses a much more serious error you would otherwise get if you failed to include stdlib.h in the first place.
Since C quietly converts the void* returned by malloc into the char* of the assigned pointer, the cast at best does nothing.
If you're getting complaints about void* conversions, then stop using C++ to compile your code, or start using new/delete for memory management.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.