|
From: Chandra on 30 Nov 2007 02:58 Hi, I have three files in which i am using 10 arrays each of size( say about 600 i.e A1[600],A2[600] ......A10[600]). I am having some memory issues so i want to make use of the memory in a appropraite way. These array are declared as global in one of the files and are being used in the other files. But my concern i want to declare them in one of the file as local to that file and make them available in other files too. The reason behind this is the array declared global are available to the other files which don't require the access to these arrays. So on a whole my query is : How can we use the variables which are declared local to one class. Probably Inheritance is one of the ways to acheive this. But i don't require this. Is there any other way to do this. Any suggestion will be helpful to me. Thnx in advance.. Chandra.
From: Ian Collins on 30 Nov 2007 03:08 Chandra wrote: > Hi, > Please don't multi-post on Usenet. -- Ian Collins.
From: Francis Glassborow on 30 Nov 2007 06:49 Chandra wrote: > Hi, > > I have three files in which i am using 10 arrays each of size( say > about 600 i.e A1[600],A2[600] ......A10[600]). I am having some memory > issues so i want to make use of the memory in a appropraite way. Yes, I can understand you might have memory issues but I do not think you understand why. The above will consume 6000 units of whatever type you are placing in the array. Some compilers have a default size for static memory (that used by global variables). In the long distant past, IIRC Borland's C had a default of only 4K. These > array are declared as global in one of the files and are being used in > the other files. Yes so? Perhaps you could provide the declarations you are using both in the file where you are creating them and in the files where you are accessing them. But my concern i want to declare them in one of the > file as local to that file and make them available in other files too. Yes, what is the problem. Well I can guess but if you show the relevant code I won't have to, > The reason behind this is the array declared global are available to > the other files which don't require the access to these arrays. And why is that a problem? Actually the aren't they are only accessible if you declare (not define) the array names in the files (translation units) where you want to use them. However this has no impact on memory use. > > So on a whole my query is : How can we use the variables which are > declared local to one class. Do you mean 'class' or 'file' they are different things. > > Probably Inheritance is one of the ways to acheive this. Why would you think that? But i don't > require this. Nor should you, it has nothing to do with the problem you have described. Is there any other way to do this. You mean like the correct way? An example (that is about global arrays rather than class scope statics) in file 1: int array[600]; // note that as a global this will be zero initialised in files that require access: extern int array[]; // note that this is one of the places you must be explicit and use the // array syntax and not the pointer syntax Alternative using a class: in the class definition file (i.e. header for the class) class storage { public : static int array[600]; // ... }; In the class implementation file (e.g. cpp file) int storage::array[600] = {}; Now storage::array will be visible and accessible in all files that include the class header. Notwithstanding the above, I would look for a better design but without knowing what you are really trying to do I cannot say what that would be (but I would almost certainly want to use a std::vector and probably a singleton and also some form of tracking (possibly a flag) to identify when the arrays contained live data rather than just default initialisation) > > Thnx in advance.. > > Chandra. -- Note that robinton.demon.co.uk addresses are no longer valid.
From: Jim Langston on 30 Nov 2007 04:59 "Chandra" <forqueries(a)gmail.com> wrote in message news:49d0dc9b-1ce7-4741-b733-7ff8d13aa3d0(a)f3g2000hsg.googlegroups.com... > Hi, > > I have three files in which i am using 10 arrays each of size( say > about 600 i.e A1[600],A2[600] ......A10[600]). I am having some memory > issues so i want to make use of the memory in a appropraite way. These > array are declared as global in one of the files and are being used in > the other files. But my concern i want to declare them in one of the > file as local to that file and make them available in other files too. > The reason behind this is the array declared global are available to > the other files which don't require the access to these arrays. > > So on a whole my query is : How can we use the variables which are > declared local to one class. > > Probably Inheritance is one of the ways to acheive this. But i don't > require this. Is there any other way to do this. Any suggestion will > be helpful to me. You have two alternatives to a global array, while still keeping it global. One is to use a global pointer then allocate the memory using new and free it with delete[]. Another choice is a global vector. However you do the global variable, in one file you declare it as normal, I.E. std::vector<int> A1; int* B1 = NULL; int C1[600]; However you do to. You would put that in a .cpp file. Now, in the other files (or a header which you include) to tell the compiler to find this variable somewhere else you would use the "extern" keyword, which means external. Like this: extern std::vector<int> A1; extern int* B1; extern C1[600]; Remember, however, that global variables are usually not the best idea. You should limit the use of global variables in your program if you can, but sometimes we just need to use them.
From: Barry Schwarz on 2 Dec 2007 11:40 On Thu, 29 Nov 2007 23:58:13 -0800 (PST), Chandra <forqueries(a)gmail.com> wrote: >Hi, > >I have three files in which i am using 10 arrays each of size( say >about 600 i.e A1[600],A2[600] ......A10[600]). I am having some memory >issues so i want to make use of the memory in a appropraite way. These >array are declared as global in one of the files and are being used in >the other files. But my concern i want to declare them in one of the >file as local to that file and make them available in other files too. >The reason behind this is the array declared global are available to >the other files which don't require the access to these arrays. > >So on a whole my query is : How can we use the variables which are >declared local to one class. > >Probably Inheritance is one of the ways to acheive this. But i don't >require this. Is there any other way to do this. Any suggestion will >be helpful to me. > Moving object from global to local usually makes memory problems worse unless you are dynamically allocating a lot of memory. If you still want the arrays to be local to one function, your two choices are to pass the addresses of the arrays to the other functions that need them as arguments in the function call statement or to define global pointers that the other functions can use to access the arrays. Remove del for email
|
Pages: 1 Prev: switch statement, case label and braces Next: std::vector as function parameter |