Difference between two pointer variables

I asked this question in a written test. while executing below code on my lapi, I get 10 as output

#include<stdio.h> int main() { int *i, *j;/* two pointer variable*/ i = (int *)60; j = (int *)20; printf("%d \n",ij); return 0; } 

Output:

 10 

Can someone tell me why conclusion 10 .

+6
source share
6 answers

According to the C standard (6.5.6 additive operators)

9 When two pointers are subtracted, both indicate the elements of the same array object or one after the last element of the array object; the result is the difference in the indices of the two elements of the array.

Thus, your program has undefined behavior, because pointers do not point to elements of the same array.

It seems that the compiler simply generates object code to subtract two pointers regardless of whether the pointers point to elements of the same array (it trusts you).

In this case, the difference between the two pointers in accordance with the arithmetic of the pointer is the number of elements that can be placed in memory between the two pointers.

In your case, sizeof( int ) is 4 . Thus, a 40-byte memory can accommodate 10 elements of type int, provided that sizeof( int ) is 4 .

This value, which is 10, is printed by the printf function.

+7
source

You evaluate the difference or "distance" between two pointers to int . sizeof(int) - 4 on your platform. The difference between 60 and 20 is 40, which is the distance between 10 goals. Your implementation seems to just appreciate this difference.

However, the C standard places a limit on evaluating the difference between two pointers: both pointers must point to elements in an array or one after the end. If you can guarantee that both i and j satisfy this, then the difference estimate is valid. Since your code does not necessarily satisfy this condition, it may have undefined behavior, in which case the result / result could be any.

Also note that there is undefined behavior for canceling the i and j links if they do not point to valid addresses containing int values.

+6
source

Taking the difference of two pointers is determined by the C standard only if both pointers point to the same object (array), so the OP code causes undefined behavior. The result can be anything.

From the C11 standard:

6.5.6 / 9 Additive operators

When two pointers are subtracted, both point to the elements of the same array object, or one after the last element of the array object; as a result, there is a difference in the indices of the two elements of the array. The size of the result is determined by the implementation, and its type (signed integer type) is ptrdiff_t defined in the <stddef.h> header. If the result is not represented in an object of this type, the behavior is undefined.

The following code is valid:

 #include <stdio.h> int main() { int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int * i = a + 0; int * j = a + 10; /* Points "one past the last element" of the array. */ printf("%td \n", i - j); return 0; } 

He also prints 10 .

+3
source

Taking the difference between pointers that don't point inside the same object is Undefined Behavior.

Your pointers do not point to any object at all.

This is Undefined Behavior for subtracting pointers in your code.

Any result (10, 40, 65535, -1) is erroneous.

+2
source

What you see is undefined behavior.

The pointer must point to some valid memory location, but 60 and 20 should not.

Just to quote from the standard

Both pointers must point to elements of the same array object or one after the last element of the array object, since this is not what you see in your code, it is UB.

+1
source

Although this is strictly undefined behavior and anything is permitted, compilers are rarely arbitrary when processing such code, and the result can be explained in this case as:

  (i - j) == ((60 - 20) / sizeof(int)) 

But keep in mind that in some cases this may not apply; it may, for example, depend on the architecture of the target memory or the relative alignment of objects.

+1
source

Source: https://habr.com/ru/post/980820/


All Articles