Data Chanturia
Data Chanturia

Reputation: 149

XOR identical data in C leads to nonzero value. Why?

How can I xor two data segments pointed by pointers?

I've tried this, one based on a similar solution on stackoverflow, but the output is not what I expected.

Here is the code:

void printXor(){    
    int j;
    char* c = strdup("hey");
    for(j = 0; j < strlen(c); j++){
        c[j] ^= c[j];
    }

    printf("%d\n", *(int*)(c));
}

But the output is: 7955712. Shouldn't the output be 0? I'm xoring "hey" over "hey" and its value in int is 0 right?

Upvotes: 2

Views: 167

Answers (2)

Bathsheba
Bathsheba

Reputation: 234795

Strictly speaking, the behaviour of your code is undefined due to an aliasing violation in reading a char array as an int.

You can recast the crux of the question to the well-defined

#include <stdio.h>
#include <stdint.h>
int main(){    

    int32_t n;
    char* c = &n;
    c[0] = 'h';
    c[1] = 'e';
    c[2] = 'y';
    c[3] = 0;

    for( int j = 0; j < strlen(c); j++){
        c[j] ^= c[j];
    }
    printf("%" PRId32 "\n", n);
}

The output is not zero since only one iteration of the for loop runs since a subsequent valuation of strlen will be 0, since c[0] will evaluate to the NUL-terminator.

Upvotes: 1

user1143634
user1143634

Reputation:

Take a look at this loop:

for(j = 0; j < strlen(c); j++) {
    c[j] ^= c[j];
}

You are modifying c and calculating its length using strlen. After the first iterator strlen returns 0 and loop stops.

If you translate 7955712 to hex it is 0x796500. 0x79 is a code for 'y' and 0x65 is a code for 'e' and the least significant byte is 0x00. Since you are running this on a little endian machine, you get an empty string.

Upvotes: 9

Related Questions