memory management - Array bound violation: segfault at different indices -


why program segfault on different value of every time?

#include <stdio.h> int main() {    int a[16], = 0;    while(1) {       a[i] = i;     printf("%d\n", a[i]);     i++;   } } 

i think way c allocates memory. when use line

int a[16] 

what saying 'allocate me enough space in memory hold sixteen integers, starting @ given point (call point x)'.

then when

while(1) {       a[i] = i;     printf("%d\n", a[i]);     i++; } 

you say, forever, set ith index of a i , print it. works starting @ x , moving along, 1 index @ time. first sixteen integers fine, have cleared space it.

however, when go past that, haven't cleared space, don't know there , can overwrite or print. might fine while, come point can't overwrite in memory (it isn't free). @ point segfault.

as each time run program allocate space in different place, take different number of iterations reach point memory taken, , segfault @ different indexes.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -