Width of POINTER

9 views
Skip to first unread message

Chris Tillman

unread,
Jun 24, 2017, 5:14:23 PM6/24/17
to eiffel...@googlegroups.com
There's something I don't understand.

When I check in a C program on my x86_64 GNU/Linux machine

  const int a[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  int i, sum;

  printf ("%lu, %lu", sizeof (a) , sizeof (*a));

I get

  32, 4

In EiffelStudio, when I check {PLATFORM}.Integer_32_bytes I get 4, so times 8 elements I still get 32. But when I check {PLATFORM}.Pointer_bytes, I get 8 rather than 4.

{PLATFORM}.Pointer_bytes comments say "-- Number of bytes in a value of type `POINTER`" ... does this mean the C pointer is a different size than the Eiffel POINTER on this machine?

--
Chris Tillman
Developer

Alexander Kogtenkov

unread,
Jun 25, 2017, 5:45:00 AM6/25/17
to eiffel...@googlegroups.com
32 printed by the C program means 32 bytes. Given that the array contains 8 elements and each element is 4 bytes, 32 = 8 * 4.

In C, for (complete) arrays, sizeof gives the number of bytes occupied by the whole array. If memory occupied by a pointer is of interest instead, either &a should be used, or a pointer type rather than an array type.

In particular,

    sizeof &a

gives 8 (for variables there is no need to use parentheses, i.e. to write "sizeof (&a)", they are required when the argument is a type, not a variable).

If we had

    extern const int x [];
    const int * y;

the results would be

   sizeof x -- 0 and the compiler raises a warning for me, because the array size is unknown to the compiler (the array type in incomplete)
   sizeof y -- 8

To summarize, the pointer size obtained by "{PLATFORM}.pointer_bytes" or by "sizeof &a" is the same.

Alexander Kogtenkov


Chris Tillman <toff.t...@gmail.com>:

Chris Tillman

unread,
Jun 25, 2017, 2:44:23 PM6/25/17
to eiffel...@googlegroups.com
Thanks very much. I think it's beginning to soak in.

--
You received this message because you are subscribed to the Google Groups "Eiffel Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to eiffel-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/eiffel-users.
For more options, visit https://groups.google.com/d/optout.



--
Chris Tillman
Developer
Reply all
Reply to author
Forward
0 new messages