Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Global reference to data Posted by Burferd on 8 Aug 2009 at 1:41 PM
I have a C++ project that will turn out to be fairly large.
I have a requirement to maintain and update several hundred variables.
These variables are used throughout the program.
My application is in several classes to handle individual tasks.
I have my variables in structures in separate files based on varible categories.

My problem is that I need to initialize the variables in one place and have those values be preserved when other classes reference them.

Basically, I want the structures to be global in context.

I have tried to use extern, but my compiler (gnu g++) complains.

How can I accomplish this?
Some simple example code snippits would be greatly appreciated.

Thanks.
Report
Re: Global reference to data Posted by Lundin on 8 Aug 2009 at 2:05 PM
Since you are using C++, you should use classes. Initialize and access all variables belonging together through their appropriate class. This is the whole point of using C++ and not C! If you aren't using the object-orientation concept, then get rid of C++ and write in pure C.

You will likely want to declare the objects where it makes sense (main function probably) then pass them as references to whatever other code modules that needs access to them.

Avoid global variables & the extern keyword! This is always poor programming and leads to spaghetti code! I dare state that there is not a single case in C/C++ where you ever need to declare a variable global/extern. Use the static keyword properly if you need to share a variable between functions inside a code module.

Report
Re: Global reference to data Posted by AsmGuru62 on 9 Aug 2009 at 4:58 AM
Here is how to avoid 'extern', but still have all variables in one place.

1. Make a large structure with all variables (put it into GLOBALS.H):

typedef struct
{
	int iMember1;
	long lMember2;
	char text [32];

	// ... etc.
}
GLOBALS;

Then declare this structure as static in some file (like GLOBALS.CPP):
static GLOBALS glb;

In the same file declare a function to return a reference to that static structure instance:
GLOBALS& Globals ()
{
	return glb;
}

Put a prototype for this function into GLOBALS.H, so it can be easily used. Now, all you have to do to use globals is to include GLOBALS.H anywhere you need - include it only into CPP files - do not include into other .H files!

Having a reference you can use this function to both GET and SET the members of the structure - isn't that cool!

int a = Globals ().iMember1;
strcpy (Globals ().text, "Alex");

Do not worry about calling the function often - in RELEASE mode the compiler should optimize this short call to be only one ASM instruction - loading address of that static structure into a register. Not sure about GNU however, it is not as great as VC++ compiler or Intel C++ Compiler. But it is easy to verify - simply disassemble the part where Globals() is called in RELEASE mode and check it out.
Report
Re: Global reference to data Posted by Lundin on 9 Aug 2009 at 9:03 AM
Regarding the optimizing of such functions: If you are writing in C++, make the function "inline". A C++ compiler must support that optimization.

Report
This post has been deleted. Posted by henrykuong on 9 Aug 2009 at 6:50 PM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.