malloc more than 2GB ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

frankp
Posts: 228
Joined: Sun Mar 12, 2006 3:11 pm

malloc more than 2GB ?

Post by frankp »

gcc version 4.3.2 (Debian 4.3.2-1.1)

On a 64bit system is it possible to get gcc to malloc more than 2GB, which seems to be the limit?
Volker Annuss
Posts: 180
Joined: Mon Sep 03, 2007 9:15 am

Re: malloc more than 2GB ?

Post by Volker Annuss »

Hello Frank,

it works here with Ubuntu 8.10, gcc 4.3.2.

Code: Select all

#include <stdlib.h>
#include <stdio.h>

int main&#40; int argc, char *argv&#91;&#93; )
&#123;
	void *p = malloc&#40; 0x100000001LL );
	printf&#40; "%p\n", p );
	exit&#40; 0 );
&#125;
frankp
Posts: 228
Joined: Sun Mar 12, 2006 3:11 pm

Re: malloc more than 2GB ?

Post by frankp »

Thanks. It works for me too.

I forgot I was making space for a record each of 12 bytes. How embarrassing.....
frankp
Posts: 228
Joined: Sun Mar 12, 2006 3:11 pm

Re: malloc more than 2GB ?

Post by frankp »

frankp wrote:Thanks. It works for me too.

I forgot I was making space for a record each of 12 bytes. How embarrassing.....
Perhaps not.

I have effectively:

num = (memory) / sizeof (recordT);
p = (recordT*) malloc (num * sizeof(recordT));

If memory is >2GB then if fails - NULL returned.
User avatar
Zach Wegner
Posts: 1922
Joined: Thu Mar 09, 2006 12:51 am
Location: Earth

Re: malloc more than 2GB ?

Post by Zach Wegner »

frankp wrote:
frankp wrote:Thanks. It works for me too.

I forgot I was making space for a record each of 12 bytes. How embarrassing.....
Perhaps not.

I have effectively:

num = (memory) / sizeof (recordT);
p = (recordT*) malloc (num * sizeof(recordT));

If memory is >2GB then if fails - NULL returned.
Is memory a signed int perchance? Then you would be trying to malloc negative memory.
frankp
Posts: 228
Joined: Sun Mar 12, 2006 3:11 pm

Re: malloc more than 2GB ?

Post by frankp »

No...
I tried num = (3000 * 1000000) / sizeof (recordT), where num is uint64

num = (2000 * 1000000) / sizeof (recordT), follow by the malloc is fine.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: malloc more than 2GB ?

Post by bob »

frankp wrote:gcc version 4.3.2 (Debian 4.3.2-1.1)

On a 64bit system is it possible to get gcc to malloc more than 2GB, which seems to be the limit?
Doesn't seem to be an issue on my linux-64 boxes:

White(1): hash=8192m
hash table memory = 6144M bytes.

Been running with big hashes in things like CCT for years.
User avatar
Zach Wegner
Posts: 1922
Joined: Thu Mar 09, 2006 12:51 am
Location: Earth

Re: malloc more than 2GB ?

Post by Zach Wegner »

frankp wrote:No...
I tried num = (3000 * 1000000) / sizeof (recordT), where num is uint64
If you had it exactly as written, it would cause issues. The integer literals are signed ints, so the multiplication gets performed (which overflows the signed int and is thus negative), then the division, and then when the whole thing is casted to an unsigned 64 bit by sign extension. The result is still negative.
User avatar
Zach Wegner
Posts: 1922
Joined: Thu Mar 09, 2006 12:51 am
Location: Earth

Re: malloc more than 2GB ?

Post by Zach Wegner »

Oops, of course, the result is not negative. But the result is sign extended to 64 bits, which gives 2^64-3 billion. I just confirmed this with an actual program...
frankp
Posts: 228
Joined: Sun Mar 12, 2006 3:11 pm

Re: malloc more than 2GB ?

Post by frankp »

Zach Wegner wrote:Oops, of course, the result is not negative. But the result is sign extended to 64 bits, which gives 2^64-3 billion. I just confirmed this with an actual program...
Thanks Zach. I just cast the numbers to unsigned and it works fine.