Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The following is a foo function that take a constant pointer:

    void foo(int *const x);
If you point x to another adress inside foo function, it will not compiled.

The author seems think that

    void foo(cont int *x);

is function that takes a constant pointer which is wrong, it is a function that takes pointer to constant object. In this case, it is legal if you point x to another address in memory inside foo function.


Where do you see this confusion? I don't think it's there.

The author points out that it is not necessarily undefined behavior to take your second prototype, cast the const away and modify the pointed-to object if you ensure that it is only called with non-constant objects.


In 2nd paragraph, the author states:

    void foo(const int *);
    ...
> The function foo takes a const pointer, which is a promise from the author of foo that it won’t modify the value of x. Given this information, it would seem the compiler may assume x is always zero, and therefore y is always zero.


It is clear that the author means "pointer to const" there.


The author does not think that, all he does is point out correctly that const-qualifying a pointer's target type cannot be used for optimization purposes as it does not make any actual guarantees about object mutability.

Note that in contrast, restrict-qualifying a ponter-to-const (ie `const int *restrict x`) does make such guarantees, but only callee-side.


Update: The author have been corrected the 2nd paragraph. My comment above is not valid anymore




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: