Live data from Hacker News

Const and Optimization in C

nullprogram.com

21–30 of 44 posts

Re: Const and Optimization in C

#21
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.

Re: Const and Optimization in C

#22
post #21

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.

Re: Const and Optimization in C

#23
post #21

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.

Re: Const and Optimization in C

#24
"void foo(const int * );"

"The function foo takes a const pointer".

I thought foo was taking a pointer a constant, where the pointer itself could change. This may seem pedantic, but perhaps the optimization he expected would have worked if foo took "const int * const ".

Re: Const and Optimization in C

#25
post #24

"void foo(const int * );" "The function foo takes a const pointer". I thought foo was taking a pointer a constant, where the pointer itself could change. This may seem pedantic, but perhaps the optimization he expected would have worked if foo took "const int * const ".

`const int * ` and `int const * ` are semantically identical, ie `const int const * ` is a duplicate declaration.

Re: Const and Optimization in C

#26
post #25
post #24

"void foo(const int * );" "The function foo takes a const pointer". I thought foo was taking a pointer a constant, where the pointer itself could change. This may seem pedantic, but perhaps the optimization he expected would have worked if foo took "const int * const ".

`const int * ` and `int const * ` are semantically identical, ie `const int const * ` is a duplicate declaration.

Opps, I'll update my post. Looks like peeyek had already pointed out that the pointer isn't const anyway.

Re: Const and Optimization in C

#27
post #21

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.

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.

Re: Const and Optimization in C

#28
post #23

Earlier quoted context omitted.

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.

Re: Const and Optimization in C

#29
post #9
post #8

Earlier quoted context omitted.

"casting away const-ness is undefined" No, it's not. That's the whole point why compiler can't optimize it away. You can often see ("char *") casts from static "strings" in legacy APIs and libraries calls, because original authors didn't know or didn't care how to use const correctly (or at all). I, personally, use const a lot throughout my C code, when you get it, it makes debugging so much easier.

you skipped the first clause of my sentence -- if a variable is declared const, casting away const-ness is undefined is that not true?

Your first part was irrelevant: if you cast away const, of course variable must have been const in first place. As other have already mentioned, casting itself is not an undefined behavior (and sometimes even have legitimate reasons, mentioned in my earlier post). On the other hand, trying to modify const variable though non-const pointer is undefined behavior.

P.S. if you downvoted to disagree with this (on my parent post), please provide an opposite example, because as a C programmer, I would really be interested to be proven wrong on this matter.

Re: Const and Optimization in C

#30
post #2

So if I understand the author, const function arguments can just have const-ness cast away inside the function, so it's really much closer to a type hint than anything else. However, if a variable is declared const, casting away const-ness is undefined. Is this about right? Yikes that's complex. I suppose the moral is casting away const-ness is a terrible idea.

Mutability of a variable and const qualification of the target type of the pointer used to store its address are independent, and language semantics really only care about the former.
Post reply on HN