Talk:Const (computer programming)

message: The article used to say the following, in the section "restrict in C99":
:The C99 language special cases a restrict qualified pointer to a const qualified type in order to notify a compiler that it can assume "strong const" semantics.
Does anyone know what the writer had in mind? After all, in the example given (which is still in the current revision, but won't be for much longer, if I remember to come back and take it out), it seems to me that the compiler would be able to change
 void foo(int *);
 
 int bar(int *a)
 {
     const int *b = a;
     foo(a);
     return *b;  /* const, non-volatile value cannot have been modified by the intervening code */
 }
into
 void foo(int *);
 
 int bar(int *a)
 {
     int ret = *a;
     foo(a);
     return ret;
 }
without any restrict-based contortions on the programmer's part. A Google search for "strong const" doesn't turn up anything enlightening. --Quuxplusone 04:22, 22 August 2007 (UTC)
replies: text: No, a conforming C compiler would not be able to do the optimization you describe, because "const" does not imply that the object is immutable to everyone, merely that it is read-only when accessed via that particular pointer. So, in your example, the compiler has to assume that a call to "foo" could have changed the value pointed to by "b". On the other hand, with "const restrict", it knows that within the scope of that pointer, noone else is accessing the value through any other means, and thus the optimization becomes possible. Thus, "weak const" == "I cannot change this", "strong const" == "noone can change this". -- int19h (talk) 10:15, 5 April 2008 (UTC)
thread_title:  restrict in C99 

message: The article states
"Similarly, C++ does not have any feature equivalent to Java's final modifier for methods, although its effect on classes can be simulated by a clever abuse of the C++ friend keyword.[3]"
It needs clarifying to the non-guru C++ users, that the default behavior for C++ member functions is to be hidden, but not overridden. There is no method hiding in Java. Someone might think that you can't prevent overriding in C++, which is not true. The fact is how ever that you cannot prevent hiding in C++.  —Preceding unsigned comment added by 84.36.31.228 (talk) 13:20, 31 October 2007 (UTC)
thread_title:  final methods in C++ 

message: Removed from the article:
:The programmer may also create volatile pointers, though their applications are rarer:
:
 // Set up a pointer to a read-only memory-mapped register that 
 // contains a memory address for us to dereference
 const int * volatile const tableLookup = reinterpret_cast<int*>(0x8004);
 
 int currentTableValue = *tableLookup; // Dereference the memory location
 int newTableValue = *tableLookup; // Dereference it again
:Since the address held in the tableLookup pointer can change implicitly, each dereference might go to a different location in the lookup table, so currentTableValue is not necessarily equal to newTableValue.
The code doesn't match the comments, and the accompanying English text glosses over the key point of confusion. The semantics of the code are: Take the value 0x8004 and convert it (at compile time) to a pointer; at runtime, store this pointer value in memory (at a location named "tableLookup"; dereference it twice, but allowing for the possibility that someone else could be overwriting "tableLookup" in between accesses. (C and C++ are fuzzy on what exactly "volatile" means, but "someone else overwriting it" (plus "someone else reading it", which is irrelevant here) is a good enough approximation of how modern compilers treat it.)
However, since the symbol "tableLookup" has internal linkage, there is no plausible way for anybody else to be modifying the value at that address (which was determined randomly by the compiler, if it exists at all; remember, &tableLookup is not (int*)0x8004; that's the value stored in tableLookup). Since tableLookup was declared const, the C++ compiler is not even required to allocate storage for an object called tableLookup at all! You'll see this happen if you remove the volatile qualifier. I don't think there's any wording in the C++ standard that forces the compiler to allocate space for tableLookup just because it's volatile. 
The critical points about the semantics of the above code are: The value stored at 0x8004 is const and can be assumed not to change. The tableLookup object itself (if it exists) is const volatile. The compiler (theoretically) cannot assume that the assignment to currentTableValue reads from address 0x8004. But the comments and English commentary don't seem to match these points, and the code itself is kind of pointless, so I suspect that the original writer was laboring under misconceptions about the semantics of what he wrote. --Quuxplusone (talk) 07:22, 13 May 2008 (UTC)
replies: text: I think you may be mistaken. Regardless of linkage, a volatile variable (pointer or not) could change due to any number of circumstances outside the compiler's control or visibility (e.g., an on-chip peripheral's memory location, a memory hardware/firmware register). However, on re-reading, I think it's fine to omit this bit, which does make the coverage more complete but which is such a rare avis that it may serve to confuse rather than elucidate and for which a reliable source is not easily found AFAICT. --Flex (talk/contribs) 02:43, 14 May 2008 (UTC)
thread_title:  Volatile pointer example 

message: The reference numbering in the article is wrong, but I don't want to edit the article as I lack the expertise.
Reference pointer [3] at the end of the section "final and JAVA" refers to number "4." in the reference list.
This is caused by the fact that the ID of reference number 3 ("endnote_cleverabuse") is not used in the article.
92.233.157.159 (talk) 20:25, 23 May 2008 (UTC)
replies: text: Fixed. Thanks! --Flex (talk/contribs) 01:06, 24 May 2008 (UTC)
thread_title:  Reference numbering is incorrect 

message: I find it odd that const_cast<>() is described as a "loophole", and given
the air of a weakness introduced by C++ ... while in reality it's supposed
to close the dangerous "loophole" which is plain old C-style casts –
which in turn is not mentioned at all!
The term "loopholes" should probably not be used at all – it makes it sound as if a programmer is never supposed to be able to break a const. As far as I can tell, that
was never Stroustrup's goal with const.
JöG (talk) 21:02, 9 February 2010 (UTC)
thread_title:  Loopholes 

message: "While const methods can be called by const and non-const objects alike, non-const methods can only be invoked by non-const objects."
Shouldn't "by" be replaced by "on"?
Rubix (talk) 12:38, 18 November 2011 (UTC)
thread_title:  Misc 

message: «Following usual C convention for declarations, declaration follows use, and the * in a pointer is written on the pointer, indicating dereferencing (...) Thus const modifies the name to its right.»
I think this assertion must be more explicit: In which sense does it modify the name? What exactly is it meant with 'name'?
Moreover, immediately after, it appears incomprehensible the jump to C++, since this language is considered and explained in the next section.
Hence, it is not clear if the subsequent examples are referred to C or C++. But, above all, it is undecipherable how you can see in this example:
«int const * ptrToConst can thus be read as "*ptrToConst is a int const" ...»
where the above mentioned modification to the name lies (i.e., if compared with the examples of the successive section "C++ convention", the difference 'name' vs 'type' modification is not evident).  — Preceding unsigned comment added by Filipsnew (talk • contribs) 22:37, 13 September 2016 (UTC)
thread_title:  C convention 

message: Hello fellow Wikipedians,
I have just modified  on Const (computer programming). Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
*Added archive https://web.archive.org/web/20140525030319/http://cm.bell-labs.com/who/dmr/chist.html to http://cm.bell-labs.com/who/dmr/chist.html
*Added  tag to http://archive.adaic.com/standards/83lrm/html/Welcome.htmlANSI/MIL-STD
*Added archive https://web.archive.org/web/20141020162257/http://archive.adaic.com:80/standards/83lrm/html/lrm-03-02.html to http://archive.adaic.com/standards/83lrm/html/lrm-03-02.html#3.2.1
When you have finished reviewing my changes, please set the checked parameter below to true or failed to let others know (documentation at ).
Cheers.—InternetArchiveBot (Report bug) 23:57, 29 November 2016 (UTC)
thread_title:  External links modified 

message: Hello fellow Wikipedians,
I have just modified one external link on Const (computer programming). Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
*Added archive https://web.archive.org/web/20080105024742/http://www.digitalmars.com/d/const.html to http://www.digitalmars.com/d/const.html
When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.
Cheers.—InternetArchiveBot (Report bug) 10:39, 12 August 2017 (UTC)
thread_title:  External links modified