奇怪的全局变量的行为,一旦变量名称更改问题消失 [英] Strange global variable behaviour, once variable name is changed issue disappears

查看:143
本文介绍了奇怪的全局变量的行为,一旦变量名称更改问题消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我大学的练习,我所遇到的一个变量的奇怪的行为。

During my university exercise I have come across strange behaviour of a variable.

/* Main parameters                                                          */
double sizeX, sizeY;      /* Size of the global domain                      */
int nPartX, nPartY;       /* Particle number in x, y direction              */
int nPart;                /* Total number of particles                      */
int nCellX, nCellY;       /* (Global) number of cells in x, y direction     */
int steps;                /* Number of timesteps                            */
double dt;                /* Stepsize for timesteps                         */
int logs;                 /* Whether or not we want to keep logfiles        */

void ReadInput(const char *fname)
{
  FILE *fp;
  char c;

  Debug("ReadInput", 0);
  if(rank == 0)
  {
    fp = fopen(fname, "r");
    if(!fp) Debug("Cannot open input file", 1);
    if(fscanf(fp, "sizeX: %lf\n", &sizeX) != 1) Debug("sizeX?",  1);
    if(fscanf(fp, "sizeY: %lf\n", &sizeY) != 1) Debug("sizeY?",  1);
    if(fscanf(fp, "nPartX:%i\n", &nPartX) != 1) Debug("nPartX?", 1);
    if(fscanf(fp, "nPartY:%i\n", &nPartY) != 1) Debug("nPartY?", 1);
    if(fscanf(fp, "nCellX:%i\n", &nCellX) != 1) Debug("nCellX?", 1); //read value is 10
    if(fscanf(fp, "nCellY:%i\n", &nCellY) != 1) Debug("nCellY?", 1);    
    if(fscanf(fp, "steps: %li\n", &steps) != 1) Debug("steps?",  1);    
//here the nCellX variable value 10 is changed somehow to 0
    if(fscanf(fp, "dt:    %lf\n", &dt)    != 1) Debug("dt?",     1);
    if(fscanf(fp, "logs:  %c\n",  &c)     != 1) Debug("logs?",   1);
    logs = (c == 'y');
    fclose(fp);
  }

  printf("(%i) reporting in...\n", rank);

  MPI_Bcast(&sizeX, 1, MPI_DOUBLE, 0, grid_comm);  
  MPI_Bcast(&sizeY, 1, MPI_DOUBLE, 0, grid_comm);
  MPI_Bcast(&nPartX,1, MPI_INT,    0, grid_comm);  
  MPI_Bcast(&nPartY,1, MPI_INT,    0, grid_comm);
  MPI_Bcast(&nCellX,1, MPI_INT,    0, grid_comm);
  MPI_Bcast(&nCellY,1, MPI_INT,    0, grid_comm);
  MPI_Bcast(&steps, 1, MPI_INT,    0, grid_comm);
  MPI_Bcast(&dt,    1, MPI_DOUBLE, 0, grid_comm);
  MPI_Bcast(&logs,  1, MPI_INT,    0, grid_comm);
  nPart = nPartX * nPartY;
  dt2 = dt * dt;
}

老师,我的结论是,如果我们从nCellX到nCellX_2改变变量名,问题消失,code按预期工作。另一个有趣的事情是,仅此单一的全局变量有这个问题,其他变量工作正常。我想知道做跨这类问题的人来为好。任何指引/解释是AP preciated。

Teacher and I have concluded that if we change the variable name from "nCellX" to "nCellX_2", the problem disappears and the code works as expected. Another interesting thing is that only this single global variable have this problem, other variables works correctly. I was wondering does anyone came across this type of problem as well. Any guideline/explanation would be appreciated.

如果这个问题不够清晰,让我知道,如果还满code需要我可以提供这一点。一般来说,code是一个粒子,在单元的并行算法。

If this problem is not clear enough let me know, also if full code is required I can provide that as well. In general the code is a parallel algorithm of a Particle-in-Cell.

推荐答案

这可能是code的下面一行导致问题:

It is possible that the following line of code is causing a problem:

if(fscanf(fp, "steps: %li\n", &steps) != 1) Debug("steps?",  1);

%利表示一个长整型,这可能是64位,而步骤 INT ,这可能是32位。格式说明符应%I 而不是%礼

The %li indicates a long integer, which might be 64-bits while steps is an int, which might be 32-bits. The format specifier should be %i instead of %li.

是否存在一个实际问题取决于环境(例如,它是最有可能的一个问题,如果建立一个64位应用程序)。如果有64位和32位的不匹配,那么的fscanf 通话将覆盖内存并可能破坏任何变量如下步骤在内存布局(这可能是 nCellX )。请注意,使用 -Wall 选项应该警告你这种情况。为什么改变nCellX的名称不同的东西应该掩盖问题并不清楚,但它似乎改变名称可能导致的变量在存储器中的布局的变化;我怀疑是由C标准不允许的(虽然我没有看过)。

Whether there is an actual problem depends on the environment (e.g., it is most likely an issue if building a 64-bit application). If there is that 64-bit vs 32-bit mismatch, then the fscanf call will overwrite memory and possibly destroy whatever variable follows steps in the memory layout (and that could be nCellX). Note that using -Wall option should warn you about this situation. Why changing the name of nCellX to something different should mask the problem is not clear, but it would seem that changing the names may be resulting in a change in the layout of the variables in memory; I doubt that is disallowed by the C standard (although I have not looked).

这篇关于奇怪的全局变量的行为,一旦变量名称更改问题消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆