public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* how to use malloc to reserve space for 1 million floats
@ 2012-08-27 11:16 Anna Sidera
  2012-08-27 16:15 ` Jonathan Wakely
  2012-08-27 16:23 ` Ángel González
  0 siblings, 2 replies; 5+ messages in thread
From: Anna Sidera @ 2012-08-27 11:16 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use malloc to reserve space for 1 million floats
  2012-08-27 11:16 how to use malloc to reserve space for 1 million floats Anna Sidera
@ 2012-08-27 16:15 ` Jonathan Wakely
  2012-08-27 16:23 ` Ángel González
  1 sibling, 0 replies; 5+ messages in thread
From: Jonathan Wakely @ 2012-08-27 16:15 UTC (permalink / raw)
  To: Anna Sidera; +Cc: gcc-help

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use malloc to reserve space for 1 million floats
  2012-08-27 11:16 how to use malloc to reserve space for 1 million floats Anna Sidera
  2012-08-27 16:15 ` Jonathan Wakely
@ 2012-08-27 16:23 ` Ángel González
  2012-08-28 11:18   ` how to use malloc to reserve space for 1 billion floats Anna Sidera
  1 sibling, 1 reply; 5+ messages in thread
From: Ángel González @ 2012-08-27 16:23 UTC (permalink / raw)
  To: Anna Sidera; +Cc: gcc-help

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...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use malloc to reserve space for 1 billion floats
  2012-08-27 16:23 ` Ángel González
@ 2012-08-28 11:18   ` Anna Sidera
  2012-08-28 13:12     ` Ángel González
  0 siblings, 1 reply; 5+ messages in thread
From: Anna Sidera @ 2012-08-28 11:18 UTC (permalink / raw)
  To: Ángel González; +Cc: gcc-help

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...
> 
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to use malloc to reserve space for 1 billion floats
  2012-08-28 11:18   ` how to use malloc to reserve space for 1 billion floats Anna Sidera
@ 2012-08-28 13:12     ` Ángel González
  0 siblings, 0 replies; 5+ messages in thread
From: Ángel González @ 2012-08-28 13:12 UTC (permalink / raw)
  To: Anna Sidera; +Cc: gcc-help

On 28/08/12 05:58, Anna Sidera wrote:
> 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.
It does.

> 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
Yes. (the 2+e9 limit being roughly for the 7,45GB needed, not due to
malloc, to which you
could ask for even bigger amounts )

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-08-28 11:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-27 11:16 how to use malloc to reserve space for 1 million floats Anna Sidera
2012-08-27 16:15 ` Jonathan Wakely
2012-08-27 16:23 ` Ángel González
2012-08-28 11:18   ` how to use malloc to reserve space for 1 billion floats Anna Sidera
2012-08-28 13:12     ` Ángel González

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).