Thread: Defining the variables

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Defining the variables

    Please help me to come out of this confusion. whenever i start a project and start developing i declare global variables based on the requirement like int, float etc. But lot of people suggesting me to use structures instead of the separate variables. Do you recommend to first get the understanding of all the variables used in the project and then take a decision of which variables to put in a structure? Sometimes if i suddenly need to define a global variable. How do i proceed? Please advise.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by Satya View Post
    Do you recommend to first get the understanding of all the variables used in the project and then take a decision of which variables to put in a structure?
    Yes, at least in a general sense. Building a program bottom-up can be useful but you should have an idea of how things will turn out. If you don't, you risk having to refactor the code later because the way you did it the first time made it too cumbersome.

    Quote Originally Posted by Satya View Post
    Sometimes if i suddenly need to define a global variable. How do i proceed?
    Ask yourself whether you really need it to be global or not. In most situations that don't involve system-wide states, you don't. If you find that you keep needing unrelated global variables, then something is probably wrong with your design.
    Last edited by GReaper; 11-16-2018 at 05:54 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    It's normally bad practice to use global variables since the can get changed from all over the code and that can make debugging very hard, it doesn't matter if they are simple vars or structs.
    It's better to keep variables local and pass them to the function that needs them.
    Structs should be used to combine related data like for a person which has a firstName and a lastName.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Structures add padding bytes i should not worry about that ?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by Satya View Post
    Structures add padding bytes i should not worry about that ?
    Ideally, you should build your program in such a way that it doesn't care about padding. In reality though, that not always possible. All compilers I've come across have some special command that disables padding when you want to make sure the struct is always the same size. GCC uses __attribute__, VS uses #pragma, etc.

    Be cautious though, as unaligned variables almost always cause performance drops and for some architectures even cause crashes!
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining variables in C++ as late as possible?
    By Absurd in forum C++ Programming
    Replies: 11
    Last Post: 09-18-2014, 09:34 AM
  2. defining macros or static variables
    By l2u in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2008, 06:28 AM
  3. Replies: 3
    Last Post: 11-28-2006, 03:44 PM
  4. Defining Variables
    By Beaner in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2005, 05:50 PM
  5. Globaly defining variables
    By PaloDeQueso in forum C Programming
    Replies: 5
    Last Post: 03-20-2002, 09:38 PM

Tags for this Thread