LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2012, 09:11 PM   #1
Zssfssz
Member
 
Registered: Sep 2011
Location: Las Vegas!
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339

Rep: Reputation: Disabled
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?
 
Old 02-04-2012, 09:16 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by Zssfssz View Post
Now are these special eccseptions or is that (declaring variables at top of function) just a heed practice?
eccseptions... hmmm... even I, after consuming a lot of beer, I know that it is spelt as "exceptions".

As for the rest of your post, there are aliens looking over your shoulder.











Trust me! It's true!
 
Old 02-04-2012, 09:18 PM   #3
Zssfssz
Member
 
Registered: Sep 2011
Location: Las Vegas!
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339

Original Poster
Rep: Reputation: Disabled
I typed this whole thing on my iPod, there will be typos.
 
Old 02-04-2012, 10:00 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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.
 
Old 02-05-2012, 09:24 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Zssfssz View Post
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.

Last edited by johnsfine; 02-05-2012 at 09:29 AM.
 
  


Reply

Tags
c++, vareabls



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
headers,declarations,definitons spx2 Programming 7 08-25-2006 03:01 AM
errors on forward declarations. manikan Programming 11 02-03-2006 09:49 AM
C++ function declarations and definitions... AM1SHFURN1TURE Programming 2 08-29-2005 06:57 PM
Forward declarations somnium Programming 3 09-16-2003 05:00 AM
global declarations Randall Programming 50 03-15-2003 01:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration