c++ - Why does this line of code cause a computer to crash? -


why line of code cause computer crash? happens on memory-specific level?

for(int *p=0; ;*(p++)=0)     ; 

i have found "answer" on everything2, want specific technical answer.

this code formally sets integer pointer null, writes integer pointed 0 , increments pointer, looping forever.

the null pointer not pointing anything, writing 0 undefined behavior (i.e. standard doesn't should happen). you're not allowed use pointer arithmetic outside arrays , increment undefined behavior.

undefined behavior means compiler , library authors don't need care @ these cases , still system valid c/c++ implementation. if programmer classified undefined behavior whatever happens happens , s/he cannot blame compiler , library authors. programmer entering undefined behavior realm cannot expect error message or crash, cannot complain if getting 1 (even 1 million executed instructions later).

on systems null pointer represented zeros , there no support memory protection effect or such loop of starting wiping addressable memory, until vital part of memory interrupt table corrupted or until code writes zeros on code itself, self-destroying. on other systems memory protection (most common desktop systems today) execution may instead stop @ first write operation.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -