Use Global Variable or Global Struct???

Discussion in 'C++' started by Bryan Parkoff, Nov 26, 2004.

  1. ….I would like to know which is the best optimization to use global
    variable or global struct. I always tell C/C++ Compiler to turn on
    optimization.
    ….I use underscore between first name and second name for better
    readable. After optimization, global variables might be misaligned
    because each global variables must be converted to 32 bits, but I do
    see that C/C++ Compiler do padding between variables. Struct does the
    same to do padding.
    ….I use dot between first name and second name using struct. It may
    be better readable than underscore between two names. After
    optimization, global struct binary file and global variable binary
    file are identical.
    ….Please state your opinion why you think that global struct is better
    that it has less error prone while it is easy to debug. Global
    variables can be very error prone because it may belong to this module
    or another module without using struct. If you declare two names
    using underscore, it might be helpful that it belongs to this module.
    It may be less bugs or no bugs. It seems that global variable and
    global struct are the exact same.
    ….Look at my code below.

    char Var__a;
    short int Var__b;
    long int Var__c;

    struct Var
    {
    char a;
    short int b;
    long int c;
    };

    Var var;

    void Func(void)
    {
    Var__a = 0x20;
    Var__b = 0x2040;
    Var__c = 0x20406080;
    }

    void Func2(void)
    {
    var.a = 0x20;
    var.b = 0x2040;
    var.c = 0x20406080;
    }
     
    Bryan Parkoff, Nov 26, 2004
    #1
    1. Advertising

  2. Bryan Parkoff

    Phil Staite Guest

    If you must have global anything, then I'd go with the struct.

    - It gets a, b, and c (in your example) out of the global namespace and
    puts them within the struct. Only Var var appears in the global namespace.

    - If a, b, and c are somehow related, wrapping them up in a struct is a
    good idea. You may even want to make Var a class and head down the OO
    path...
     
    Phil Staite, Nov 27, 2004
    #2
    1. Advertising

  3. Bryan Parkoff

    ajk Guest

    On 26 Nov 2004 14:02:06 -0800, (Bryan Parkoff)
    wrote:

    >….I would like to know which is the best optimization to use global
    >variable or global struct. I always tell C/C++ Compiler to turn on
    >optimization.
    >….I use underscore between first name and second name for better
    >readable. After optimization, global variables might be misaligned
    >because each global variables must be converted to 32 bits, but I do
    >see that C/C++ Compiler do padding between variables. Struct does the
    >same to do padding.
    >….I use dot between first name and second name using struct. It may
    >be better readable than underscore between two names. After
    >optimization, global struct binary file and global variable binary
    >file are identical.
    >….Please state your opinion why you think that global struct is better
    >that it has less error prone while it is easy to debug. Global
    >variables can be very error prone because it may belong to this module
    >or another module without using struct. If you declare two names
    >using underscore, it might be helpful that it belongs to this module.
    >It may be less bugs or no bugs. It seems that global variable and
    >global struct are the exact same.
    >….Look at my code below.


    If you necessarily need global variables you should put them in an own
    namespace, that way you will have them collected in one place and will
    not clash with other globals

    namespace myglobaldat
    {
    int foo1;
    int foo2;
    };

    int main(int argc,char**argv)
    {
    myglobaldat::foo1 = 10;
    }

    If you don't want to use namespaces, then I personally would prefer
    a global struct since it cleanly identifies global variables being
    global. Among the more difficult to read programs are the ones who
    have global variables and use them in functions together with local
    variables.

    this is seen purely from the readability viewpoint, whether it is
    effective from a machine code point of view I find it in most cases
    irrelevant - it only matters when doing very low-level programming.


    br/ajk
     
    ajk, Nov 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    414
    RA Scheltema
    Jan 6, 2004
  2. Bryan Parkoff
    Replies:
    0
    Views:
    444
    Bryan Parkoff
    Apr 2, 2005
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,433
    Chris Fogelklou
    Apr 20, 2004
  4. PCHOME
    Replies:
    5
    Views:
    461
    Darius
    Apr 13, 2005
  5. Replies:
    18
    Views:
    7,170
    Karl Heinz Buchegger
    Jul 22, 2005
Loading...

Share This Page