Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared variables

Status
Not open for further replies.

Disruptive

Programmer
Mar 8, 2005
40
GB
Hi

Am having a bit of trouble with variables not updating when they should when they are declared with the extern keyword. I have taken an example from the web to try this out and still it does not work. Do you have any idea where this is going wrong.

I keep getting the old values and the new values never appear. The variables are there, but for some reason when I change their initial value, this is never reflected in the code. Is there a memory issue here?

Failing this is there anyway of getting variables across several files easily and elegantly?

Thanks

/*----------------------------------------------------------------------------
VARS.H

Note: #define VAR_DECLS 1 before including this file to DECLARE and INITIALIZE
global variables. Include this file without defining VAR_DECLS to extern
these variables.
----------------------------------------------------------------------------*/
#ifndef CNF_DEFS // Make sure this file is included only once
#define CNF_DEFS 1

/*----------------------------------------------
Setup variable declaration macros.
----------------------------------------------*/
#undef CNF_DECLS
#ifndef CNF_DECLS
# define _DECL extern
# define _INIT(x)
#else
# define _DECL
# define _INIT(x) = x
#endif

/*----------------------------------------------
Declare variables as follows:

_DECL [standard variable declaration] _INIT(x);

where x is the value with which to initialize
the variable. If there is no initial value for
the varialbe, it may be declared as follows:

_DECL [standard variable declaration];
----------------------------------------------*/

_DECL int Xsections _INIT(5);
_DECL int Ysections _INIT(5);
_DECL int Zsections _INIT(5);
_DECL int MaxLipidsinCell _INIT(40);
_DECL double PI _INIT(3.14159);
_DECL int LipidTotal _INIT(9);
_DECL int Beads _INIT(2);
_DECL double sigma _INIT(1.0);

#endif
/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/





and the .cpp file:


#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>


#define CNF_DECLS
#include "Vars.h"
//#include "Sorts.h"






int main(void)
{

int myarray[Beads][LipidTotal];
int myarrayEnd[Beads];
int TotalArray[LipidTotal];
int TotalArrayEnd;
int duplicates;


myarray[0][0] = 0;
myarray[0][1] = 134;
myarray[0][2] = 1423;
myarray[0][3] = 1323;
myarray[0][4] = 121;
myarray[0][5] = 13;
myarray[0][6] = 1321;
myarray[0][7] = 112;
myarray[0][8] = 12;
myarrayEnd[0] = 9;
myarrayEnd[1] = 9;
myarray[1][0] = 0;
myarray[1][1] = 0;
myarray[1][2] = 0;
myarray[1][3] = 0;
myarray[1][4] = 0;
myarray[1][5] = 0;
myarray[1][6] = 0;
myarray[1][7] = 0;
myarray[1][8] = 0;

printf("Lipid %d\n",LipidTotal);
// AddListsTogether(TotalArray,TotalArrayEnd,myarray,myarrayEnd);
for (int i=0;i<(TotalArrayEnd);i++)
{
//printf("before [%d]: %d\n",i,TotalArray);

}


}
 
The only extern variables that I see being used in main() are LipidTotal and Beads. Neither of them appear to be assigned any values. I also don't see where they are defined - I just see them declared in Vars.h.
 
They should be declared automatically as they are included and declared, Look at the #define portion. The variables are most certainly assigned. However their value cannot be changed on susequent compile and run.
 
Oh I see now, you #define CNF_DECLS before you include Vars.h, in which case they are assigned a value but not extern... But I still don't see where you are changing their values.
These are the only places I see the global variables being referenced:
Code:
 int myarray[Beads][LipidTotal];
 int myarrayEnd[Beads];
 int TotalArray[LipidTotal];
...
printf("Lipid %d\n",LipidTotal);
 
I agree with cpjust, you need to show where you're trying to modify those globals, not just a few snippets of commented out code.

Oh, and that macro trickery is really tacky IMO.


--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top