Use Global Variable or Global Struct???

B

Bryan Parkoff

….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;
}
 
P

Phil Staite

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...
 
A

ajk

….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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top