How to output functions involving operations on huge arrays

Hi, i am working on an assignment that requires me to use a two-dimensional double array with 400 values in the 1st dimension, and 10000 values for each of those elements.. So the problem is when I write functions to calculate the means and mean of the means, and i click compile and run, the program does a runtime error and stops working..

How do i output stuff with these huge arrays? I am required to output answers like mean of the means and std. deviations of the means, but how am i supposed to do that when the program refuses to work with these numbers..?
I don't think the stack has enough space for 400 * 10,000 (4,000,000) bytes. (that's almost 4 MiB).

Try allocating it dynamically, e.g.
char* 2D_array = new char[400][10000];
then the operating system will allocate the memory for you.

Just remember to delete[] 2D_array when you're done with it to avoid memory leaks.
Last edited on
ok thanks, but how do i put two-dimensional dynamic array parameters in functions?
i know for regular arrays, it's double array[][size] in the parameter, but
i tried just putting double *array as the parameter, but i get a lot of errors..
i think i figure it out, do i have to use double **array in the parameter? and i also used a very complicated pointers to pointers declaration like:
1
2
3
double **array = new double*[firstdimension]
for(int i =0; i<firstdimension;i++)
   array[i] = new double[seconddimension];
I would do it like that, yes.

As for passing as a parameter; it's as simple as this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

void function(const char** _2D_array) {
    for (int i = 0; i < 4096; i++) {
        std::cout << "_2D_array: " << _2D_array[i] << std::endl;
    }
}

int main(void) {
    char** _2D_array = new char*[4096];
    
    for (int n = 0; n < 4096; n++)
            _2D_array[n] = new char[1024];
            
    for (int i = 0; i < 4096; i++)
        for (int j = 0; j < 1024; j++)
            _2D_array[i][j] = 'a';
            
    function((const char**)_2D_array);
    
    delete[] _2D_array;
            
    return 0;
}

There you'll see that _2D_array is full of letter 'a's.
Last edited on
Topic archived. No new replies allowed.