Changes between Version 2 and Version 3 of const


Ignore:
Timestamp:
Oct 19, 2009, 11:18:21 PM (12 years ago)
Author:
andersk@mit.edu
Comment:

Mention gcc -Wcast-qual.

Legend:

Unmodified
Added
Removed
Modified
  • const

    v2 v3  
    3131This implies that a `foo` cannot be freed through a `const foo *`.  Therefore, the code that “owns” (i.e., is responsible for allocating and freeing) the `foo` must hold on to the (non-`const`) `foo *`.  When possible, it should pass only a `const foo *` to any other code that does not need to modify or free the `foo`.
    3232
    33 Do not try to subvert this rule by adding casts!  Code that needs to free a `foo` should not have been given a `const foo *`.  A cast only hides this potential bug.
     33Do not try to subvert this rule by adding casts!  Code that needs to free a `foo` should not have been given a `const foo *`.  A cast only hides this potential bug.  `gcc -Wcast-qual` will warn you about casts that throw away `const` qualifiers.
    3434
    3535These rules can help you locally find memory leaks and double-free bugs.  For example, if a `foo` is malloc’d and only stored into a `const foo *`, you know that it has been leaked.  If you ignore a compiler warning that `const foo *` is converted into `foo *`, you may be freeing a `foo` that was not supposed to be freed.