how to use malloc to reserve space for 1 billion floats
Anna Sidera
sidera.anna@ucy.ac.cy
Tue Aug 28 11:18:00 GMT 2012
The system has 10 giga byte RAM.
The command:
uname -a
gives the following output:
Linux olympus 2.6.26-2-amd64 #1 SMP Wed Aug 19 22:33:18 UTC 2009 x86_64 GNU/Linux
I think this means that the system is 64 bit.
So you think that the command:
int myvariable = 1000;
float *myarray = malloc(pow(myvariable,3)*sizeof(float));
will work if pow(myvariable,3) is smaller than 2 billion?
Many Thanks,
Anna
----- Original Message -----
From: Ángel González <keisial@gmail.com>
Date: Monday, August 27, 2012 2:14 pm
Subject: Re: how to use malloc to reserve space for 1 million floats
> 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