-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

Mukesh Verma
  • 524
  • 5
  • 9
Tree
  • 7
  • 4
  • `iptr=reinterpret_cast(num);` makes no C sense; C tag deleted. – pmg Oct 08 '18 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. – Sami Kuhmonen Oct 08 '18 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) – 463035818_is_not_an_ai Oct 08 '18 at 11:26
  • 1
    The value of a pointer is the address where it points. – Some programmer dude Oct 08 '18 at 11:26
  • No sure I understand the question. Are you asking when does an integer represent a valid address? – StoryTeller - Unslander Monica Oct 08 '18 at 11:27
  • On a slightly unrelated note, `NULL` and `printf` are very C-specific things, and should not really be used in C++. For a null pointer use [`nullptr`](https://en.cppreference.com/w/cpp/language/nullptr) or the simple integer literal `0`. For output use the type-safe and standard C++ `std::cout` and the standard overloaded formatted stream output operator `<<`. – Some programmer dude Oct 08 '18 at 11:29
  • `iptr` is not used in the example code, so I am not sure what you are doing. However yes, it is possible to store a pointer value in a suitably sized integer. This is seen in some API's, usually C ones when accepting some user-defined value, such as for a callback, as an alternative to `void*`. – Fire Lancer Oct 08 '18 at 12:01

1 Answers1

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.

P.W
  • 26,289
  • 6
  • 39
  • 76