简体   繁体   中英

C++ Global variable and function

I am programming in C++ with using different functions. I have a problem with the declaration and usage of local and global variables. There are many variables which first have to used in main, and also need to be used in the functions.

SO, I declared some global variables. I also want to allocate some memory outside main() .

But now the code does not seem to work. Please help me solve this issue.

I have:

int *mb = new int [c];  //declare variables before you use them
matrix *M = new matrix  [n+1+m];

When I use the above two variables as global variables then my code does not work.

Here is a part of my code:

int p=4, n=5, m=5;
int num;
struct matrix{
public:
int row;
int col;
int val;
};

int P(int k){
return k-1;
}

void M_to_C(){

int *mb,*mc,*mi;
double *mv;
int i, cur_col;
matrix*M;
cur_col=-1;
for(i=0; i<= num; i++) {
    cur_col=M[i].col;
    mb[cur_col]=i;
    mc[cur_col]=1;
    mi[i]=M[i].row;
    mv[i]=M[i].val;
}}

int main (int argc, char **argv){
int status = 0;

int project = 4; 
int employee = 5;
int time = 5; 

int empl [] = {2, 2, 2, 3};
int meet [] = {2, 4, 3, 3};

int NUMCOLS=n+m;

double *lb = new double [NUMCOLS];
double *ub = new double [NUMCOLS];
double *x  = new double [NUMCOLS];
int    *mb= new int [NUMCOLS];
int    *mc= new int [NUMCOLS];
char   *ctype = new char [NUMCOLS];

int **F = new int*[employee]; 
for(int a = 1; a <= employee; a++){     
    F[a] = new int [time];
    for(int b = 1; b <= time; b++){
        F[a][b]=rand()%2;
    }}

int NUMROWS=0;  

for(int i=1; i<=n; i++){        
    for(int j=1; j<=m; j++){ 
        for(int l=1; l<=p; l++) {
            if(F[i][j] ==0){            
                NUMROWS++;
                                    num++;
            }
        } 
    }
}

int    *mi= new int [2*NUMROWS+m];
double *mv= new double [2*NUMROWS+m];
matrix_entry *M = new matrix_entry  [num+1+m];

num=-1;
int row=-1;
for(int i=1; i <= n; i++) {
    for(int j=1; j <= m; j++) {
        for(int k=1; k <= project; k++) {
            if(F[i][j] ==0) {
                row++;
                num++;
                M[num].col=P(k); 
                M[num].row=row;
                M[num].val=-1;
            }}}}

M_to_C();

You can define them as globals and populate them in main thus:

int *mb;
matrix *M;

int main () {
    mb = new int [c]; 
    M = new matrix  [n+1+m];
    :
}

But I should warn you that global variables (and short variable names like mb , lb and ub ) are generally considered a bad idea.

You would be better of limiting their accessibility by placing them in a class or passing them around as references, as needed.

Inside the M_to_C() function you do some operation on unitialized pointer M , so basically this is undefined behaviour:

matrix*M;
cur_col=-1;
for(i=0; i<= num; i++) {
    cur_col=M[i].col; // access local uninitialized pointer M

In order to fix it you should initialize M property before using it. In your case local variable M hides global variable. I would propose to pass M into M_to_C() function, so that it would operate on existing object:

void M_to_C(matrix * M)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM