468,771 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Use Global Variable or Global Struct???

.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;
}
Jul 22 '05 #1
2 4886
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...

Jul 22 '05 #2
ajk
On 26 Nov 2004 14:02:06 -0800, Br**********@yahoo.com (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

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dan Elliott | last post: by
reply views Thread by Bryan Parkoff | last post: by
8 posts views Thread by Fernan Bolando | last post: by
10 posts views Thread by Kleenex | last post: by
7 posts views Thread by Michael | last post: by
13 posts views Thread by Sunil | last post: by
37 posts views Thread by eoindeb | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.