Why do we need a different data type for a pointer?

  • Basically, a pointer is a variable used to store a memory address, which is always in hexadecimal (memory address), then why do we need another type of data to store the address.
  • EX: int * a; Can we use this to store the float address.
+7
c ++ c operating-system
source share
5 answers

Not all pointers (should be) the same size. If you had a large structure that should be aligned to 10 MB, the compiler could decide that 8 bits could be found to store all possible addresses (instead of the usual 32 or 64).

In addition, you do not need to need a different data type, you can use void* just fine, but why do you need it? In C ++, this is the smell of code.

There is also type-safety. You know that int* points to int , and that is in your best interest.

+10
source share

Two reasons:

  • On some architectures, pointers may have different formats depending on the size of the data they point to. For example, a pointer to char should be able to address individual bytes, but a pointer to int should be able to address groups of 4 bytes. Thus, the latter can use a format containing a byte address divided by 4.

  • This allows the compiler to generate the correct programs. If you try to dereference the char pointer and assign it to int , it needs to know that it should read only one byte from the source and expand it to the size of int . Without a pointer type declaration, it will read more bytes than necessary.

+6
source share

First, I do not know the machine where the pointers are stored as hexadecimal; every machine i know is binary representation inside. (Over the past 30 or 40 years, at least. IIRC, IBM 1401 has used decimal everywhere.)

As others have pointed out, not all pointers have the same size and presentation. I worked on machines where char* was larger than other types of data pointers, and of course, different sizes for function pointers and data pointers used for are very common.

The real answer, however, is based on a 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 should know whether to use integer arithmetic or floating point arithmetic, for example.

Regarding your second question: usually, yes, although you may have reinterpret_cast somewhere. However, the only thing you can legally do with your int* is to drop it back to float* ; dereferencing occurs undefined.

There are some exceptions for char* and unsigned char* . And also 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 really do similar things for some kinds of low level debugging or programming; things like extracting an exponential field from a float . However, such code is extremely rare, and formally includes undefined behavior, so you need to check what the compiler really does and possibly disables certain optimizations.

+3
source share

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

EX: int * a; Can we use this to store the float address.

Type C insecure function: yes, but not directly, especially in the latest compilers and standards (which are generally more secure and secure)

+2
source share

why do we need another type of data to store the address

This is really the right question, and the answer is hidden in it - why do we need a different type of data to store addresses. We (programmers) need this. The machine does not care - one number is exactly the same as another.

If you think of a “variable” as a placeholder for some data, there is a dichotomy in programming languages ​​between using the data itself and using the variable. Sometimes you only need data (for example, you need to print it), and sometimes you need an address where this data is stored (for example, so that you can change it). Most languages ​​have syntactic sugar, which confuses the two cases, and treats the variable identifier differently in different contexts.

One such case is as follows: consider the statement

 a = 1; 

In this case, the compiler looks at the address of the variable identified by "a" and writes "1" to that address. Identifier "a" may also be a pointer. Now let's look at another thing:

 if (a == 1) ... ; 

You do not compare the address of the variable identified by "a" with something, you compare what is stored at this address with "1".

Various "types of pointers" exist again for the convenience of programmers: basically, we make fewer errors that erroneously access data. Take a look at this example:

 double d; double* dp; int i; int* ip; 

By default, C is very relaxed, and you can usually do:

 dp = ip; 

or

 dp = &i; 

... but! sizeof (i) == 4 and sizeof (d) == 8, so if you search for a dp pointer, you will try to read 8 bytes from variable (i), which contains only 4. This means that you will read 4 unpredictable (random) bytes after the first four bytes of i, which is definitely what you don't want to do.

Again, all this is for our convenience. The car does not care. Both pointers look and behave exactly the same as in the CPU.

+1
source share

All Articles