if we dynamically allocate 8 bytes, we can represent 8 byte numbers yeah?
but even if I dynamically allocate, the pointer seems to point to a 4 byte location. it acts just like an integer.
largest number i can represent even after a dynamic allocation of 8 bytes is 2147483647. that is also the max number represented by int variable
In the view of C and C++, an variable could be treated as a typed memory region.
If you allocate 8 bytes memory, of course you could store a 64-bit number in it. However, do not forget the type. You will then also have to treat this region as a, for example, int64_t variable.
On the other hand, pointer is an abstraction of memory space. If you get a valid pointer, you get 1) the address to that memory region, and you get 2) the type of it. Now, in your case, although you have a 8-byte memory region, you hold a pointer of type, for example, int32_t*. Hence, only 4 bytes will be used to store the integer, and the other 4 bytes remain unused. To solve this problem, reinterpret_cast the pointer to int64_t*.
If only one byte is allocated for your integer, and a pointer typed in int32_t is pointing to the memory region, then writing behaviour through this pointer will infect the following 3-byte memory, thus undefined behaviour happens.