Changes between Version 2 and Version 3 of const
- Timestamp:
- Oct 19, 2009, 11:18:21 PM (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
const
v2 v3 31 31 This 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`. 32 32 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. 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. `gcc -Wcast-qual` will warn you about casts that throw away `const` qualifiers. 34 34 35 35 These 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.