This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: how to use malloc to reserve space for 1 million floats
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Anna Sidera <sidera dot anna at ucy dot ac dot cy>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 27 Aug 2012 12:06:16 +0100
- Subject: Re: how to use malloc to reserve space for 1 million floats
- References: <f50180873a5d3.503b6ad3@ucy.ac.cy>
On 27 August 2012 10: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.
malloc takes a size_t parameter, not an int, so it's an unsigned value
and can represent larger positive numbers than int.
Whether your system will allow an allocation that size is another
matter. It might be a problem on a 32-bit platform.