4 ms·
There is no absolutely foolproof way to code in C such that no matter how someone changes the program, they will be spared from making a mistake. If we just pr
by kazinator 8y ago
There is no absolutely foolproof way to code in C such that no matter how someone changes the program, they will be spared from making a mistake.
If we just program for today, sizeof buffer is much better than proliferating a preprocessor constant that may or may not correctly reflect the object being overwritten.
For the silly mistake of changing an array to a pointer without taking care of sizeofs, GCC gives us some diagnostics:
-Wsizeof-pointer-memaccess
Warn for suspicious length parameters to certain string and memory
built-in functions if the argument uses "sizeof". This warning
warns e.g. about "memset (ptr, 0, sizeof (ptr));" if "ptr" is not
an array, but a pointer, and suggests a possible fix, or about
"memcpy (&foo, ptr, sizeof (&foo));". This warning is enabled by
-Wall.
-Wsizeof-array-argument
Warn when the "sizeof" operator is applied to a parameter that is
declared as an array in a function definition. This warning is
enabled by default for C and C++ programs.