I'm having trouble with XOR doubly link list that has one pointer that must be contain NEXT and PREV pointers of nodes. I must xor address of pointers to do so, but I can't. I can allocate a memory address that XORed from two address but I can't get value to its address (this has segmentation fault error):
int main(){
int* ptr = new int;
int *ptr2 = new int;
ptr2 = (int*)((unsigned long)ptr ^ (unsigned long)ptr2);
*ptr2= 5; /here has segmentation fault
cout <<*ptr2;
return 0;
Why does this code have an error? How can I fix it?
thanks for your response but I cant transfer my idea I say my question in other words: normally we have a pointer that allocate the space of memory to it by "new" (in c++) keyword. this address that reserve for our pointer is determine by the os,correct? for example the address that pointer points to it is 0x8f3400b (this is where the memory is free that can reserve) I want to do that manually by addressing not with new keyword like this code:
int* ptr1 = (int*) 0x2355;
int* ptr2 = (int*) 0x23ff;
now i dont know the address 0x2355 and 0x23ff can be reserved or not ? then i decide that "new" these pointers and then XORing them, like below:
int* ptr1 = new int //ptr1 is now manage by OS
int* ptr2 = new int //like ptr1 ...
then I want XORing these pointers and make a new space to append a node that is the third node but i cant the addressing of it is correct but when i want to valuing it segmentation fault occured:
int* ptr1 = new int; // for example the address is X
int* ptr2 = new int; // for example the address is Y
int* ptr3 = (int*)((unsigned long)ptr1 ^ (unsigned long)ptr2); //the final address is X^Y
how can i correct this? i dont know how use intptr_t and other please help me thanks