|
|
Log in / Subscribe / Register

Buried in warnings

Buried in warnings

Posted Nov 2, 2006 10:39 UTC (Thu) by Asebe8zu (subscriber, #24600)
In reply to: Buried in warnings by pcdavid
Parent article: Buried in warnings

Another very useful feature in Java without equivalent in C
is the final modifier for local variables.
When used, the compiler ensures that the variable is set
once and only once before being used.

I don't know how you would implement this in C though, without
introducing some new keyword.


to post comments

Java 'final', C 'const'

Posted Nov 2, 2006 15:20 UTC (Thu) by jreiser (subscriber, #11027) [Link] (2 responses)

If the final value is (or could be) assigned at declaration, then the C keyword const suffices.

My own favorite candidate for compiler enhancement is a message, "warning: 'const' omitted". The compiler detected that there would be no complaints if the programmer supplied the keyword const.

Java 'final', C 'const'

Posted Nov 2, 2006 21:16 UTC (Thu) by jzbiciak (guest, #5246) [Link] (1 responses)

The problem I run into time and time again with const in C is that there are too many functions in too many libraries which take a non-const pointer, even though they do not modify the pointed-to entity. Very annoying.

Pointer to pointer to const

Posted Nov 3, 2006 1:40 UTC (Fri) by xoddam (subscriber, #2322) [Link]

That would be a fine idea, if you didn't run into "C++ Gotcha #32" all the time when trying to do this (it's just as much a gotcha in C as it is in C++). As long as you don't try to take the address of your const pointer, you're safe. But you can't mix const and non-const indirect pointers, because of aliasing problems.

Of course said aliasing problems might all be hidden deep inside the library (and/or the imagination of the compiler) and users shouldn't have to care at all -- but if it causes a problem for the developer, they're far more likely to drop 'const' than to suppress compiler warnings with casts and hope everything is otherwise correct.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds