473,389 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 software developers and data experts.

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 5156
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
0
by: Bryan Parkoff | last post by:
I know that after optimization is done under C++ compiler's option, it always align byte and word into dword inside struct. What about global variable in this slope? If global variable has done...
8
by: Fernan Bolando | last post by:
I have been going through some of the tutorials on the internet regarding coding styles in unix C programming. Most of the stuff I have read do not prefer using global variables, But for multi-file...
10
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
13
by: Sunil | last post by:
Hi all, I want to know how good or bad it is using global variables i.e advantages and disadvantages of using global variables. Sunil.
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
1
by: sunil | last post by:
Hi, Am developing one shared library for one application. In that .so am having one global array of structure ( two more different structure pointers in this struct). Once the application is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.