how to use malloc to reserve space for 1 million floats
Ángel González
keisial@gmail.com
Mon Aug 27 16:23:00 GMT 2012
On 27/08/12 11:40, Anna Sidera wrote:
> Hello,
>
> I want to use the following command:
>
> int myvariable = 1000;
> float *myarray = malloc(pow(myvariable,3)*sizeof(float));
>
> but I don't know if it will work because if sizeof(float) is equal to 4 then pow(myvariable,3)*sizeof(float) is equal to 4 billion which is larger than the maximum integer which is about 2 billion.
>
> Can you tell me what is the right way to create an array of pow(myvariable,3) floats?
>
> Many Thanks,
> Anna
How much memory do you have available?
The parameter to malloc is a size_t, you should have no problems
providing a size of 4000000000 in a 64 bit system.
If you're using a 32 bit system, then you will have problems stating
that size. But the address space is also smaller than that,
so you couldn't reserve so much memory*, even if you were able to
provide that number to malloc().
* using conventional methods. But seems silly not to be using a flat
address space nowadays...
More information about the Gcc-help
mailing list