[GRASS-dev] Allocating memory for a huge array

Paul Kelly paul-grass at stjohnspoint.co.uk
Thu Mar 22 16:45:08 EDT 2007


On Thu, 22 Mar 2007, Paul Kelly wrote:

>> unsigned int * H_Array;
>> (...)
>> H_Array=(unsigned int *)G_calloc(2^32, sizeof(unsigned int));
>> 
>> because in this way (unsigned int) with 32 bits I can manage 4 billions 
>> cells, instead of 2 billions related to 32 bits as (traditional signed) 
>> int.
>
> I think you're confusing the size of the array data type with the size of the 
> array index. The array index is always a pointer of size size_t and can get 
> as big as the system allows a size_t to be.

Sorry just thought of a way I could explain this more clearly. The amount
of values you can have in the array is equal to sizeof(unsigned int *). 
This is exactly the same value as sizeof(int *), sizeof(short *), even 
sizeof(char *) - they are all pointers of size size_t. In other words, 
nothing to do with the data type that is actually stored in the array. I 
used to be terribly confused about this and indeed introduced some bugs 
into GRASS because of it* but am mostly over my confusion now ;)

Paul


* On 32-bit operating systems a size_t is 32 bits, but on 64-bit operating 
systems it is 64 bits - make sense? So maybe an AMD64 and a 64-bit 
compatible operating system is the solution to your problem?




More information about the grass-dev mailing list