title: How to resolve a weird changing value?
body: <p>I am making a virtual machine for a small computer language. This virtual machine is developed in C using the GNU utility Flex. The project compilation is therefore with GNU GCC and then Flex.</p>
  
  <p>Within this virtual machine, I have a GC Stop &amp; Copy. Before my changes, GC's working memory could not enlarge - e.g go from 512 bytes to 1024 bytes if the first flip has not optimized the space used to be able to make a new allocation.</p>
  
  <p>This changes seemed to work. In fact, I don't know if it worked really since those changes, but now I have a bug. It just appeared in the first flip. Indeed, when it comes copying data, <strong>I have a <em>constant</em> variable that changes</strong>. But this variable is important because it points to the item that I want to copy. In the Stop &amp; Copy, this variable is used to change a <em>slot</em> (here <code>SLOT_FORWARD</code>) to inform the new position of the data in memory (in case we would still copy).</p>
  
  <p>So I have a loop that copies each box of previous container whose position in memory is specified by the variable <code>old</code>. And I have a new container which is filled from <code>addr</code> position. <strong>But the old value is changed during iteration!</strong> And after the copy, I want to change the slot forward to put the new container's address. But as old has changed, you can imagine that I record this value in the wrong place.</p>
  
  <p>So I spent a long time debugging cases where this happens very rarely (it happens some times after 2 flip with 3 to 4 container). I use GDB to be aware the value changed in one of my debug function (while she was also amended by adding debug functions). I then changed compiler (clang to gcc) to restart GDB and see that it was a brace character (still in debug function) that changed the value ... Finally, I put all my parameters for all functions <code>const</code> when it was possible, and now I am told that the value have been changed in the file <code>iofwrite.c</code> line 37. It is therefore a mistake from another world.</p>
  
  <p>The code in question where the bug is here:</p>
  
  <pre><code>static t_case
  copy(t_dono *dono, const t_case old)
  {
    t_case  addr;
    t_case  size;
    t_case  temp;
    int     i;
  
    temp = old;
  
    if (mem[old + SLOT_FORWARD] &gt;= ns
        &amp;&amp; mem[old + SLOT_FORWARD] &lt;= ts)
      return (mem[old + SLOT_FORWARD]);
    else
      {
        addr = mp;
        size = mem[old + SLOT_SIZE];
        i = 0;
  
        fprintf(stderr, "change:\t");
        dump(stderr, mem, old);
  
        assert(old == temp);
  
        while (i &lt; size)
          {
            fprintf(stderr, "!!!COPY:\t");
            dump(stderr, mem, old);
            assert(old == temp);
            mem[addr + i] = mem[old + i]; /* BUG IS HERE */
            i = i + 1;
          }
  
        mem[old + SLOT_FORWARD] = addr;
        fprintf(stderr, "change:\t");
        dump(stderr, mem, old);
        assert(old == temp);
        mp = mp + size;
  
        return (addr);
      }
  }
  </code></pre>
  
  <p>As you can see, I did a lot of debug to target the error and I got this log file:</p>
  
  <pre><code>ref:            [ 0005 0001 0003 0004 0035 ]
  copy:           [ 0007 0001 0003 0004 0075 0001 00f9 ]
  change:         [ 0007 0001 0003 0004 0075 0001 00f9 ]
  !!!COPY:        [ 0007 0001 0003 0004 0075 0001 00f9 ]
  !!!COPY:        [ 0007 0001 0003 0004 0075 0001 00f9 ]
  !!!COPY:        [ 0007 0001 0003 0004 0075 0001 00f9 ]
  !!!COPY:        [ 0003 0001 0003 ]
  !!!COPY:        [ 0003 0004 0003 ]
  !!!COPY:        [ 0003 0004 0075 ]
  !!!COPY:        [ 0003 0004 0075 ]
  change:         [ 0003 0033 0075 ]
  </code></pre>
  
  <p>I also used Valgrind which told me that many errors but only after this bug (which is normal since the GC will be accessing random data now). During this change of variable, I have absolutely no errors.</p>
  
  <p>We can see that other containers passing through function copy (l: 662) do not get this undefined behavior (see the log file at line 10, 48, 54, 66, 82, 120, 126 and 134) . It is only at execution time that everything goes wrong, which, of course, erroneous all GC data.</p>
  
  <p>The code is really long (about 1000 lines) because the goal is to run the VM in a single file C. I'm sorry I can not make the code clearer. But the problem just appears magically and I am not able to go further and make the language of the future that will surpass Python (joke).</p>
  
  <p>The link of repository is: <a href="http://git.osau.re/?p=dinoscheme.git;a=summary" rel="nofollow">git.osau.re</a><br>
  The change's link is: <a href="http://ompldr.org/vaHVuZQ/sandbox.tar.gz" rel="nofollow">ompldr</a></p>
  
  <p>Kind regards.</p>