Click here to Skip to main content
15,887,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Given the following declarations:

<br />
size_t const ARRAY_ROWS = 2000000000; // 2,000,000,000<br />
typedef double DoubleArray_T[ARRAY_ROWS];<br />
<br />
...<br />
<br />
int main(void)<br />
{<br />
    DoubleArray_T * A1;<br />
}<br />


How can I allocate the array?

I've tried:
<br />
	A1 = new DoubleArray_T;<br />


Would anyone be kind enough to let me ask them C++ questions occasionally through Instant Messaging?

Sure would be helpful if I could...
Posted

If you really want to allocate an array of 2,000,000,000 doubles why not just do it directly?

double *A1 = new double[ ARRAY_SIZE ];


If you want to have a distinctly named large object bin the typedef and use

struct DoubleArray_T
{
    double data[ ARRAY_SIZE ];
};

DoubleArray_T *A1 = new DoubleArray_T;


instead. This makes the array behave more like an object than the typedef does. Remember a typedef only introduces an alias for another type and that pointers to arrays (as DoubleArray_T * was in your code) are really strange beasts unless you know what you're doing. In your original code new DoubleArray_T was returning a pointer to a double and you were trying to assign that to a DoubleArray_T *, which are different types.

Cheers,

Ash

PS: Edited to make it at least look English
 
Share this answer
 
v3
Do you really want to allocate almost 16 GB?
:)
 
Share this answer
 
Comments
RichardInToronto 9-Nov-10 13:30pm    
Yes, I actually want to do that!!! I'm stressing out this beautiful machine.
CPallini 9-Nov-10 13:57pm    
I believe you can hardly get such a huge amount of memory...
RichardInToronto 10-Nov-10 14:12pm    
The wonders of great masses of inexpensive memory, and a 64-bit OS, like Fedora 13!!! Hip-hip-hooray!
I wonder if mainframe developers would've been able to gobble up memory like that, fifteen years ago!

Io devo fare la spezza. Io devo guidari la machina. Dame la chiavvi, Grazie.
CPallini 10-Nov-10 14:31pm    
:-D
I've found a way.

This works, but I don't like it:
A1 = new DoubleArray_T[1];

Isn't there another way?
Using hard-coded values is never recommended.
There must be a better way...
 
Share this answer
 
you could do it the c way with malloc which gives information about its failing with returning a null pointer.

you just have to typecaste the returned void* (pointer) to you datatype and bingo (if its not null), if it is null you know that it isn't possible ^^

it would probably look sth like this:

void* poniter = malloc(sizeof(double) * 2000000000);

if( pointer == NULL) {
//FAILD HARD ^^;
return;
}


DoubleArray_T * A1 = (DoubleArray_T *) pointer;


hope it works even if it is crazy :)

(if you want to process a lot of data you could also put the file into ram with the linux /dev/shm ramfs folder (hold half the size of the aviable ram)
and process it partially , the speed wont be that much slower i guesss ;)<b></b>
 
Share this answer
 
I would recommend that you do something like what Aescleal (how do you pronounce that name?) suggested above.

But if you really want to do it this way (as an academic exercise), then you need a cast. Example:

C++
size_t const ARRAY_ROWS = 128;
typedef double DoubleArray_T[ARRAY_ROWS];

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    DoubleArray_T * arr = (DoubleArray_T*)new DoubleArray_T;
    for (int i=0; i<ARRAY_ROWS; i++)
    {
        (*arr)[i] = i * 0.321;
    }

  // ... more code here

}
 
Share this answer
 
v2
Comments
Aescleal 9-Nov-10 16:45pm    
It's pronounced Ashley - it's an old spelling.

Cheers,

Ash
Nish Nishant 9-Nov-10 19:37pm    
Ah, thanks :-)
CPallini 10-Nov-10 14:32pm    
Well, Ash was, at least an hint.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900