Disruptive
Programmer
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);
}
}
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);
}
}