What happens when two pointers are pointing to the same address? Is this going to cause a security problem?
Yes, two pointer variables can point to the same object: Pointers are variables whose value is the address of a C object, or the null pointer.
Yes. A pointer p to type T can point to a single T , or to an array of T .
We can compare pointers if they are pointing to the same array. Relational pointers can be used to compare two pointers. Pointers can't be multiplied or divided.
The main feature of a pointer is its two-part nature. The pointer itself holds an address. The pointer also points to a value of a specific type - the value at the address the point holds.
The fact itself is ok, but you'll run into undefined behavior if you call delete
on one of the pointers and attempt to use the other afterwards:
int* x = new int(5);
int* y = x;
delete x;
//y is a dangling pointer
If you run into a situation where you have to use multiple pointers to the same memory address, you should look into smart pointers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With