I have the following code:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
typedef uint64_t Huge_num; // 8 bytes
typedef Huge_num * Huge_Arr; // Array of 8 byte elements
using namespace std;
// Function: Return a huge array
static Huge_Arr* requestMem(int n)
{
cout << "requesting :"
<< (sizeof(Huge_num)*n)/(1024*1024*1024)
<< "GB" << endl;
Huge_Arr *buff;
try {
buff = new Huge_Arr[n];
}
catch (bad_alloc){
cout << "Not enough mem!" << endl;
exit(-1);
}
return buff;
}
// Main
int main(){
for (int i=1; i< 10; i++){
Huge_Arr *mem = requestMem(i*90000000);
delete mem;
}
}
For some reason malloc is only able to grab no more than 2GB of memory before throwing bad_alloc()
error.
I understand that on 32-bit systems, the maximum address space is indeed on same order of 2GB.
But I am using Ubuntu 12.04 x86_64, which should be able to handle larger memory requests, right?
EDIT: Turns out the answer was I was compiling with g++ -std=c00x
, which is 32-bit? Not sure, either way I changed the Makefile to remove the -std
flag and it compiled fine