0

Without changing the byte[] numArray both Message Boxes show a different output even though the bytes stay exactly the same. Im confused.

Result of the first MessageBox: stream:stream to="" version="1.0" xmlns:stream="http://etherx.jabber.org/streams">

Result of the second MessageBox: ���F^��v

Third MessageBox: "Match"

MessageBox.Show(System.Text.Encoding.UTF8.GetString(numArray));
byte[] num1 = numArray;
byte[] encrypted =  getEncryptedInit(numArray);
MessageBox.Show(System.Text.Encoding.UTF8.GetString(numArray));
byte[] num2 = numArray;
if (num1.SequenceEqual<byte>(num2) == true)
{
    MessageBox.Show("Match");
}
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
user2880751
  • 23
  • 2
  • 7
  • @CodeCaster: Not a duplicate. While the information from the other question is definitely relevant to the solution, or even for debugging in order to find a solution, the questions are quite different, as evidenced by the correct answers. Compare the (shortened) correct answers to the other question ("The reference is passed by value.") and to this question ("`getEncryptedInit` must be modifying the contents of `numArray`."), which are by no means interchangeable. – O. R. Mapper Dec 03 '13 at 14:25
  • @O.R.Mapper I don't care that the _question_ isn't an exact duplicate (there is no question in this question anyway). My point is: this question is _answered in that question and its answers_: the array reference is passed by value to `getEncryptedInit()`, so the array modified in there is the same array as `numArray` points to. Marc's answer there indicates this: _"That reference is passed by value, meaning that changes to the contents of the array will be seen by the caller"_. The alternative would've been a close vote for not understanding, I think what I did was more constructive. – CodeCaster Dec 03 '13 at 14:40
  • Also keep in mind that a single duplicate close vote does not harm a question, it only gets closed after four more votes. A comment gets auto-inserted with a link to the possible duplicate, which OP can click on and learn from. If OP doesn't understand or doesn't agree with the possible duplicate, he or she can update the question or add a comment to explain the parts they don't get (and I do choose my duplicates carefully and retract the vote if OP proves me wrong). On the other hand the possible duplicate might just provide OP with the insight required to solve the problem at hand. – CodeCaster Dec 03 '13 at 15:04
  • @CodeCaster: As I pointed out, the answer to this question is that `numArray` must be modified within `getEncryptedInit` (because it is the one piece of code we are not seeing). The fact that the debugging method used by the OP did not yield the desired insight was related to array variables being references, as explained in the other question and its answers, is definitely, helpful, but is not necessarily related to the original problem. Based on the information we have, `getEncryptedInit` might also modify `numArray` as a side-effect, not via the passed reference. – O. R. Mapper Dec 03 '13 at 16:24
  • @CodeCaster: Of course, a single close vote does not close the question. It just means that a possible reason for closing the question has been brought forth, and it is the right time to utter possible counter-arguments to the close vote, which is what I have done, *before* other users who might not think of those counter-arguments cast additional close votes and thus do end up closing the question. – O. R. Mapper Dec 03 '13 at 16:26
  • @O.R.Mapper there are infinite ways of describing this problem, does each way deserve its own question? The problem is: _"changes to the contents of the array will be seen by the caller"_. The cause of that problem is explained in the duplicate. – CodeCaster Dec 03 '13 at 16:31
  • @CodeCaster: As I have pointed out, that is not necessarily the problem. Please read my explanations again, and sorry if I did not explain it clearly enough. – O. R. Mapper Dec 03 '13 at 16:32
  • @O.R.Mapper you don't have to be sorry, I do understand you, but I'm afraid I just don't agree. If the most obvious solution (_"the array must be modified in the method"_) isn't true but the scenario you made up is, this question should be closed as offtopic because the code shown doesn't reproduce the problem. I think my close vote is more constructive, as it points to a resource where OP can learn more. Again, I have given **OP** more than enough time to respond, so the close vote stays. Let's not litter his inbox any more. :) – CodeCaster Dec 03 '13 at 16:56

1 Answers1

6

getEncryptedInit must be modifying the contents of numArray.

Since num1 and num2 both point to numArray, of course they would be equivalent.

Remember, arrays are reference types, so when you say num1 = numArray, you're just pointing the num1 variable to the same location in memory that numArray points to. If you really want to capture what numArray looks like at a specific point in time, you would have to make a copy of it, rather than just doing a simple assignment.

Consider the following example:

void DoStuff(byte[] bytes) {
  for (int i = 0; i < bytes.Length; i++) {
    bytes[i] = 42;
  }
}

bool Main() {
  // This allocates some space in memory, and stores 1,2,3,4 there.
  byte[] numArray = new byte[] { 1, 2, 3, 4 };

  // This points to the same space in memory allocated above.
  byte[] num1 = numArray;

  // This modifies what is stored in the memory allocated above.
  DoStuff(numArray);

  // This points to the same space in memory allocated above.
  byte[] num2 = numArray;

  return (num1 == num2 == numArray); // always true
}
RobSiklos
  • 8,348
  • 5
  • 47
  • 77
  • but both the second MessageBox and num2 point to numArray after getEncryptedInit is called. So how can one point to the modified version and the other one not? – user2880751 Dec 03 '13 at 14:15
  • 1
    No - they all point to the same place - it's the same location in memory. `getEncryptedInit` is just modifying what is stored in that memory. – RobSiklos Dec 03 '13 at 14:16
  • @user2880751: To see things more clearly here, replace `numArray` with `(byte[])numArray.Clone()` in your assignments. – O. R. Mapper Dec 03 '13 at 14:22
  • 1
    @user2880751 Between your first and second message box you modified numArray, therefore the output is different. The third message box compares two references that both reference that same array. Regardless of its contents that comparison will be true. When you assign byte[] num1 = numArray, it does not copy the array, it just creates a new name for it. – Alan Dec 03 '13 at 14:22