Search code examples
carrayslimit

Why is there a limit in accessing array in C?


The OSX system can allocate the memory just fine from malloc (return non-NULL). However when accessing position pass 1073741823, Segmentation fault: 11 happened. Can anyone please educate me the reason?

I can access any points below 1073741824 just fine (as shown in the code.) I did try accessing random positions above 1073741824 point and results were still the same error while any random access below 1073741824 worked just fine.

    void dead(){
        size_t pos = 0;
        size_t max =2147483647;
        max *= 2;
        printf("%ld\n",max);
        int* data = malloc(max);
        if(data == NULL){exit(1);}
        pos = 2147483647/2;
        pos-=3;
        for (; pos < 2147483647; pos++) {
            printf("%ld\n",pos);
            data[pos] = 10;
            printf("%i\n",data[pos]);
        }
    }

    4294967294
    1073741820
    10
    1073741821
    10
    1073741822
    10
    1073741823
    10
    1073741824
    Segmentation fault: 11

Theoretically it should not die there.


Solution

  • The malloc() function allocates chars, and sizeof(int) is typically much larger than 1 char.

    For example; if you allocate memory for 2147483647 chars but a sizeof(int) is "4 chars"; then you've only allocated enough memory for "2147483647/4 = 536870911 ints".

    Note that malloc() typically asks the operating system for a larger amount of memory and splits it up into smaller pieces. This means that when you access something you didn't allocate with malloc() you may still access the larger amount of memory that malloc() asked the OS for, so you don't get a page fault ("segmentation fault") as soon as you access something you didn't allocate (more specifically, you don't get "segmentation fault" as soon as you try to access the 536870912th int that you didn't allocate memory for).