7

My question is related to C language. I have to create a big array of around two million elements but the computer gives a "Segmentation fault (Core dumped)" error. I am simply saying:

int integer_array[2000000];
float float_array[2000000];

I am sure this has something to do with the memory allocated to arrays but I cannot figure out the right solution.

Harris
  • 117
  • 1
  • 2
  • 8
  • Please give a complete code example. We should be able to compile and run it ourselves and get the same result you are asking about. – Code-Apprentice Mar 25 '17 at 10:11
  • 1
    Your are allocating these array on stack and not heap. – Ankur Mar 25 '17 at 10:11
  • I strongly suspect this is one of *many* duplicates to this problem: [Segmentation fault on large array sizes](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes). – WhozCraig Mar 25 '17 at 10:12
  • Possible duplicate of [Is there any limitation on the maximum size of array in c?](http://stackoverflow.com/questions/12552968/is-there-any-limitation-on-the-maximum-size-of-array-in-c) – Tim Biegeleisen Mar 25 '17 at 10:12
  • @WhozCraig Right you are sir. Just throw that puppy in the free store and off the stack. – Tim Biegeleisen Mar 25 '17 at 10:13
  • @i486 - oops... – Paul Ogilvie Mar 25 '17 at 10:16
  • @TimBiegeleisen unless I see the actual encompassing function, thereby validating my assumption this is a stack issue, I can't pull the lever on closing this as a duplicate. It's likely, but the OP did *not* post a mcve that confirms it, which frankly kinda sucks =( – WhozCraig Mar 25 '17 at 10:21
  • Possible duplicate of [C programming, why does this large array declaration produce a segmentation fault?](http://stackoverflow.com/questions/3049934/c-programming-why-does-this-large-array-declaration-produce-a-segmentation-faul) – Andrew Henle Mar 25 '17 at 12:58

2 Answers2

8

Usually you need to create such an array dynamically on the heap.

int *integer_array = (int*)malloc(2000000 * sizeof(int));
float *float_array = (float*)malloc(2000000 * sizeof(float));

The array might be too large for stack allocation, e.g. if used not globally, but inside a function.

int main () {
    int a[200000000]; /* => SEGV */
    a[0]=0;
}

The easiest fix, move the array outside:

int a[200000000];
int main () {
    a[0]=0;
}

You can also declare it static:

int main () {
    static int a[200000000];
    a[0]=0;
}

Note that the stack size is system dependent. One can change it with ulimit.

rurban
  • 4,025
  • 24
  • 27
  • 4
    All casts are bad. No need to cast the return value of malloc(). Also, a more robust alternative is to use sizeof object instead of sizeof(type). Final nit, integer_array is poorly named. It's not an array, sizeof(integer_array) will not return 2000000 * sizeof(int). int *integer_array = (int*)malloc(2000000 * sizeof(int)); int *ip = malloc(2000000 * sizeof *ip) is more idiomatic in C. – Bjorn A. Mar 25 '17 at 13:44
  • 2
    @BjornA. You are talking nonsense. All of your arguments are false. – rurban Mar 26 '17 at 10:58
  • 2
    Really? I think not, but feel free to _argue_ properly. Show me where I'm wrong. But remember, the language in question is C, not C++. – Bjorn A. Mar 27 '17 at 16:27
  • 2
    I said all your arguments are false. malloc needs to be casted, this is C. sizeof(type) is superior. integer_array is properly named. int integer_array = (int)malloc(2000000 * sizeof(int)); is nonsense. int *ip is not the question here. – rurban Mar 30 '17 at 05:58
  • 3
    " malloc needs to be casted, this is C. " You're a funny guy, @rurban. Or a troll, hard to tell. ;) Anyhow, the comments speak for themselves. TTFH – Bjorn A. Mar 30 '17 at 11:19
  • Edit: I see that lots of * (asterix) has disappeared from my first comment. That's unfortunate and may have caused some confusion. – Bjorn A. Mar 30 '17 at 11:44
  • For disputes like this I refer to the C book (k&r) to see what the creator did. On pg 142 he casts the output of malloc and uses `sizeof()` on the data type: `return (struct tnode *) malloc(sizeof(struct tnode))`. – young_souvlaki Nov 18 '20 at 19:25
  • ip is a terrible name as it doesn't mean anything by itself. The naming convention for dynamic arrays I use is `` *dynArray``. Then ``int *ip`` becomes ``int *dynArrayInt`` It's really easy to write cryptic C code when you're not careful. – Scover Jul 01 '23 at 17:10
-1

Define them as static or put outside function. Now they are automatic and get 16MB space of stack which probably is much smalled. BTW, it is better to use double instead of float.

i486
  • 6,491
  • 4
  • 24
  • 41