const void * const_pointer = &const_pointer;
void * const const_value = &const_value;
I've never really understood why people put const before the type, to me the following is far more obvious: void const* const_pointer = &const_pointer;
void* const const_value = &const_value; cdecl> explain const void * const_pointer
declare const_pointer as pointer to const void
cdecl> explain void * const const_value
declare const_value as const pointer to void
cdecl>
The first must be the one that segfaults on write, IFF the compiler chooses to place it in the .text (as it should).Testing it on my machine with the following code seems to validate this hypothesis.
//file: test.c
#include <stdio.h>
const void * const_pointer = &const_pointer;
void * const const_value = &const_value;
int main()
{
printf("%p\n", const_pointer);
*(int*)const_pointer = 0;
printf("%p\n", const_pointer);
printf("---------------------------\n");
printf("%p\n", const_value);
*(int*)const_value = 0;
printf("%p\n", const_value);
return 0;
}
Result:
$ gcc test.c
test.c:4:30: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
void * const const_value = &const_value;
^
$ ./a.out
0x55b29ebfc010
0x55b200000000
---------------------------
0x55b29ebfbdb8
Command terminated
As to why the first one doesn't also result in a segfault, I don't know.In the second case the const_value variable itself is const-qualified and thus located in .rodata, but the pointer itself is not const-qualified so nothing prevents you from attempting to modify the data through that pointer. This is why you get a compiler warning about discarding the 'const' qualifier in the initialization. Since const_value is in .rodata, writing to it through the pointer causes a segfault.
As Sean1708 pointed out, it's more obvious what is going on if you place the 'const' qualifier immediately before the thing it's modifying, which is either the pointer operator or the variable name, never the type itself:
void const *const_pointer = &const_pointer;
void *const const_value = &const_value;
What would something like "const int" even mean on its own, anyway? There is no such thing as a mutable integer. It's the memory location holding the integer which may be either mutable or immutable.Edit: Actually I don't think the pointer is put anywhere, rather its value is stored in .data (non-read-only data) so it can be mutated without issue. Again though, my assembly isn't amazing.
unsigned int x;
unsigned int * x_addr = &x;
unsigned int x_addr_addr = &(&x);
(or arbitrarily many levels of "address-of") and you'd just get the same address.
It was before C had built-in booleans and the author had defined their own, but true was:
true_ptr is a pointer to itself. So however many times you deference it: You get the same pointer: I still think that it's neat that, even with ASLR, you have an address at compile time that you know won't collide with address space of malloc results, or the address space of your stack.Also you can declare the pointer as const and the value it points to as const and, if your kernel faults on writing to readonly memory pages, you get a buggier version of a NULL pointer that only segfaults on write.
Also it takes a second to figure out why the position of the const matters even though the pointer's value is the value of the pointer, and why only one of these segfaults on write: