0

I have the following code, that I'm using to reorganize some data in a simulation I'm working on. Essentially, I read in a list of components, and then reorganize them into a tensor, where the indexes of the tensor correspond to a spatial position.

The first time I read in the file, just to count the number of lines

int k1 = -1;    
ifstream fin;
fin.open("C:\\Users\\Chelsea\\Documents\\Visual Studio 2015\\Projects\\Full Spin Tracking\\Full Spin Tracking\\Fields\\RoundField1cmGrid.txt"); // open a file
if (!fin.good())
    return 1; // exit if file not found
              //Count lines in file
while (fin.good()) {
    getline(fin, line);
    k1++;
    if (k1 >= 270005) { break; }
}

fin.clear();
fin.seekg(0, fin.beg);  // reset the position of the file to the beginning.
cout << k1 << endl;

Then I set up arrays to hold data, and assign data to these arrays

//Initialize new arrays to hold static field info
double *xx = new (nothrow) double[k1];
double *yy = new (nothrow) double[k1];
double *zz = new (nothrow) double[k1];
double *bbx = new (nothrow) double[k1];
double *bby = new (nothrow) double[k1];
double *bbz = new (nothrow) double[k1];
double *bbmag = new (nothrow) double[k1];

//Write file static field data to 1D arrays
while (fin.good()) {
    data = line.find('\t');
    x1 = stof(line.substr(0, data));
    xx[j1] = x1;

    data2 = line.find('\t', data + 1);
    y1 = stof(line.substr(data + 1, data2));
    yy[j1] = y1;

    data3 = line.find('\t', data2 + 1);
    z1 = stof(line.substr(data2 + 1, data3));
    zz[j1] = z1;

    data4 = line.find('\t', data3 + 1);
    bx1 = stof(line.substr(data3 + 1, data4));
    bbx[j1] = bx1;

    data5 = line.find('\t', data4 + 1);
    by1 = stof(line.substr(data4 + 1, data5));
    bby[j1] = by1;

    data6 = line.find('\t', data5 + 1);
    bz1 = stof(line.substr(data5 + 1, data6));
    bbz[j1] = bz1;

    bmag = stof(line.substr(data6 + 1));
    bbmag[j1] = bmag;

    j1++;
    if (j1 == k1) { break; }
}//end while

fin.close();//Close file

Now, while I never use the integer k1 ever again, I find that if I place a watch on the variable, the value of k1 changes just before the file closes, and I can't figure out why, but I'm suspicious that something else is awry, and I want to make sure that this isn't going to screw up my program anywhere else.

What is causing this value to change?

  • 1
    `k1` may be vanishing on its lifetile ends at end-of-block. Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – MikeCAT Apr 28 '16 at 16:05
  • If you don't use that var. ever again, the compiler is free to reuse whatever storage it was in for other purposes. Try `std::cout`int it after the close and rerun your test with the watch. – Mat Apr 28 '16 at 16:06
  • Also using `while (fin.good())` is just like using `while (!fin.eof)` [which should no be used](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – NathanOliver Apr 28 '16 at 16:09
  • @NathanOliver Do you mean `while (!fin.eof())`? `!fin.eof` emitted [compile error](http://melpon.org/wandbox/permlink/k37IpkWozC8MsND3). – MikeCAT Apr 28 '16 at 16:18
  • @MikeCAT Yeah, I missed the `()` in there. – NathanOliver Apr 28 '16 at 16:19
  • @Mat, Are you saying that the program has the foresight upon being compiled to wipe that piece in the memory? If that were the case, wouldn't the watch on my debugger tell me something about the variable being "Optimized away" rather than a random number? If I ask for the variable again at the end of the program, it seems to remember what's going on. (To print it to the console) – Chelsea Hendrus Apr 28 '16 at 16:59
  • The memory that was used for `k1` may be reused in the code that performs the call to `fin.close()`, since the variable is no longer being used at that point in the code. The debugger's watchpoint doesn't know that the memory is being used for something else. – Barmar Apr 28 '16 at 17:08
  • @ChelseaHendrus a compiler is allowed to emit whatever code it wants, provided that the observable results and side-effects are indistinguishable from it slavishly following the code we write. Very often (particularly in optimised code) your experience with a debugger will be surprising - even to the point that sometimes variables you 'created' don't actually exist at all. – Richard Hodges Apr 28 '16 at 17:18

0 Answers0