Re: [Dev-C++] Re: malloc , extra bytes ! Somewhat urgent !
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2003-02-24 18:04:58
|
Hello!
veenurs wrote:
> if( (buffer = ( PBYTE )calloc( 1000 ,sizeof(BYTE) )) == NULL ){
> }
> size = _msize( buffer );
> The size I get it is not 1000 , but 1008 !Same for malloc.
From the C Runtime Library Reference:
----
Syntax
#include <malloc.h>
size_t _msize(void *block);
Description
Returns the size of a heap block.
_msize returns the size of the allocated heap block whose address is
block. The block must have been allocated with malloc, calloc, or
realloc. The returned size can be larger than the number of bytes
originally requested when the block was allocated.
Return Value
_msize returns the size of the block in bytes.
----
Especially take care of this sentence:
The returned size can be larger than the number of bytes originally
requested when the block was allocated.
I suppose the reason for this is because it would take longer to give
you the EXACT amount of memory, so it gives the amount that is nearly
(and at least) what you need, but what can be given the fastest - only
what I think.
But you should always use the REQUESTED amount, not the actually
RECIEVED amount. If calloc doesn't return NULL, then the requested
amount is at your hand for sure.
Anyway, why is it a problem if you have more memory? If I get more money
for example, I would rather keep it, than complaining about it :)
--
Greetings,
Balázs
|