compiler-warnings

g++ “warning: iteration … invokes undefined behavior” for Seemingly Unrelated Variable

旧巷老猫 提交于 2019-12-10 16:29:58
问题 Consider the following code in strange.cpp : #include <vector> using namespace std; int i = 0; int *bar() { ++i; return &i; } int main() { for(size_t j = 0; j < 99999999999; ++j) // (*) { const auto p = bar(); if(!p) // (**) return -1; } } Compiling this with g++ gives a warning: $ g++ --std=c++11 -O3 strange.cpp strange.cpp: In function ‘int main()’: strange.cpp:12:12: warning: iteration 4294967296ul invokes undefined behavior [-Waggressive-loop-optimizations] ++i; ^ strange.cpp:19:9: note:

Why doesn't GCC throw a warning in this example

北城余情 提交于 2019-12-10 14:55:36
问题 With -Wsequence-point enabled, GCC should warn user when undefined behavior code is spotted. For example b = a + ++a; should be noticed by GCC and should be reported as "undefined behavior" code (because ISO C doesn't specify the order of evaluating operands for addition). However, I played with the syntax and I tried this one: int *a = malloc(sizeof(int) * 2); a[0] = 1; printf("%d\n", *(a + (*a)++ - *a)); Of course, I got the warning warning: operation on '*a' may be undefined [-Wsequence

Visual Studio: Warn on Missing XML Documentation for Private and Internal Members

孤街醉人 提交于 2019-12-10 14:05:14
问题 I am using Visual Studio 2005 (VS.8.0) and I am looking to enforce the requirement that all class members, not just the public ones, be documented. While trivial to setup Visual Studio to generate warnings when public, protected, or internal protected members are not documented, I am looking for a way to have the private and internal members throw this same warning. Any suggestions? NOTE: I am using warning level 4, am treating Warnings as Errors, and have the Generate XML flag set. 回答1:

Can C# compiler be configured to give warning when explicit cast may cause data loss?

此生再无相见时 提交于 2019-12-10 13:08:58
问题 Is there a way to configure the VS2008 C# compiler to give a warning for code like this: Int64 x = 123456789000; Int32 y = (Int32)x; 回答1: The whole point of an explicit cast is to say that "I take responsibility for the problem, please just do it." In your trivial case, it would perhaps be easy for the compiler to figure out that the value would not fit in a Int32, and thus produce your warning. However, what about this: Int64 x = CallSomeMethod(); Int32 y = (Int32)x; How can it warn you

Why this erasure warning with member variables declared as a tuple?

老子叫甜甜 提交于 2019-12-10 12:45:53
问题 Have a look at this Scala class: class Example { val (x, y): (Int, Int) = (1, 2) } Compiling this results in a warning: Example.scala:2: warning: non variable type-argument Int in type pattern (Int, Int) is unchecked since it is eliminated by erasure val (x, y): (Int, Int) = (1, 2) ^ Removing the explicit type annotation gets rid of this warning: class Example { val (x, y) = (1, 2) } Why do I get the warning and why does removing the explicit type annotation get rid of it? As far as I can see

Why MSVC generates warning C4127 when constant is used in “while” - C

这一生的挚爱 提交于 2019-12-10 12:45:49
问题 For code, while(1) { /* ..... */ } MSVC generates the following warning. warning C4127: conditional expression is constant MSDN page for the warning suggests to use for(;;) instead of while(1) . I am wondering what advantage for(;;) is giving and why it warns for the constant usage in while ? What flag should I use on GCC to get the same warning? 回答1: Constant conditionals are often enough simply bugs. Consider this: unsigned k; ... while (k>=0) { ... } The condition k>=0 would make sense if

Compile C with function gethostbyname to static linked error

喜你入骨 提交于 2019-12-10 10:28:50
问题 I'm trying compile program using function gethostbyname() with the cross compiler arm-none-linux-gnueabi , but it did not work when I run my binary on android. My code in below: /* gethostbyname-example.c */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> extern int h_errno; int main(int argc,char **argv) { int x, x2; struct hostent *hp; for ( x=1; x<argc

How to wisely interpret this compiler warning?

岁酱吖の 提交于 2019-12-10 04:35:30
问题 When I executed the code of this question, I got this warning: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=] printf("P-Q: %d, P: %d, Q: %d", (p - q), p, q); ~^ ~~~~~~~ %ld As a reflex fix, I used %ld to print the subtraction of two pointers. And the compiler agreed. Fortunately, I saw a comment from another user mentioning that %td should be used, since the result type of the subtraction is ptrdiff_t . This answer confirms this claim. Now

Compiler gets warnings when using strptime function (C)

一世执手 提交于 2019-12-10 03:18:58
问题 Typing man strptime it sais that this function needs to have declared _XOPEN_SOURCE and included time.h header. I did it. But, when I try to compile my code I get: ./check.c:56: warning: implicit declaration of function ‘strptime’ Look at my code: int lockExpired(const char *date, const char *format, time_t current) { struct tm *tmp = malloc(sizeof(struct tm *)); time_t lt; int et; strptime(date, format, tmp); lt = mktime(tmp); et = difftime(current, lt); if (et < 3600) return -et; return 1;

Is there an equivalent to -pedantic for gcc when using Microsoft's Visual C++ compiler?

試著忘記壹切 提交于 2019-12-10 02:44:44
问题 I would like to have my warnings set to the highest level using Microsoft Visual C++ compiler. Similar to using -pedantic on gcc. What compiler switches do you use to have the most warnings enabled? 回答1: The highest warning level on Visual C++ is /Wall. The warning level can also be set numerically with /W0, /W1, ... /W4 to generate increasing levels of warnings. The compiler will also check for 64 bit portability issues with /Wp64. And you can tell it to treat warnings as errors with /WX