-2

I want to write a program in which I want to initialize integer array of size 987654321 for storing values of 1 and 0 only, here is my program

#include <stdio.h>
#include <stdlib.h>

int main(){
    int x,y,z;
    int limit = 987654321;
    int arr[limit];
    for (x = 0;x < limit;x++){
        printf("%d \n",arr[x]);
    }
    return 0;
}

but it gives segmentation fault

R K
  • 307
  • 7
  • 22
  • What is the initial content? – Sourav Ghosh Aug 22 '18 at 07:57
  • 1
    Its probably too big for the stack, beside the fact that it is not initialized. – Osiris Aug 22 '18 at 07:59
  • 1
    That is over 3GB of memory. Many computers don't even have that much available to programs. – Susmit Agrawal Aug 22 '18 at 08:01
  • 1
    Local variables, including arrays, are usually stored on the stack of the process. The stack is a limited resource, usually 8MiB on Linux but only a single MiB on Windows. Now if `sizeof(int) == 4` (which is the usual) then you attempt to create an array of over 3 ***giga*** bytes on the very limited and small stack. – Some programmer dude Aug 22 '18 at 08:01
  • 1
    Also note that local variables, if uninitialized, will have an *indeterminate* (and seemingly random) value. – Some programmer dude Aug 22 '18 at 08:02
  • I only wanted to store 1 or 0 and that costs 3GB memory – R K Aug 22 '18 at 08:03
  • @RK if you want only store 0 or 1 you could use a bitmap where each value is represented by one bit, this will save memory by a factor of 32 (if sizeof(int) is 4 on your platform), or even by a factor of 64 if sizeof (int) is 8 on your platform. However this is slighly more complicated and may be slightly slower. [This SO article](https://stackoverflow.com/q/1225998/898348) may be useful. – Jabberwocky Aug 22 '18 at 08:26

2 Answers2

1

987654321 is certainly too big for a local variable.

If you need a dynamically sized array of that size you need to use malloc like:

int limit = 987654321;
int *arr = malloc(limit * sizeof(*arr));
if (arr == NULL)
{ 
  ... display error message and quit
}
...
free(arr); // free it once you're dont with the array

BTW are you aware that your array uses roughly 4 gigabytes of memory assuming the size of int is 4 on your platform?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I am certainly a newbie in C ,not aware of the memory management, recommend me some tutorial or resource if you can – R K Aug 22 '18 at 08:05
  • @RK there are many, many resources on the internet covering this. Googling "C memory variables" or similar stuff should help. Any good C text book should cover this as well. – Jabberwocky Aug 22 '18 at 08:30
0

Since you want to store values of 1 and 0 only, and these values require only one bit, you can use a bit array instead of an integer array.

The size of int is 4 bytes (32 bits) usually, so you can reduce the memory required by a factor of 32.

So instead of about 4 GB, you will only need about 128 MB of memory. Resources on how to implement a bit array can be found online. One such implementation is here.

P.W
  • 26,289
  • 6
  • 39
  • 76