ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339
Rep:
C++ Declarations And Position
In all the C, C++, and Obj-C tutorials I have read they say all vareabls need to be declared at the top of a function (or be global) or there will be problems. Now I can think of several areas where this doesn't apply:
File I/O: I always declare the fstream variable when I use it and have always been fine.
For Loops: It makes you declare a variable in them for testing
Now are these special eccseptions or is that (declaring variables at top of function) just a heed practice?
C89 (gcc -std=c89, or -std=c90, or -ansi) requires all declarations to be at the beginning of a block, C++ and C99 both support declarations anywhere within a function.
In all the C, C++, and Obj-C tutorials I have read they say all vareabls need to be declared at the top of a function (or be global) or there will be problems.
For C++, that would be a major flaw in the tutorial.
For ANSI C, it is roughly correct.
Best practice is declaring variables with the narrowest scope. The compiler's optimizer often fails to deduce a narrow scope (though it tries) and much better asm code can be generated when the optimizer knows about narrow scope. But the more important reason is to help the maintenance programmer. When you want to correct or enhance a module, obvious narrow scopes for most of the variables make understanding the code much easier.
In C++, it is also best practice, when possible, to delay declaring each variable until the initial value for that variable is available. There are many situations in which having a default constructor for a type is a problem. Without a default constructor, you need some initial value to declare the variable. It is confusing and inefficient to declare a dummy initial value that isn't used. Even with a default constructor (or POD objects not needing construction) the declaration without the initial value can be confusing to both the maintenance programmer and the optimizer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.