[fpc-devel] cmem not aligning memory

Jonas Maebe jonas.maebe at elis.ugent.be
Tue Apr 6 14:15:37 CEST 2010


On 06 Apr 2010, at 13:49, Michael Schnell wrote:

> On 04/06/2010 01:07 PM, Jonas Maebe wrote:
>>
>> No, calloc simply zeroes the allocated memory.
>
> So why does it provide a size argument ?

One argument I know if is to prevent overflows, because size_t is an  
unsigned type while most multiplications are evaluated using signed  
arithmetic, but then one could ask why not do the same for malloc (the  
reply to that is that calloc is more often used for arrays, and arrays  
more often need to be zero than random data structures --see also  
dynamic arrays and ansistrings in Pascal vs other types-- but it's not  
that convincing to me).

Anyway, the reason for the two arguments is definitely completely  
unrelated to alignment, because
1) size does not define the alignment requirements. A struct  
containing 16 "1 byte" fields has an alignment requirement of 1, while  
a 16 byte vector usually has an alignment requi

2) the official standard also says that the result of calloc has to be  
assignable to any other pointer type: http://www.opengroup.org/onlinepubs/000095399/functions/calloc.html 
  ("The pointer returned if the allocation succeeds shall be suitably  
aligned so that it may be assigned to a pointer to any type of object")


Jonas



More information about the fpc-devel mailing list