gcc-warning

Pass a two dimensional array to a function of constant parameter

浪子不回头ぞ 提交于 2019-11-26 23:03:40
I learned from C Primer Plus that if you want to protect an array from being accidentally modified by a function, you should add const modifier before the pointer declaration in the header of function definition. Following this sensible advice, in the following minimal example, I'm trying to pass a non-constant two-dimensional array array to the function Sum2D , one parameter of which is a pointer-to-const-int[2] . #include <stdio.h> #define ROWS 2 #define COLS 2 int Sum2D(const int ar[][COLS], int rows); //use `const` to protect input array int main(void) { int array[ROWS][COLS]={{1,2},{3,4}}

Compiler warning for function defined without prototype in scope?

白昼怎懂夜的黑 提交于 2019-11-26 22:05:18
问题 [Question inspired by a comment thread at this answer.] As everyone knows, since C99 it's an error to call a function that hasn't been declared, preferably with a proper prototype. But, going beyond that, I want my compiler to warn me if I define a function without a prototype declaration in scope, presumably included out of the same header file that the callers are using. (Unless the function is static, in which case all of this is moot.) The reason should be obvious: If there's a prototype

Is there a way to get warned about unused functions?

↘锁芯ラ 提交于 2019-11-26 17:38:35
问题 I'd like to find unused functions in a codebase - including across compilations units. I'm using gcc as my compiler. Here's an example: foo.c (assume appropriate foo.h ): void foo() { .... } void bar() { .... } main.c : #include <stdio.h> #include "foo.h" int main(void) { bar(); return 0; } In this example, I'd like to get warned about foo() not being used. There is the -Wunused-function gcc option: -Wunused-function Warn whenever a static function is declared but not defined or a non-inline

warning: incompatible implicit declaration of built-in function ‘xyz’

大城市里の小女人 提交于 2019-11-26 15:47:30
I'm getting a number of these warnings when compiling a few binaries: warning: incompatible implicit declaration of built-in function ‘strcpy’ warning: incompatible implicit declaration of built-in function ‘strlen’ warning: incompatible implicit declaration of built-in function ‘exit’ To try to resolve this, I have added #include <stdlib.h> at the top of the C files associated with this warning, in addition to compiling with the following flags: CFLAGS = -fno-builtin-exit -fno-builtin-strcat -fno-builtin-strncat -fno-builtin-strcpy -fno-builtin-strlen -fno-builtin-calloc I am using GCC 4.1.2:

How to turn on (literally) ALL of GCC&#39;s warnings?

混江龙づ霸主 提交于 2019-11-26 15:39:48
I would like to enable -- literally -- ALL of the warnings that GCC has. (You'd think it would be easy...) You'd think -Wall might do the trick, but nope! Still need -Wextra . You'd think -Wextra might do the trick, but nope! Not all of the warnings listed here (for example, -Wshadow ) are enabled by this. And I still have no idea if this list is comprehensive. How do I tell GCC to enable (no if's, and's, or but's!) all the warnings it has? You can't. The manual for GCC 4.4.0 is only comprehensive for that version, but it does list all the possible warnings for 4.4.0. They're not all on the

How to print the address of a function?

你离开我真会死。 提交于 2019-11-26 14:35:33
问题 I let gcc compile the following example using -Wall -pedantic : #include <stdio.h> int main(void) { printf("main: %p\n", main); /* line 5 */ printf("main: %p\n", (void*) main); /* line 6 */ return 0; } I get: main.c:5: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int (*)()’ main.c:6: warning: ISO C forbids conversion of function pointer to object pointer type Line 5 made my change the code like in line 6. What am I missing to remove the warning when printing a function

How to suppress “unused parameter” warnings in C?

廉价感情. 提交于 2019-11-26 14:06:35
For instance: Bool NullFunc(const struct timespec *when, const char *who) { return TRUE; } In C++ I was able to put a /*...*/ comment around the parameters. But not in C of course, where it gives me the error error: parameter name omitted . Job I usually write a macro like this: #define UNUSED(x) (void)(x) You can use this macro for all your unused parameters. (Note that this works on any compiler.) For example: void f(int x) { UNUSED(x); ... } Flow In gcc, you can label the parameter with the unused attribute . This attribute, attached to a variable, means that the variable is meant to be

How to suppress GCC warnings from library headers?

浪尽此生 提交于 2019-11-26 11:10:49
I have a project that uses log4cxx, boost, etc. libraries whose headers generate lots of (repetitive) warnings. Is there a way to suppress warnings from library includes (i.e. #include <some-header.h>) or includes from certain paths? I'd like to use -Wall and/or -Wextra as usual on project code without relevant info being obscured. I currently use grep on make output but I'd like something better. Phi You may try to include library headers using -isystem instead of -I . This will make them "system headers" and GCC won't report warnings for them. For those using CMake, you can modify your

What&#39;s a proper way of type-punning a float to an int and vice-versa?

心不动则不痛 提交于 2019-11-26 10:41:57
The code below performs a fast inverse square root operation by some bit hacks. The algorithm was probably developed by Silicon Graphics in early 1990's and it's appeared in Quake 3 too. more info However I get the following warning from GCC C++ compiler : dereferencing type-punned pointer will break strict-aliasing rules Should I use static_cast , reinterpret_cast or dynamic_cast instead in such situations? float InverseSquareRoot(float x) { float xhalf = 0.5f*x; int32_t i = *(int32_t*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } Forget casts. Use memcpy

Pass a two dimensional array to a function of constant parameter

﹥>﹥吖頭↗ 提交于 2019-11-26 08:30:54
问题 I learned from C Primer Plus that if you want to protect an array from being accidentally modified by a function, you should add const modifier before the pointer declaration in the header of function definition. Following this sensible advice, in the following minimal example, I\'m trying to pass a non-constant two-dimensional array array to the function Sum2D , one parameter of which is a pointer-to-const-int[2] . #include <stdio.h> #define ROWS 2 #define COLS 2 int Sum2D(const int ar[]