Re: [Dev-C++] Re: malloc , extra bytes ! Somewhat urgent !
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-02-24 03:51:33
|
Stop bothering about the size of the returned memory block.
The allocation functions are only required to return AT LEAST the required
amount of memory, or a NULL pointer.
Internally, they normally add 8-16 byte of internal header for
book-keeping on the heap, i.e. to allow the heap manager to walk free and
used memory block.
Besides, most heap managers performs one of the following:
- rounds each block up to the nearest 8 or 16 byte increment.
- allocates the nearest larger 2^n size.
- uses the buddy system and allocates the nearest larger block size.
As a result - for allocation of very small objects, it might often
be better to allocate a bunch of objects at a time, and keep pool of
evailable objects in a list.
In the example below, the calloc call MUST use sizeof(BYTE). The
statement calloc(1000,BYTE) doesn't make sense.
/Per W
On Mon, 24 Feb 2003, Danny Roelofs wrote:
> Hi,
>
> Well i dont realy know what youre doing with it.. and i never used calloc but it seems your allocating memory for 1000 bytes
> and a additional 8 bytes.. sizeof(byte) reserves 8 bytes in my opinion. is it perhaps that you have to do this:
>
> calloc(1000,BYTE) instead of calloc(1000,sizeof(BYTE))
>
> Well i actualy dont know.. i am just programming in C for just a month or so..
>
>
> ----- Original Message -----
> From: veenurs
> To: dev...@li... ; ra...@ww...
> Sent: Monday, February 24, 2003 1:16 AM
> Subject: [Dev-C++] Re: malloc , extra bytes ! Somewhat urgent !
>
>
>
>
> Hi !
> It seems too simple .But what is it !?
> if( (buffer = ( PBYTE )calloc( 1000 ,sizeof(BYTE) )) == NULL ){
> ------
> -------
> }
> size = _msize( buffer );
>
> The size I get it is not 1000 , but 1008 !Same for malloc. Any subsequent call to realloc does not add the 8 bytes,though .
>
> [ Dev Cpp Version 4.9.7.0 ]
> What's happening ?
> BTW - "Generate debugging info" is OFF .
> Thanks in advance .
>
> -Rahul.
>
>
>
|