The Ksplice Pointer Challenge

I am reading The Ksplice Pointer Challenge in the page below:
https://blogs.oracle.com/ksplice/entry/the_ksplice_pointer_challenge

Could anyone read the article and tell me what is the value of x in that article?
The writer says that x is stored at address 0x7fffdfbf7f00 and x + 1 has address 0x7fffdfbf7f04.

When we write x + 1 does it mean that value of x + 1 or address of x + 1?

In this case, x is an array name and its value is the address of the first element of the array.
Should the article say that the first element of the array is stored at 0x7fffdfbf7f00 not x is stored at address 0x7fffdfbf7f00?

1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main() {
  int x[5];
  printf("%p\n", x);
  printf("%p\n", x+1);
  printf("%p\n", &x);
  printf("%p\n", &x+1);
  return 0;
}


Assume that the first element of the array is stored at address 0x7fffdfbf7f00.

 
printf("%p\n", x);

In this case, x will be decayed to the pointer to the first element of the array.
Result: 0x7fffdfbf7f00

 
printf("%p\n", x+1);

In this case, x also will be decayed to the pointer to the first element of the array. And x + 1 = 0x7fffdfbf7f04.
Result: 0x7fffdfbf7f04

 
  printf("%p\n", &x);

&x is a pointer to entire array.
Result: 0x7fffdfbf7f00

 
 printf("%p\n", &x +1);

Result: 0x7fffdfbf7f14

I am confused what is x by itself?
&x is a pointer to entire array and its type is int (*)[5].
x is an array name but what is it?
x + 1 = array name + 1 doesn't make sense to me.
I think in that case the array name is decayed to a pointer and that is what I thought as above.

The article says that x is stored at address 0x7fffdfbf7f00 and this means the array name is stored at that address.
However, if that is the case x+ 1 doesn't make sense.
"x", given the rules of C and nothing else, translates into "&x[0]" here. x[0] is of type int. So think of it like "Address of x + sizeof(int)". Whenever we take the address of x, "&x", it is no longer a pointer to a single element to an array but the array itself. We can then think of "&x + 1" as "Address of x + sizeof(x)".

The lesson here is to show the difference in how C arrays work with pointer arithmetic, to not assume that "&x" is the same "&x[0]", and to show how C types degrade when you use them with pointer arithmetic.

If you want more clarification as to how the typing system works more specifically, re read the section in that article where they use cdecl. It's actually quite a good example.
Last edited on
Thanks!
"x", given the rules of C and nothing else, translates into "x[0]" here.

I think you meant &x[0] here not x[0].
I am confused about the sentence in that article:
To make this concrete, let's assume that x is stored at address 0x7fffdfbf7f00 (this is a 64-bit system).


Shouldn't it be "that the first element of x is stored at address 0x7fffdfbf7f00"?
Last edited on
The reason &x + 1 is 0x7FFFDFBF7F14 is because that is 20 bytes, i.e. 5 * sizeof(int), after 0x7FFFDFBF7F14. The compiler converts &x + 1 to &x + 1*sizeof(x) which in this case is 5*4=20=0x14.
Last edited on
No because x can be considered a single variable rather than an array of variables. That's like saying you have a struct and saying the address of that instance of the struct is actually the address of the first member of the instance of that struct. While they're conceptually different, they are the same in practice here. That should not be what's confusing you.

And you are correct, I meant &x[0]. Changed accordingly.
Last edited on
The reason &x + 1 is 0x7FFFDFBF7F14 is because that is 20 bytes, i.e. 5 * sizeof(int), after 0x7FFFDFBF7F14. The compiler converts &x + 1 to &x + 1*sizeof(x) which in this case is 5*4=20=0x14.

Yes, that is what I got too.

No because x can be considered a single variable rather than an array of variables. That's like saying you have a struct and saying the address of that instance of the struct is actually the address of the first member of the instance of that struct. While they're conceptually different, they are the same in practice here. That should not be what's confusing you.

I understand this but I think you misunderstood my question.
From the article:


To make this concrete, let's assume that x is stored at address 0x7fffdfbf7f00 (this is a 64-bit system).

Here x is a variable and it is stored at address 0x7fffdfbf7f00. But what is it value? (the content at that location?)
printf("%p\n", x);
In this command, x is considered as a pointer and the line will print the value of x to screen.
Because x is not a pointer but an array name, first it will be converted to a pointer to the first element of that array and then the value of that pointer (address of the first element of the array) will be printed out.
However, the article doesn't say the value of x (address of the first element of the array).
And it seems to take the address of x as its value.
Actually, you're right according to C89 specification:
an expression that has type ‘‘array of type’’ is
converted to an expression with type ‘‘pointer to type’’ that points to the initial element of
the array object and is not an lvalue.

The article then is referring to where the address of the array of x rather than the pointer conversion of it.
Last edited on
Here is what I thought:
x has type array of int.
1
2
printf("%p\n", x);
printf("%p\n", x+1);

In these commands, x will be converted to type pointer to int that points to the first element of the array.
So, what are printed out here are values of x and x+1.
However, the article doesn't mention the value of x, so for me, it is confusing.

1
2
3
 
printf("%p\n", &x);
printf("%p\n", &x+1);

In these two commands, because of operator & the type of x is not converted.
x is of type int [5].
So, &x is of type int (*) [5].
The value of this pointer also equal to the value of the pointer to the first element of the array.
&x+1 = the value of &x + sizeof (int (*) [5])
Last edited on
Topic archived. No new replies allowed.