Friday 2 September 2016

Common Logical Errors in C and C++

Today I spent over two hours discovering this stupid mistake in my code.
a=a++;
Thus I came up with the idea to make a cheat-sheet for common programing mistakes. I hope this is useful to you during your practical exams preparations, debugging times and coding competitions. In case, I have not mentioned one of the mistakes that you make just leave a comment and I will add it.


1.if(condition);statement;

Your if statement has no effect.

2.a=a++

Has no effect. The value of 'a' remains the same. Should be a=a+1 or a++.

3.for(i=0;i<=n;i++)

Should usually give a segmentation fault or some garbage value being printed. This is the off by one error. Consequences can range from a garbage value being printed to causing a serious vulnerability in the system.

4. if(condition) s1;s2;s3;

Should be if(condition){s1;s2;s3}. The root cause of this when you write code as follows:

if(condition) s1;

While this works perfectly fine. You discover that you missed two statements. You end up writing

if(condition) s1;s2;s3;

5.if(condition){break;s1;}

s1 is ignored. s1 should have been placed before the break statement. 

6.num1^num2

Usually you are making a error unless you want the ex-or operation to be performed. It should have been pow(num1,num2).

7.if(a=b)s1;

s1 will always be executed.The if statement has no effect.Should have been a==b.

8. return val; s1;

Once I return I will not revisit! Statement one is ignored.

9.printf("%d",&a);

I want to print the address of the integer. The postman requires it. Drop the ampersand. 

10. scanf("%d",a)

No, I am not my brother printf. I am the postman. Where's the address?. Ampersand required.

11.Overflow errors 

These happen when you or your code ends up storing a value larger then what a primitive is expected to hold. Trying to store 70,000 in an unsigned 2 byte integer for example. This will cause an overflow. Instead of an error being generated, what usually happens is a negative value gets generated.

If you are lucky then the negative value gets printed. And you understand that an overflow took place. But you could also be unlucky and this negative value, somehow eventually ends up being printed as a positive value after several modifications. In this case, you will be still get an error, however you are less likely to suspect an overflow.

Debugging then can turn into a infinite loop. Usually once you look over your code several times, I suggest considering looking into this possibility. You will have to make corrections in your code or use a larger primitive.

12.Writing an = instead of a +

Since + and = to share the same key. You could end up writing an equal to instead of a +. Consider a=b+c; you could end up writing a=b=c.
 

Additional Tips

1.Use a fresh mind to debug

A tired mind is less likely to find any mistake but instead is likely to go in an infinite loop. While a frustrated  mind is likely to create additional logical errors.

2.Ask someone else to look at your code

Even if someone doesn't know anything about your code, he/she could find a logical error quickly which you have been reading for several times and not realizing.



No comments:

Post a Comment