简体   繁体   中英

Declaring a 5d array of size 26^5 in C

My question is pretty straightforward.

I'm building a small program to analyse and simulate random text using Markov chains. My first MC had memory of size 2, working on the alphabet {a, b, ..., z}. Therefore, my transition matrix was of size 26 * 26 * 26.

But now, I'd like to enhance my simulation using a MC with memory of size 4. Therefore, I need to store my probabilities of transitions in a 5D array of size 26*26*26*26*26.

The problem is (I believe), that C doesn't allow me to declare and manipulate such a array, as it might be too big. In fact, I got a segmentation faults 11 prompt when writing :

int count[26][26][26][26][26]

Is there a way to get around this restriction?

Thanks!

On a typical PC architecture with 32-bit integers, int count[26][26][26][26][26] creates an object of size 47525504 bytes, 47MB, which is manageable on most current computers, but is likely too large for automatic allocation (aka on the stack ).

You can declare count as a global or a static variable, or you can allocate it from the heap and make count a pointer with this declaration:

int (*count)[26][26][26][26] = calloc(sizeof(*count), 26);
if (count == NULL) {
    /* handle allocation failure gracefully */
    fprintf(stderr, "cannot allocate memory for 5D array\n");
    exit(1);
}

Make it global 1 or make it static or dynamically allocate the same amount of memory. Dynamic memory allocation allocates memory from a portion of memory which doesn't have the constraint to an extent larger than the one you faced. Variables having automatic storage duration are likely to stored in stack in most implementations. Dynamic memory belongs to heap in most implementations.

You can do this (Illustration):-

int (*a)[26][26][26][26] = malloc(sizeof *a *26);
if(!a){ perror("malloc");exit(1);}
...
free(a);

1 static storage duration - all variables defined in file scope have static storage duration.

With this kind of array declaration, your data will be stored in stack . And stack have usually only 8 MB on Unix like systems and 1 MB on Windows. But you need at least 4*26^5 B (roughly 46 MB).

Prefered solution would be allocate this array on heap using malloc .

But you can also instruct compiler to increase the stack size ...

Try this

#define max=11881376   //answer of 26*26*26*26*26
int count[max];   //array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM