gcc-warning

How to make gcc warn about returning the address of local variables?

霸气de小男生 提交于 2019-11-27 23:40:41
问题 With gcc 4.4.5, I have a warning with the following code. char *f(void) { char c; return &c; } But, when I use a temporary pointer, there is no warning anymore (even if the behavior is wrong). char *f(void) { char c; char *p = &c; return p; } I heard that pointer-analysis is difficult in C, but can gcc warn about such code ? 回答1: Compilers, and most static analyzers, do not try to warn for everything wrong a program might do, because that would entail too many false positives (warnings that

MSVC equivalent of __attribute__ ((warn_unused_result))?

空扰寡人 提交于 2019-11-27 21:57:19
I'm finding __attribute__ ((warn_unused_result)) to be very useful as a means of encouraging developers not to ignore error codes returned by functions, but I need this to work with MSVC as well as gcc and gcc-compatible compilers such as ICC. Do the Microsoft Visual Studio C/C++ compilers have an equivalent mechanism ? (I've tried wading through MSDN without any luck so far.) It's _Check_return_ . See here for examples of similar annotations and here for function behaviour. It's supported since MSVC 2012. Example: _Check_return_ int my_return_must_be_checked() { return 42; } UPDATE FOR MSVC

Warning: cast to/from pointer from/to integer of different size

戏子无情 提交于 2019-11-27 13:04:00
问题 I'm learning Pthreads. My code executes the way I want it to, I'm able to use it. But it gives me a warning on compilation. I compile using: gcc test.c -o test -pthread with GCC 4.8.1. And I get the warning test.c: In function ‘main’: test.c:39:46: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] pthread_create(&(tid[i]), &attr, runner, (void *) i); ^ test.c: In function ‘runner’: test.c:54:22: warning: cast from pointer to integer of different size [-Wpointer

How to print the address of a function?

别说谁变了你拦得住时间么 提交于 2019-11-27 09:13:44
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's address? R.. This is essentially the only portable way to print a function pointer. size_t i; int (

Why does GCC not warn for unreachable code?

主宰稳场 提交于 2019-11-27 07:36:02
问题 I wonder why gcc (4.6.3) gives me no warning for the unreachable code in this example: #include <stdio.h> int status(void) { static int first_time = 1; if (first_time) { return 1; first_time = 0; /* never reached */ } else { return 0; } } int main(int argc, const char *argv[]) { printf("first call %d\n", status()); printf("second call %d\n", status()); return 0; } Note, the purpose of the faulty status() function was to maintain a status. I had expected to get a warning for this with -Wall .

Is there a way to get warned about unused functions?

爷,独闯天下 提交于 2019-11-27 07:23:20
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 static function is unused. This warning is enabled by -Wall. but it's only for static functions - it

Why does GCC only sometimes detect the use of a variable before its initialization? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-27 06:01:35
问题 This question already has an answer here: Compiler not detecting obviously uninitialized variable 4 answers I was reading some code from a book, when I decided to make a change to see what the uninitialized value of sec would be before the while statement: #include<stdio.h> #define S_TO_M 60 int main(void) { int sec,min,left; printf("This program converts seconds to minutes and "); printf("seconds. \n"); printf("Just enter the number of seconds. \n"); printf("Enter 0 to end the program. \n");

What is &&& operation in C

♀尐吖头ヾ 提交于 2019-11-27 05:01:26
问题 #include <stdio.h> volatile int i; int main() { int c; for (i = 0; i < 3; i++) { c = i &&& i; printf("%d\n", c); } return 0; } The output of the above program compiled using gcc is 0 1 1 With the -Wall or -Waddress option, gcc issues a warning: warning: the address of ‘i’ will always evaluate as ‘true’ [-Waddress] How is c being evaluated in the above program? 回答1: It's c = i && (&i); , with the second part being redundant, since &i will never evaluate to false . For a user-defined type,

gcc warning: braces around scalar initializer

北战南征 提交于 2019-11-27 03:13:56
问题 I have look-up-table as defined below and I'm making use of GCC. When I compile I get warnings as warning: braces around scalar initializer What does this warning mean? How should I initialize this LUT? Am I making a mistake in initializing this structures? Help!! typedef struct TECH { float velocity1, velocity2; float temp; float measure; int id; float storage[64]; }TECH; struct TECH lut_model_1[2] = {{{296.001465}, {74.216972}, {2.025908}, {1.516384}, {1}, {0.001746, 0.000256, 0.006216, 0

Pedantic gcc warning: type qualifiers on function return type

醉酒当歌 提交于 2019-11-27 01:12:08
问题 When I compiled my C++ code with GCC 4.3 for the first time, (after having compiled it successfully with no warnings on 4.1, 4.0, 3.4 with the -Wall -Wextra options) I suddenly got a bunch of errors of the form warning: type qualifiers ignored on function return type . Consider temp.cpp : class Something { public: const int getConstThing() const { return _cMyInt; } const int getNonconstThing() const { return _myInt; } const int& getConstReference() const { return _myInt; } int&