Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Addressing in Xcode for C program

Tags:

c

pointers

xcode

I get this output when I compile the code below in a 64 bit Intel in Xcode.

#include<stdio.h>
#include<limits.h>

int main(void)
{
  /* declare some integer variables */
  long a = LONG_MAX;
  long b = 2L;
  long c = 3L;

  /* declare some floating-point variables */
  double d = 4.0;
  double e = 5.0;
  double f = 6.0;

  printf("A variable of type long occupies %d bytes.", sizeof(long));
  printf("\nHere are the addresses of some variables of type long:");
  printf("\nThe address of a is: %p  The address of b is: %p", &a, &b);
  printf("\nThe address of c is: %p", &c);
  printf("\nThe address of a-b is: %ld\nvalue of a is %ld\nValue of b is %ld\nsize of pointer %ld ", (&a-&c),a,b,sizeof(&a));
  printf("\n\nA variable of type double occupies %d bytes.", sizeof(double));
  printf("\nHere are the addresses of some variables of type double:");
  printf("\nThe address of d is: %p  The address of e is: %p", &d, &e);
  printf("\nThe address of f is: %p\n", &f);

    printf("\n size long - %d", sizeof(a));
  return 0;
}
A variable of type long occupies 8 bytes.

Here are the addresses of some variables of type long:

The address of a is: 0x7fff5fbff880 
The address of b is: 0x7fff5fbff878 
The address of c is: 0x7fff5fbff870 
The address of a-b is: 2

value of a is 9223372036854775807 
Value of b is 2 
size of pointer 8 

A variable of type double occupies 8 bytes.

Here are the addresses of some variables of type double:

The address of d is: 0x7fff5fbff868 
The address of e is: 0x7fff5fbff860 
The address of f is: 0x7fff5fbff858 
size long - 8

What is strange to me is that the difference between the address for a and b is only 2. I would expect it to have been 8, which would match the number of bytes for a long. Does anyone know a reason why this would be?


I did have a typo in the code where I subtracted &a-&c, but that really does not pertain to my question. My question is why is there only a difference of 2 bytes from variable a's address to variable b's address, when the long is 8 bytes long and I would expect to see a difference of 8?

like image 325
ControlAltDelete Avatar asked Dec 07 '22 06:12

ControlAltDelete


1 Answers

Pointer arithmetic is based on the size of the type it points to not in bytes, this reference on Pointer Arithmetic covers the topic quite well, you also have a typo:

(&a-&c)

you are actually subtracting c from a.

This is also undefined behavior since pointer subtraction is only defined if the pointers point to the same array, see section 6.5.6/9 from the C11 draft standard:

[...] When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object;[...]

Also section 6.5.6/8 is also relevant:

[...] If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. [...]

like image 196
Shafik Yaghmour Avatar answered Dec 26 '22 22:12

Shafik Yaghmour