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.
mallochave an undocumented behavior on certain inputs like(size_t)-2, and do something other than actually allocate memory.