// you’re reading...

Programming

C ate my homework

C ate my homework
Pointer -> Pointer -> Important homework file. Ouch!

There are no C courses that don’t mention some horror story about someone crashing their system by “accidentaly” writing to an arbirary memory location, or worse yet – erasing their harddrive in some infinite loop.

Dan and I have decided to put this to the test!

int main(void)
{
int i = 0; // counter
for (i; 1; i = i + sizeof(int)) // infinite loop
{
// print where we are, location and content
printf("%d : %d\n", (&i + i), *(&i + i));
// nasty part - write to this location
*(&i + i) = 0;
}
return 0;
}

Few moments, and no Blue Screen of Death, later the execution is terminated – “Segmentation fault”. More specifically, as the dump file reveals, a STATUS_ACCESS_VIOLATION Exception has occured.

It turns out that modern 32-bit compilers (we were testing with gcc and Borland) create executables in a “protected mode” by default. What this means is that the program is not allowed to write to memory locations that are not allocated for its use. The stories must have originated during the use of older 16-bit compilers, with programs running in “real mode” and having lenient access to hardware.

That being said, CompSci.ca will be allowing C during the DWITE programming contest. Which is pretty awesome, as C is a very fun language and allows for some nifty data structures and operations with the use of pointers.

Though I still snicker at the idea of using an arbitrary memory range for playing pacman!

Read more

Uhh... nothing else appears to be relevant enough.

Discussion

Post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>