Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > C++

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 10-23-2009, 08:16 AM
maey_kich maey_kich is offline
Registered User
 
Join Date: Oct 2009
Posts: 3
Question Arrays across multiple functions and modules

hello,

I can't figure out how to create an array that can be called from any module. Global or public sounds familiar but they don't work.

Cheers.
Reply With Quote
  #2  
Old 10-23-2009, 10:32 AM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
int numz[10] = {0};

.....


void fill_in(int* p, int size)
{
for(int i = 0; i < size; i++)
p[i] = i;
}

.......

fill_in(numz, 10);

-------------------------------------------

If you are trying to make a global instead, you need to put the extern thing in your .h files as with C code. However, do not do this unless you have an extremely good reason, globals are messy. You should probably study how to do this and recognize it though, if for no other reason when you see it you immediately know the code is going to get ugly.

in c++, at least wrap your globals in a namespace, if not in a struct, just to qualify them for the reader. Better, put them in a class as static variables, locally create an instance of the class to read and write them:


class global_stuffs
{
public:
static int x[50];
...... ///more stuff
global_stuffs() {//initilize your globals here, BE WARNED PROTECT THE CTOR ONLY DO IT ONCE}

};

int global_stuffs::x[50];


void foo()
{
global_stuffs access;
access.x[0] = 10; //this is global, even though access is local you are touching a global, static object so each instace of the class uses the same memory and same global variable.

}

void bar()
{
global_stuffs same;
printf("%i\n", same.x[0]); //will print what was set in foo!

}


Remember this stuff is not thread safe nor particularly wise to do, a global is still a global and the problems they cause are all still there, its just wrapped up to look nicer and be a touch more clean. You can *add* things to your global class to protect for threading and to handle other problems with globals but thats your problem -- it balloons the code up fast and is mostly bloat if you do not need it.


Note: you can actually fool with the globals directly via
global_stuffs::x[0];

because the memory persists without an object of the class. In fact, you want to be careful with any code in your constructor for that reason: making a new instance will delete the persistant value if the constructor modifies them all back to a default! You can add the do once code to the ctor of course, but as I said, it quickly becomes hackery no matter how you want to address the problem. (Also, I am in the minority here, but the entire object oriented paradigm is actully a clever way to make everything more or less global in a nice, clean way in my opinion. That being the case, you really shouldnt need any of my stuff here at all: a good design should remove the need for globals period, and the only reason to have them is to work around a design flaw or for speed (they are faster, and sometimes it matters, but this is not the norm).

Last edited by jonnin; 10-23-2009 at 10:44 AM.
Reply With Quote
  #3  
Old 10-23-2009, 10:39 AM
Danny's Avatar
Danny Danny is offline
Super Moderator
 
Join Date: Nov 2003
Posts: 3,948
You need to declare the array as a global variable. Define it in one module in the global namespace, say before main()
Then declare that same array as extern in any other module that refers to the array.
//module 1
int arr[5]={0,1,2,3,4};
int main()
{}
//module 2
extern int arr[5];
arr[0]=9;
__________________
Danny Kalev
Reply With Quote
  #4  
Old 10-23-2009, 11:34 AM
maey_kich maey_kich is offline
Registered User
 
Join Date: Oct 2009
Posts: 3
Question Cheers!

Thanks guys that was really helpful! But is there anyway to apply this to a class? So that info about a wav file could be read from multiple modules?

Thanks again!
Reply With Quote
  #5  
Old 10-23-2009, 03:11 PM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
Quote:
Originally Posted by maey_kich View Post
Thanks guys that was really helpful! But is there anyway to apply this to a class? So that info about a wav file could be read from multiple modules?

Thanks again!
What is a module, exactly? The term is one of those textbook/classroom terms that is a little different in meaning depending on who said it at what location on what day.


Do you mean, how can class A share its data with class B? This is not done by globals, its done by object oriented techniques such as polymorhpism (assign A into B and the correct fields are copied over) or inheritance (B uses the data from A directly, or the getter function of A, etc) or the like. Remember when I said the point of objects is really a clever disguise to make everything global (ok, its really a clever way to control who can get at what, but it certainly can make everything open to everything else in a global sense if that is what you like, or lock it out if you prefer, etc.). Or you can handle it with simple parameter passing, or have another object C that both A and B use, or dozens of other ways. Even a simple assignment operator overload can be used (B = A).

In your program, can B see A? If so, a simple solution is very possible. If not, a more complex solution is needed. Do you want to share the data or copy it? Can they ever be different (A != B)? These sorts of questions help you pick a solution that is right for your design. Again, try to avoid the true global approach unless you are 100% sure its the only way out. Even then, better to fix the underlying issue if you have time.
Reply With Quote
  #6  
Old 10-25-2009, 09:46 AM
maey_kich maey_kich is offline
Registered User
 
Join Date: Oct 2009
Posts: 3
Question

I mean modules as in adding another source of code to the project. from main i'd #include "new module.h"

I can define a variable in the main code file, using a class that stores information about a WAV file, but if i try and use that variable within a function of the module, it tells me that the variable isn't defined. What I need is a way to define the variable, then be able to access its data in any function, in any code source.

Cheers,
Mike
Reply With Quote
  #7  
Old 10-25-2009, 10:07 AM
Danny's Avatar
Danny Danny is offline
Super Moderator
 
Join Date: Nov 2003
Posts: 3,948
As I said, define the array in the global namespace and then declare it 'extern' in every translation unit (that's the correct term, btw) that uses it.
__________________
Danny Kalev
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
modules as DLL's? vchatlive VB Classic 1 07-13-2006 04:48 PM
Access Modules Hemant Sathe VB Classic 2 07-23-2001 11:11 AM


All times are GMT -4. The time now is 10:08 PM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.