1

I find that the value of '*ptr' in the steps:3 does not equal 3

#include <bits/stdc++.h>
using namespace std;
int main(int arg, char* args[])
{

    int* ptr;
    int x = 4;
    float y = 3.142;
    cout << y << "    " << &y << endl; //step:1
    ptr = &x;
    cout << ptr << "  " << *ptr << endl; //step:2
    ptr = (int*)(&y);
    cout << ptr << "  " << *ptr; //step:3 ->problem here
    return 0;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Mohamed
  • 43
  • 1
  • 5
  • 5
    You have undefined behavior. `ptr = (int*)(&y);` and then `*ptr` makes it read an `int` from the memory occupied by a `float`. They have totally incompatible representations in memory. – Ted Lyngmo Mar 29 '21 at 20:59
  • 4
    Casting a value between a `float` to an `int` works fine. Casting the memory backing a float (a `float*`)to an `int*` is undefined behaviour. – Mike Vine Mar 29 '21 at 21:02
  • 2
    `ptr = (int*)(&y)` That's lying to the compiler that address `&y` holds an `int` value. But in reality what's stored at that address is a `float` value. Once you dereference `*ptr` the behavior is undefined. Whether you get a `0`, some other garbage value, or a crash is an artifact of the implementation. – dxiv Mar 29 '21 at 21:03
  • how can i defined behavior of `ptr=(int*)(&y)` – Mohamed Mar 29 '21 at 21:11
  • 1
    You cant. Why would you want to? – Mike Vine Mar 29 '21 at 21:13
  • (If you want to look at the bits which represent a float for some reason, you can `memcpy` from a `float` to an `int`. The phrase you are looking for is _type punning_.) – Mike Vine Mar 29 '21 at 21:14
  • I try to do that,then I get error so I wanted to know the reason for error – Mohamed Mar 29 '21 at 21:15
  • Show your code which uses _memcpy_ then. – Mike Vine Mar 29 '21 at 21:16
  • Copy it to a `char[sizeof(float)];` and look at the bytes there. That you can do without UB. – Ted Lyngmo Mar 29 '21 at 21:16
  • 1
    In C++20 you will be able to use [std::bit_cast](https://en.cppreference.com/w/cpp/numeric/bit_cast) – Blastfurnace Mar 29 '21 at 21:17
  • 1
    ok, thanks for your efforts to explain that to me – Mohamed Mar 29 '21 at 21:17
  • 1
    Unrelated: Be cautious of the first two lines. They can make very simple programs fail spectacularly for reasons that may, at first, seem inscrutable. Not your problem here, but when you do run into it, it'll hurt. See [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) for more details. – user4581301 Mar 29 '21 at 21:23

1 Answers1

3
ptr = (int*)(&y);
cout << ptr << "  " << *ptr; //step:3 ->problem here

The second line invokes undefined behaviour. *ptr dereferences the memory pointed by ptr as if it pointed to an int. Since it doesn't (it points to a float), this is undefined behaviour.

Anything could happen from that point on.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42