Array of size 2 ^ 25

I am trying to create an array of size 2 ^ 25 in c, and then perform some elementary operations on it (memsweep function). Code c

#include <stdio.h>
#include <time.h>
#define S (8191*4096)
main()
{
                clock_t start = clock();
                unsigned i;
                volatile char large[S];
                for (i = 0; i < 10*S; i++)              
                large[(4096*i+i)%S]=1+large[i%S];

                printf("%f\n",((double)clock()-start)/CLOCKS_PER_SEC);
}

I can compile it, but when executed it gives a segmentation error.

+3
source share
4 answers

You do not have such a large stack space to allocate an array that is large ... on Linux, for example, the stack size is usually 8192 bytes. You definitely exceeded that.

The best option would be to allocate memory on the heap using malloc(). Therefore you must write char* large = malloc(S);. You can access the array using notation [].

, Linux, sudo ulimit -s X, X - , , ... .

+3

, .

  • large
  • malloc
+9

The array is too large to fit on your stack. Use a bunch of s instead char *large = malloc(S).

+4
source

A large one stands out on the stack and you overflow it.

Try using char *large = malloc(S)

+3
source

All Articles