-3

can the Pointer have value?? so In which case is it used

int num=100;
int* iptr=NULL;
iptr=reinterpret_cast<int*>(num);
printf("%d \n",num);
printf("%d \n",num);

result 100 100

7
  • iptr=reinterpret_cast<int*>(num); makes no C sense; C tag deleted. Commented Oct 8, 2018 at 11:25
  • 4
    Please clarify the question. What do you mean by pointer having a value? And your code prints the same variable twice so the result obviously is 100 100. The pointer isn’t used in any way here. Commented Oct 8, 2018 at 11:26
  • completely unclear what is the question. What your code basically does is: It assigns 100 to num and then prints the value twice. No pointers needed to understand whats going on, but it bet you worry about those two lines inbetween (that currently have zero effect on the output of your code) Commented Oct 8, 2018 at 11:26
  • 1
    The value of a pointer is the address where it points. Commented Oct 8, 2018 at 11:26
  • No sure I understand the question. Are you asking when does an integer represent a valid address? Commented Oct 8, 2018 at 11:27

1 Answer 1

1
  1. Mappings between pointers and integers are implementation-defined.
  2. Conversion of an integer to a pointer using reinterpret_cast will not be a safely-derived pointer value except under certain conditions. Those conditions are not met in your example.

Citation from CPP draft (N4713):

8.5.1.10 Reinterpret cast
...
6. A value of integral type or enumeration type can be explicitly converted to a pointer. A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation-defined. [ Note: Except as described in 6.6.4.4.3, the result of such a conversion will not be a safely-derived pointer value. —end note ]

The conditions for Safely-derived pointers.

6.6.4.4.3 Safely-derived pointers
...
2 A pointer value is a safely-derived pointer to a dynamic object only if it has an object pointer type and it is one of the following:
(2.1) — the value returned by a call to the C++ standard library implementation of ::operator new(std::size_t) or ::operator new(std::size_t, std::align_val_t);
(2.2) — the result of taking the address of an object (or one of its subobjects) designated by an lvalue resulting from indirection through a safely-derived pointer value;
(2.3) — the result of well-defined pointer arithmetic using a safely-derived pointer value;
(2.4) — the result of a well-defined pointer conversion of a safely-derived pointer value;
(2.5) — the result of a reinterpret_cast of a safely-derived pointer value;
(2.6) — the result of a reinterpret_cast of an integer representation of a safely-derived pointer value;
(2.7) — the value of an object whose value was copied from a traceable pointer object, where at the time of the copy the source object contained a copy of a safely-derived pointer value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.