title: Passing struct by reference updating it and then getting different member value in main
body: <p>Title says it all here is the code in question :</p>
  
  <p>Basically what happens is that the value of the pipeline struct member args changes at stages of the program where it shouldn't, this is obviously an issue as it causes segfaults in other bits of the program that rely on its value.</p>
  
  <p>This in main :</p>
  
  <pre><code>int cycle = 1;
    do {
      printf("Block here %i \n", cycle);
      if (cycle == 3) {
        printf("%u, args[2]\n", pip_ptr-&gt;decoded_args[2]);
      }
      pip_ptr-&gt;decoded (pip_ptr-&gt;decoded_args, mach_ptr);
      decode(cycle, pip_ptr-&gt;fetched, pip_ptr, mach_ptr); 
      if(cycle == 2) {
        printf("%u, before fetch args[2]\n", pip_ptr-&gt;decoded_args[2]);
      }
      fetch(pc, pip_ptr, mach_ptr);
      printf("%u fetched\n", *(pip_ptr-&gt;fetched));
      if(cycle == 2) {
        printf("%u, after fetched args[2]\n", pip_ptr-&gt;decoded_args[2]);
      }
      pc += 4;
      cycle ++;
      output_machine_state(mach_ptr);
    } while (pip_ptr-&gt;halt == 0);
  </code></pre>
  
  <p>Here is the code decode_data_proc which is where the arguments are passed in decode.</p>
  
  <pre><code>`void decode_data_proc(uint32_t instr, struct pipeline *pip, struct machine_state
                      *mach) {
  void (*op_ptrs[14]) (uint32_t* args, struct machine_state *mach);
  
  op_ptrs[0] = &amp;and;
  op_ptrs[1] = &amp;eor;
  op_ptrs[2] = &amp;sub;
  op_ptrs[3] = &amp;rsb;
  op_ptrs[4] = &amp;add;
  op_ptrs[8] = &amp;tst;
  op_ptrs[9] = &amp;teq;
  op_ptrs[10] = &amp;cmp;
  op_ptrs[12] = &amp;orr;
  op_ptrs[13] = &amp;mov;
  
  pip-&gt;decoded_args = process_args(instr, mach);
  pip-&gt;decoded = op_ptrs[extract_bits(instr, 21, 4)];
  printf("%u passed dest reg\n", pip-&gt;decoded_args[2]);
  }`
  </code></pre>
  
  <p>Now all the code that decode_data_proc references</p>
  
  <pre><code>uint32_t ror(int reg, int shiftvalue, struct machine_state *mach) {
  if (shiftvalue != 0) {
    printf("ror : non zero shift\n");
    mach-&gt;shifter_carryout = read_bit(reg, shiftvalue-1);
    int firstYbits = reg &lt;&lt; (32 - shiftvalue);
    int lastbits   = reg &gt;&gt; shiftvalue;
    return (firstYbits | lastbits);
  } else {
    uint32_t ret = reg;
    printf("%i shift is 0 ror\n", ret);
    return reg;
  }   
  
  
  uint32_t* process_args(uint32_t instr, struct machine_state *mach) {
    uint32_t rn = extract_bits(instr, 16, 4);
    uint32_t rd = extract_bits(instr, 12, 4);
    uint32_t o2 = extract_bits(instr,0, 12);
    bool imm = read_bit(instr, 25);
    uint32_t flagS = read_bit(instr, 20);
  
    uint32_t (*shift_ptr[4]) (int x, int y, struct machine_state *mach);
    shift_ptr[0] = lsl;
    shift_ptr[1] = lsr;
    shift_ptr[2] = asr;
    shift_ptr[3] = ror;
  
    if (imm) {
      printf("%i before ror", extract_bits(instr, 0, 4));
      o2 = ror(extract_bits(instr,0, 8), 2 * extract_bits(instr, 8, 4),     mach);
    } 
    if (!imm &amp;&amp; read_bit(instr,4)) {
      int reg = extract_bits(instr, 0, 4);
      int shift = extract_bits(instr, 5, 2);
      int sregv = extract_bits(mach-&gt;registers[extract_bits(instr, 8, 4)], 0,  8);
      o2 = (*shift_ptr[shift]) (mach-&gt;registers[reg], sregv, mach); 
    } 
    if (!imm &amp;&amp; !read_bit(instr, 4)) {
      int reg = extract_bits(instr, 0, 4);
      int shift = extract_bits(instr, 5, 2);
      uint8_t val = extract_bits(instr, 7, 5);
      o2 = (*shift_ptr[shift]) (mach-&gt;registers[reg], val, mach);
    }
  
    uint32_t *ret = (uint32_t*) malloc(4* sizeof(int32_t));
    uint32_t arr[4] = {rn, o2, rd, flagS};
    ret = &amp;arr[0];
    printf("%i, dest register as about to be returned\n", ret[2]);
  
    return ret;
  }  
  </code></pre>
  
  <p>If you guys are wondering I am trying to emulate a ARM11 architecture and args is where the arguments to the instruction to be executed are stored.</p>