C Traps?

Common false assumptions(also known as myths)

  • The getc function returns a char. This is not true, the function getc returns an int, and assuming otherwise could lead to infinite loops.
  • All ints are the same. This is a very common mistake, which unfortunately is even believed by some profesionals. This is simply not true. Ints are sometimes implemented as 16bits, sometimes as 32bits, and so on. Never assume that an int is a specific size. It makes your code platform dependent and it can lead to errors(specially in dynamic memory allocation). It is better to use the sizeof operator.

    Tricky Situations

    This section will be used to show some code that looks correct, but it is in effect incorrect or flawed in some way. After the code, there will be an explanation of why the code doesn't work.
  • unsigned char a, dest[256],source[256]; a = 0; for(a;a <=255;a++) dest[a] = source[a];

    This code looks inocent enough. However, it is severly flawed. The reason for this is that the code will never stop running. Notice how the comparison checks for a <= 255, well when a = 255 the inequality is true, so the loop increases a as it prepares for the next run. However when a is increased it turns over to 0. The reason for this is that the highest number a char can hold is 255 so when it tries to increases to 256 it actually turns over and it starts again from 0, which causes an infinite loop.

  • int i=1; char dest[10]; for(i;i<=10;i++) dest[i] = 'a'

    This is bad code! Arrays in C start from 0 not one, therefore if you try to access dest[10] you are overwritting whatever was been stored in that memory location. This could cause your program to crash.


    Simple Optimizations

  • Clean your loops. This will make your code execute faster! Example: for(j=0;j<200;j++) { for(i=0;i<320;i++) dest[j*320 + i] = source[j*320 + i]; }

    Although this code works, it performs 199 * 2 useless mults. It would be better to implement this code like:

    for(j=0;j<200;j++) { temp = 320 * j; for(i=0;i<320;i++) dest[temp + i] = source[temp + i]; }

    Note: This code could be better implemented using a memcopy and other techniques, the point of this example is to show that keeping a loop clean can save a lot of time, in this case 398 instructions.

  • Division and multiplication takes a lot of time. Therefore it is better to learn some tricks to avoid using them. Example: If you have a line of code that reads a = 24/4, it is better to use shift right by 2 (>>2), this is a lot faster than the division. Multiplication is a similar case. If you have something like a = 6 * 4, it is better to use a shift left by 2 (<<2). This works for any power of 2, that is:2, 4, 8, 16, 32, 64, 128...

    More Traps and errors will follow

    If you have any addition to the list, please feel free to write me e-mail. eagle@amadeus.upr.clu.edu