+ 1
The code declares a zero-length array. int count=0; int arr[count]; That means no storage is allocated for any array element of arr[]. If it accesses arr[0], then it will go out of bounds of the array and by happenstance it will access the storage for count, but only by a fluke in the way memory is managed. Furthermore, if it accesses arr[1] or arr[2] ... then it will go beyond the memory segment allotted for variable storage. Hence, it reports a memory segment violation exception.
27th Feb 2022, 9:07 AM
Brian
Brian - avatar
+ 2
int count=0; int arr[count]; arr[0] here your array size is static it wont grow dynamically
26th Feb 2022, 7:37 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
By giving arr a length of zero, arr effectively becomes a pointer to the variable that is defined before it. When i=0 it stores n in arr[0], which is the same as writing count = n. That is not the cause of the exception, though. As i increments to 1, 2, 3,... the code a[i]=n begins overwriting memory outside the bounds of the variable storage and that causes a segmentation fault.
26th Feb 2022, 6:14 PM
Brian
Brian - avatar