diff --git a/Appendix1/Assertion/Assertions.c b/Appendix1/Assertion/Assertions.c new file mode 100644 index 0000000..40a0388 --- /dev/null +++ b/Appendix1/Assertion/Assertions.c @@ -0,0 +1,18 @@ +#include +#include + +int main() +{ + int x = 7; + + /* Some big code in between and let's say x +       is accidentally changed to 9 */ + x = 9; + + // Programmer assumes x to be 7 in rest of the code + assert(x==7); + + /* Rest of the code */ + + return 0; +} \ No newline at end of file diff --git a/Appendix1/CodeBlocking/Blocking.c b/Appendix1/CodeBlocking/Blocking.c new file mode 100644 index 0000000..ac55f27 --- /dev/null +++ b/Appendix1/CodeBlocking/Blocking.c @@ -0,0 +1,45 @@ +#include + +#define BLOCKSIZE (8) + +int main(void) +{ + int i = 0; + int limit = 19; /* could be anything */ + int blocklimit; + + /* The limit may not be divisible by BLOCKSIZE, + * go as near as we can first, then tidy up. + */ + blocklimit = (limit / BLOCKSIZE) * BLOCKSIZE; + /* unroll the loop in blocks of 8 */ + while( i < blocklimit ) + { + printf("process(%d)\n", i); + printf("process(%d)\n", i+1); + printf("process(%d)\n", i+2); + printf("process(%d)\n", i+3); + printf("process(%d)\n", i+4); + printf("process(%d)\n", i+5); + printf("process(%d)\n", i+6); + printf("process(%d)\n", i+7); + + /* update the counter */ + i += 8; + } + if( i < limit ) + { + /* Jump into the case at the place that will allow + * us to finish off the appropriate number of items. */ + switch( limit - i ) + { + case 7 : printf("process(%d)\n", i); i++; + case 6 : printf("process(%d)\n", i); i++; + case 5 : printf("process(%d)\n", i); i++; + case 4 : printf("process(%d)\n", i); i++; + case 3 : printf("process(%d)\n", i); i++; + case 2 : printf("process(%d)\n", i); i++; + case 1 : printf("process(%d)\n", i); + } + } +} \ No newline at end of file diff --git a/Appendix1/DebugAssertion/DebugAssertion.c b/Appendix1/DebugAssertion/DebugAssertion.c new file mode 100644 index 0000000..6f7c59c --- /dev/null +++ b/Appendix1/DebugAssertion/DebugAssertion.c @@ -0,0 +1,11 @@ +# define NDEBUG +#include +# include + +int main() +{ + int x = 7; + assert (x==5); + printf("x = %d\n", x); + return 0; +} \ No newline at end of file diff --git a/LC21/cond.c b/LC21/Cond/cond.c similarity index 100% rename from LC21/cond.c rename to LC21/Cond/cond.c