It is currently Mon May 20, 2013 12:34 pm

All times are UTC - 5 hours [ DST ]




 Page 1 of 5 [ 50 posts ]  Go to page 1, 2, 3, 4, 5  Next
#1) 
Author Message
 Post subject: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 2:36 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1800
Location: India
Disclaimer:
I created very few of these guidelines on my own.Some i learned from experience others i learned from my fellow team members or good books or some great forums on the net.My knowledge is no where near complete so there can be mistakes,feel free to correct them and your feedback is greatly anticipated.This is a book i highly recommend and it is not tied to any particular language.And irrespective of your domain i hope you will find it a good read.
The Practice of Programming

For all these examples i will be using Microsoft Visual C++ 2010 Express edition(Provided free of charge by Microsoft)
VS 2011
U can use VS 2011 without any problem.
and Cppcheck(Free again) as a lint tool.A little about lint
http://en.wikipedia.org/wiki/Lint_%28software%29
Basically Cppcheck is a static code analyzer for C/C++ code.More on this later

Lets begin :grin:

1.
Almost all the beginners make this mistake in C/C++
if (i=6)
instead of:
if (i==6)
Once experienced, this painful(but hard to catch) error (doing an assignment where comparison was intended) is rarely
repeated. Some programmers have developed the habit of writing the literal first, like this: if
(6==i)
. Then, if an equal sign is accidentally left out, the compiler will throw an error
"left operand must be l-value."(from VS 2010)This error means u can't assign a value to a literal. This won't protect you when comparing two variables, but every little bit helps.

U can see that there are no syntactical errors when u write if(i=6) but a logical error has occurred.Another thing tools are not substitute for shoddy programming,good programming habits makes your life a hell lot easier.


Last edited by jaydip on Sat Apr 14, 2012 4:11 am, edited 1 time in total.

Offline
 Profile  
 
#2) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 4:06 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1800
Location: India
Integrating Cppcheck with VS:

As of now now there are no official plugins available for VS but u can add Cppcheck as an External Tool in VS.But why do we need Cppcheck in the first place?After the integration i will show u an example which will clarify my point

In VS goto Tools->External tools.This screen will open up
Attachment:
ET1.png
ET1.png [ 33.68 KiB | Viewed 1118 times ]

Give any meaningful name in the title option here Cppcheck
The command should be the path to the cppcheck.exe here D:\Cppcheck\cppcheck.exe
The title should be -f --enable=all --template vs $(ItemPath)(By default Cppcheck shows only errors, by using --enable=all we see styles,warning messages for eg,I am using VS(visual studio) as a template here).
Now tick the 'use output window' and click OK.Now we are ready to use cppcheck :grin:


Offline
 Profile  
 
#3) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 4:26 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1800
Location: India
Here is a small program to illustrate the need-fullness of cppcheck
int main()
{
int i,x;
if(i==5)
x=6;
return 0;

}
This is very bad programming but on compilation(default settings) it compiles fine with just one warning "warning C4700: uninitialized local variable 'i' used" .You can run this program without any errors.This is not unexpected,compilers are generally geared up for optimum run-time performance of the code and there is a trade-off for this.At first lint was part of the compiler but later it got separated and started its career as a static analyzer tool.What happens if we see the output from cppcheck?(Please save your program first before using cppcheck)U can invoke cppcheck from Tools->cppcheck

style: The scope of the variable 'x' can be reduced
style: Variable 'i' is not assigned a value
style: Variable 'x' is assigned a value that is never used
error: Uninitialized variable: i

Now i think u can see my point.It identifies problems beyond the realm of compilers.So its always a good practice to run a static code analyzer on your code.Javaguys typically use checkstyle for the same purpose,

Remember:
"cppcheck doesn’t detect syntax errors. Cppcheck only detects the types of bugs that the compilers normally fail to detect. The goal is no false positives."(From the cppcheck manual)

To be continued... :grin:


Last edited by jaydip on Sat Apr 14, 2012 8:22 am, edited 1 time in total.

Offline
 Profile  
 
#4) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 7:59 am 
User avatar

Joined: Fri Jul 04, 2008 10:30 am
Posts: 4932
(goodpost) :good:


Offline
 Profile  
 
#5) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 8:24 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1800
Location: India
Thx, I will continue to add new guidelines.


Offline
 Profile  
 
#6) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 9:37 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1800
Location: India
2.
Here is another gem ;)
int main()
{
int i[10];
i[10]=0;
return 0;

}
Upon compilation there is no error or warning messages from the VS compiler and as u guessed it runs just fine.Lets have a look at the output from cppcheck
style: Variable 'i' is assigned a value that is never used
error: Array 'i[10]' index 10 out of bounds
Unchecked the last one would be a major source of frustration.int i[10]; creates an array for 10 consecutive integers but the array starts at index 0 instead of 1.So the last valid index would be i[9] not i[10]. This has to do with the offset terminology in compiler where we start with 0 offset instead of 1.i[10]=0; is not an error in C/C++ but can result in potential unsafe behavior,u can overwrite other program's data for instance.This can come as a surprise for the javaguys where i[10]=0; would have resulted in a compilation error.When programming for C/C++ u should always remeber that array index starts at 0 and the maximum addressable index is arraysize-1.Using a static code analyzer during the development process minimizes this kind of dubious programming.


Offline
 Profile  
 
#7) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 10:15 am 
User avatar

Joined: Fri Jul 04, 2008 1:26 am
Posts: 19682
Location: 404 - Not Found!
Very cool.

This topic has been "stickied"
:)


Offline
 Profile  
 
#8) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 10:16 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1800
Location: India
Fun Staff ;)
If u ask any C/C++ beginner about the size of an integer the answers will probably vary from 2 to 4 bytes.C/C++ doesn't impose any hard limit on the size of the variables,its actually implementation dependent.So the correct answer will be sizeof(int) is the size of an int.This is in stark contrast with Java where its defined in the language itself.The limits of various data types are defined in limits.h header file.Programmers should never rely on the size of the variables as it affects portability.


Offline
 Profile  
 
#9) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 10:17 am 
User avatar

Joined: Mon Mar 26, 2012 11:32 am
Posts: 1800
Location: India
apoppin wrote:
Very cool.

This topic has been "stickied"
:)


Thx :grin:


Offline
 Profile  
 
#10) 
 Post subject: Re: C/C++ Programming Guidelines
PostPosted: Sat Apr 14, 2012 10:27 am 
User avatar

Joined: Sat Apr 24, 2010 11:19 am
Posts: 4969
I can fumble my way around C. Learned it back in the Amiga days, you had to have a working knowledge or you had no hope of understanding the Amiga documentation since all but a tiny portion of the Amiga's OS and kernel are written in C.

Hate it as a programming language though. Too low level, almost like writing assembler. I prefer a higher level language.



_________________
This is such total Horse-S**t!
"At NVIDIA we know that all shredders are green." --Jensen Huang
Adam knew he should have bought a PC, but Eve fell for the marketing hype. >:)
Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 5 [ 50 posts ]  Go to page 1, 2, 3, 4, 5  Next

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: