Page 3 of 31. Showing 310 results (0.013 seconds)
MSC09-C. Character encoding: Use subset of ASCII for safety
() will include it: char myFilename[1000]; const char elimNewLn[] = "\n"; fgets(myFilename, sizeof(myFilename)-1, stdin); myFilename[sizeof(myFilename)-1] = '\0 … file names that violate the guidelines for selecting safe characters: char myFilename[1000]; const char elimNewln[] = "\n"; const char badChars[] = "-\n\rDCL01-C. Do not reuse variable names in subscopes
]; static const size_t msgsize = sizeof( msg); void report_error(const char *str) { char msg[80]; snprintf(msg, msgsize, "Error: %s\n", str … variable names: #include <stdio.h static char message[100]; static const size_t message_size = sizeof( message); void report_error(const char *str) { char msgENV30-C. Do not modify the object referenced by the return value of certain functions
(), localeconv(), asctime(), and strerror(). In such cases, the function call results must be treated as being const-qualified. The C Standard, 7.24.4.6, paragraph 4 [ISO … ') { if (*c_str == orig) { *c_str = rep; } ++c_str; } } void func(void) { const char *env; char *copy_of_env; env = getenv("TEST_ENVDCL58-CPP. Do not modify the standard namespaces
(const std::string &data) : data(data) {} const std::string &get_data() const { return data; } }; namespace std { template <> struct plus<string> : binary_function<string, MyString, string> { string operator()(const string &lhs, const MyString &rhs) const { return lhs + rhs.get_data(); } }; } void fERR30-C. Take care when reading errno
value (ULONG_MAX), so errno is the only means of determining if strtoul() ran successfully. #include <errno.h #include #include void func(const char *c_str … errno after the call: #include <errno.h #include #include void func(const char *c_str) { unsigned long number; char *endptr; errno = 0; numberMSC37-C. Ensure that control never reaches the end of a non-void function
, returning various values along the execution path where no return statement is defined. #include <string.h> #include <stdio.h> int checkpass(const char *password) { if (strcmp(password, "pass") == 0) { return 1; } } void func(const char *userinput) { if (checkpass(userinput)) { printf("Success\nDCL05-C. Use typedefs of non-pointer types only
Using type definitions (typedef) can often improve code readability. However, type definitions to pointer types can make it more difficult to write const-correct code because the const qualifier will be applied to the pointer type, not to the underlying declared type. Noncompliant Code Example The following typeFIO10-C. Take care when using the rename() function
The rename() function has the following prototype: int rename(const char *src_file, const char *dest_file); If the file referenced by dest_file exists prior … (POSIX) This code example is noncompliant because any existing destination file is removed by rename(): const char *src_file = /* ... */; const char *dest_fileCTR54-CPP. Do not subtract iterators that do not refer to the same container
. #include <cstddef> #include <iostream> template <typename Ty> bool in_range(const Ty *test, const Ty *r, size_t n) { return 0 < (test - r) && (test - r … architecture (such as some antiquated x86 variants). Consequently, it is noncompliant. #include <iostream> template <typename Ty> bool in_range(const Ty *testSTR52-CPP. Use valid references, pointers, and iterators to reference elements of a basic_string
to any standard library function taking a reference to non-const basic_string as an argument. Calling non-const member functions, except operator[], at, front, back, begin, rbegin, end, and rend. Examples of standard library functions taking a reference to non-const std::basic_string are std::swap(), ::operator