Failing to declare an unchanging value const prohibits the function from working with values already cast as const. This problem can be sidestepped by type casting away the const , but doing so violates EXPC. Do not cast away a const qualification.
Can detect violations of this recommendation while checking for violations of recommendation DCLC. Const-qualify immutable objects.
A pointer parameter in a function prototype should be declared as pointer to const if the pointer is not used to modify the addressed object. Search for vulnerabilities resulting from the violation of this rule on the CERT website. The 2nd noncompliant code example seems to be redundant since it is almost identical to the first one but changed the argument declaration in a compliant-way.
The intent of the programmer may be clarified. I've reworded the example. It's confusing because the 2nd code example abides by the rule but fails to compile. In this case failure to compile is a Good Thing. The illustrated problem is unrelated to const-correct function parameters, as the same problem would occur even if the standard strcat were used. It also seems that missing is an example that shows how the properly-declared standard strcat can be used to do things like strcat str,".
I'll grant your point, that that specific line of code fails because the string literal is assigned to a non-const string pointer. It's still useful for completeness. We have a separate rule STRC. Use pointers to const when referring to string literals which covers the issue you describe. Pages Boards. Page tree. In C, constant values default to external linkage, so they can appear only in source files. A pointer to a variable declared as const can be assigned only to a pointer that is also declared as const.
You can use pointers to constant data as function parameters to prevent the function from modifying a parameter passed through a pointer. For objects that are declared as const , you can only call constant member functions. This ensures that the constant object is never modified. You can call either constant or nonconstant member functions for a nonconstant object. You can also overload a member function using the const keyword; this allows a different version of the function to be called for constant and nonconstant objects.
You cannot declare constructors or destructors with the const keyword. Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. A constant member function cannot modify any non-static data members or call any member functions that aren't constant.
For example,. Such constants are useful for parameters which are used in the program but do not need to be changed after the program is compiled. This is useful for returning constant strings and arrays from functions which, because they are implemented as pointers, the program could otherwise try to alter and crash.
Instead of a difficult to track down crash, the attempt to alter unalterable values will be detected during compilation. Of course, the compiler could theoretically have worked that out anyway but C is not that clever. Therefore the subroutine can read the value of the variable passed to it but not alter it because any alterations it makes are only made to the copy and are lost when the subroutine ends. To pass an alterable variable in original C, a rather involved method was used.
This involved using a pointer to the variable as the parameter then altering what it pointed to was used.
0コメント