compiler-warnings

warning : 'integer conversion results in truncation'

自作多情 提交于 2019-12-24 02:05:40
问题 I get an warning here. The warning says 'integer conversion results in truncation'. It persists even if I remove the typecast(U16). typedef unsigned short U16; U16 mask; mask = ~(U16)(0x8000); How do I resolve this warning? I used the below code and removed the warning, but unsure if its the right way to do it. mask = (U16)(~(U32)(0x8000)); Thanks in advance! 回答1: C compilers don't like when you try to assign constant values into an L-value that's not big enough to hold them. I would guess

How to be warned about pointers to out-of-scope local variables

浪尽此生 提交于 2019-12-23 21:01:07
问题 Consider the following code: #include <stdio.h> void badidea(int**); int main(void) { int* p; badidea(&p); printf("%d\n", *p); /* undefined behavior happens here: p points to x from badidea, which is now out of scope */ return 0; } void badidea(int** p) { int x = 5; *p = &x; } The intent seems to be that it will print 5 , but it actually invokes undefined behavior, due to dereferencing a pointer to an out-of-scope local variable in main . How can I find instances of this problem in a codebase

Avoid incompatible pointer warning when dealing with double-indirection

ⅰ亾dé卋堺 提交于 2019-12-23 15:44:42
问题 Assuming this program: #include <stdio.h> #include <string.h> static void ring_pool_alloc(void **p, size_t n) { static unsigned char pool[256], i = 0; *p = &pool[i]; i += n; } int main(void) { char *str; ring_pool_alloc(&str, 7); strcpy(str, "foobar"); printf("%s\n", str); return 0; } ... is it possible to somehow avoid the GCC warning test.c:12: warning: passing argument 1 of ‘ring_pool_alloc’ from incompatible pointer type test.c:4: note: expected ‘void **’ but argument is of type ‘char **’

C error:format '%s' expects argument of type 'char *'but argument 2 has type 'char (*)[100]'

ⅰ亾dé卋堺 提交于 2019-12-23 12:53:53
问题 I'm working on an exercise in c the last few days and I'm having this warning (as the title suggests). I've tried a bunch of stuff but I don't really know how to exactly fix this. I'm not good at programming so there are mistakes. Below are the structs I'm using (which cannot be changed because that's how they are given): typedef struct bookR* book; struct bookR{ char author[MAXSTRING]; enum genres{fiction,scientific,politics}; int id; char review[MAXLINES][MAXSTRING]; }; typedef struct nodeR

Why do I not get compiler warning about access uninitialized member variable in ctor?

和自甴很熟 提交于 2019-12-23 11:16:10
问题 Here is a simple test case that compiles without any warning. Looks like a common mistake but clang, gcc and visual studio doesn't emit warning in this case. Why? class Image { private: int width, height; int* array; public: Image(int _width, int _height); void crashTest(); }; Image::Image(int _width, int _height) { array = new int[width * height]; // ^^^^^ ^^^^^^ this is wrong // I expect a warning here e.g.: 'width is uninitialized here' width = _width; height = _height; } void Image:

Why do I not get compiler warning about access uninitialized member variable in ctor?

最后都变了- 提交于 2019-12-23 11:15:11
问题 Here is a simple test case that compiles without any warning. Looks like a common mistake but clang, gcc and visual studio doesn't emit warning in this case. Why? class Image { private: int width, height; int* array; public: Image(int _width, int _height); void crashTest(); }; Image::Image(int _width, int _height) { array = new int[width * height]; // ^^^^^ ^^^^^^ this is wrong // I expect a warning here e.g.: 'width is uninitialized here' width = _width; height = _height; } void Image:

What does -Wformat=2 do?

守給你的承諾、 提交于 2019-12-23 11:01:13
问题 I'm seeing numerous mentions on the web of a -Wformat=2 option to Clang, sometimes alongside the already-known -Wformat (the one that Xcode lists as “Typecheck calls to printf / scanf , although it covers many more string-formatting APIs now). Does this do anything at all? If it does, what, if anything, does it do differently from -Wformat ? Is it useful to have both, or is either one a superset of the other? 回答1: If it does, what, if anything, does it do differently from -Wformat? It's

How to make Visual Studio 2010 warn about unused variables?

ⅰ亾dé卋堺 提交于 2019-12-23 09:34:25
问题 #include <string> using namespace std; int main() { string s; // no warning int i; // warning C4101 return 0; } Why does Visual Studio warn me about the unused variable i but not about s in the example? I assume that the compiler is not sure about side effects of the string constructor. Is this the reason for not showing a warning? Can I somehow enable warnings about unused string variables? My warning level is set to 4. 回答1: I hypothesize that compilers only warn about unused variables for

Mark method call that it always returns not null result

 ̄綄美尐妖づ 提交于 2019-12-23 09:31:53
问题 Scala compiler has -Xcheck-null which tries to check if there are any potential null pointer dereference in runtime. It's ok for me, but I get too much false positives, i.e. suppose I define logger : private final val LOGGER: Logger = LoggerFactory.getLogger(classOf[GenericRestImpl]) The method getLogger never returns null . How can I pass this knowledge to compiler so it will not complain? [WARNING] TestImpl.scala:31: warning: potential null pointer dereference: LOGGER.debug [WARNING] LOGGER

“Mixed declaration and code” warning, is it worth addressing?

自古美人都是妖i 提交于 2019-12-23 07:31:19
问题 I've recently enabled -pedantic option on gcc and now I've got about two or three pages of "ISO C90 forbids mixed declaration and code" warnings. My goal with this project is to be able to deploy it on any mainstream system with a c compiler, so I realize it wouldn't be wise to assume that C99 will be supported everywhere, but is it even worth my time to address these warnings? Are there still systems out there with c compilers that don't support mixed declaration and code? 回答1: Well, was/is