Actual meaning of "value" and "address" in a relationship with pointers and references

When I passed this lesson I was rather confused by the code that was presented in it. I think that “value” of something means the actual data that stored in memory and presented by this variable and address of something is the actual address of this data in memory. In this sense, illustration in this lesson is wrong because if you want to see a value of a pointer, you should just print an address that this pointer stores (if the pointer is “valPtr”, you need to print “valPrt” without any operators) and if you want an address of a pointer, then you need to get it’s address using & operator ("&valPtr").
There will be some problems with references at that point. In fact, you have no ability to get neither value nor address of a reference. Actually, when you are using reference, there is a place where that reference stored in the memory, but you have no ability to uncover it’s real address using the tools of C++. Nevertheless, i think that we cannot say that the “value of reference is 5”. I think that just a fact that we will never get it’s value does not allow us to do this, at least, because references does not compleatly safe. To understand, what am I talking about, you can look at this code:

// Creating a new integer pointer and allocating a variable for this pointer.
int* xPtr = new int;
// Setting the value of the variable, allocated to this pointer.
(*xPtr) = 10;
// Creating a reference to the variable, allocated to our pointer.
int& xRef = (*xPtr);
// Printing thee same variable through the pointer to this variable and through the reference to it.
// Output should be: "xPtr: 10 xRef: 10"
std::cout << "xPtr: " << *xPtr << " xRef: " << xRef << std::endl;
// Freeing of a variable, allocated for the pointer.
delete xPtr;
// Now we still have a reference to the variable, but this variable no longer exist.
// We still can try to print it, and we even can success, but the actual result of this operation is unknowable.
std::cout << "xRef: " << xRef << std::endl;

So, i think that illustration code in lesson should not be like this:

std::cout << “Values” << std::endl;
std::cout << "val: " << val << " val2: " << val2 << " valPtr: " << *valPtr << " valRef: " << valRef << std::endl;
std::cout << “Address” << std::endl;
std::cout << "val: " << &val << " val2: " << &val2 << " valPtr: " << valPtr << " valRef: " << &valRef << std::endl;

but like this:

std::cout << “Values” << std::endl;
std::cout << "val: " << val << " val2: " << val2 << " valPtr: " << valPtr << " valRef: " << “Сan not be obtained” << std::endl;
std::cout << “Address” << std::endl;
std::cout << "val: " << &val << " val2: " << &val2 << " valPtr: " << &valPtr << " valRef: " << “Сan not be obtained” << std::endl;

This all is just my point of view and lecturer can have another, but I would really like to know what arguments can be given in favor of the option presented in the lecture.

Privacy & Terms