简体   繁体   中英

C++ passing parameter by reference strange behavior

I have a very simple C++ function, that passes a value by reference:

int read_buf(unsigned char *buf, int num_bytes, unsigned char *input_file, long &position)
      {



          for (int i=0; i<num_bytes; i++){
               buf[position] =  input_file[position];
             //  buf++;
             //  input_file++;
               position = position +1;
          }


          return 0;
    }

And, it works fine if I just call this function inside my main() function:

for example:

unsigned char *input_buf = (unsigned char *) malloc(lSize * sizeof(unsigned char));

int result = fread (input_buf,1,lSize,input_file);

rewind (input_file);

unsigned char *test_buf = (unsigned char *) malloc(lSize * sizeof(unsigned char));

long pos = 0;

read_buf(test_buf, 1000, input_buf, pos); 
read_buf(test_buf, 1000, input_buf, pos);
read_buf(test_buf, 1000, input_buf, pos);

And, when I run the code, I can see that the value of position increments (0 -> 1000, 1000 -> 2000, 2000 -> 3000)

Now, the problem comes when I try to do the same thing in an Object. I define an Object, and I declare a public variable of type long for the "position" variable, and define the EXACT above function as a method for that object. Now the problem is that when I run the method, the value of "position" doens't increment more than 541??? For example when I run something like this:

  read_buf(test_buf, 1000, input_buf, pos); 

The value of pos keeps incrementing to 541, and all of a sudden the next value, instead of changing to 542, it goes to 535 !!?? And it keeps incrementing and when it reaches 541, it goes back to 535, and this pattern keeps repeating !!! I have no idea why its is not incrementing simpliy just like the case that I used it in the main() function!!!

Any idea? Many thanks in advance

Update:

When I changed the above function to something like this, I could see that the variable "position" increments okay now... Well even though it doesn't quite work, becasue it breaks at some other parts, but at least I can see that it is incrementing correctly.

   int read_buf(unsigned char *buf, int num_bytes, unsigned char *input_file, long &position)
      {
          long temp = position;
          for (int i=0; i<num_bytes; i++){
               buf[temp] =  input_file[temp];
             //  buf++;
             //  input_file++;
            // position = position + 1;
               temp = temp +1;
          }
          position = temp;

          return 0;
    } 

Does anyone know why the first one was not working? I beleive that the value of "position" was getting updated as an Ivalue when (buf[position] = input_file[position]) was being implemented. But any idea WHY the value of position was changing? I am sure that I didn't hit end of file. Input_file has a huge size (over 5000), and this was happening around way low (only around position 541) !!

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