Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need different datatype for pointer?

  1. Basically pointer is a variable used to store memory address ,which is always
    hexadecimal(memory address) then why do we need different datatype for storing address.
  2. EX:int *a; Can we use this a for storing float address.
like image 399
user2866139 Avatar asked Oct 10 '13 09:10

user2866139


4 Answers

Not all pointers are (required to be of) the same size. If you had a large structure that needs to be aligned to 10 MB, the compiler could decide that it only needs 8 bits (instead of the usual 32 or 64) to store all possible addresses your variable could be located at.

You also don't need a different datatype, you can use void* just fine, but why would you? In C++, this is a code-smell.

There's also type-safety. You know an int* points to an int, and this is to your advantage.

like image 113
Luchian Grigore Avatar answered Nov 13 '22 19:11

Luchian Grigore


Two reasons:

  1. On some architectures, pointers may have different formats depending on the size of the data they're pointing to. For instance, a pointer to char must be able to address individual bytes, but a pointer to int only needs to be able to address groups of 4 bytes. So the latter could use a format that contains the byte address divided by 4.

  2. It allows the compiler to cgenerate correct programs. If you try to dereference a char pointer and assign it to a int, it needs to know that it should only read one byte from the source, and widen it to the size of an int. Without the pointer type declaration, it would read more bytes than appropriate.

like image 35
Barmar Avatar answered Nov 13 '22 19:11

Barmar


For starters, I don't know of a machine where pointers are stored as hexadecimal; every machine I'm familiar with used a binary representation internally. (For the last 30 or 40 years, at least. IIRC, the IBM 1401 used decimal everywhere.)

As others have pointed out, not all pointers do have the same size and representation. I've worked on machines where char* was bigger than other data pointer types, and of course, having different sizes for function pointers and data pointers used to be very common.

The real answer, however, it based on the type system in C and C++. If p is a pointer, what is the type of *p? If I write something like *p + *q, the compiler has to know whether to use integer arithmetic or floating point arithmetic, for example.

As for your second question: typically, yes, although you'll probably need a reinterpret_cast in there somewhere. However, the only thing you can legally do with your int* is cast it back to a float*; dereferencing it is undefined behavior.

There are some exceptions for char* and unsigned char*. And in practice, if you know what you are doing, you can get away with things like:

float f;
int* a = reinterpret_cast<int*>( &f );
std::cout << *a << std::endl;

I actually do similar things for some sorts of low level debugging or programming; things like extracting the exponent field from a float. Such code is extremely rare, however, and formally involves undefined behavior, so you have to verify what the compiler actually does, and possibly turn off certain optimizations.

like image 3
James Kanze Avatar answered Nov 13 '22 18:11

James Kanze


Because it gives information on how to interpret data pointed by the pointer.

EX:int *a; Can we use this a for storing float address.

Through C type unsafety feature: yes, but not directly, especially in recent compilers and standard (which tend to be safer and safer)

like image 2
LeleDumbo Avatar answered Nov 13 '22 19:11

LeleDumbo