Compiling the following code in Release configuration with SDL checks disabled:
#include <immintrin.h>
int main()
{
const auto Set128Epi16 = []()
{
#ifdef NDEBUG
#pragma warning( push )
#pragma warning( disable : 4700 )
__m128i x = _mm_cmpeq_epi16( x,x );
x = _mm_srli_epi16( x,15 );
return _mm_slli_epi16( x,7 );
#pragma warning( pop )
#else
__m128i x = _mm_setzero_si128();
x = _mm_cmpeq_epi16( x,x );
x = _mm_srli_epi16( x,15 );
return _mm_slli_epi16( x,7 );
#endif
};
const auto xmm = Set128Epi16();
return *xmm.m128i_i32;
}
Gives the following output:
1>------ Rebuild All started: Project: pragmatic, Configuration: Release Win32 ------
1> main.cpp
1> Generating code
1>e:\projects\pragmatic\pragmatic\main.cpp(10): warning C4700: uninitialized local variable 'x' used
1>e:\projects\pragmatic\pragmatic\main.cpp(10): warning C4700: uninitialized local variable 'x' used
1>e:\projects\pragmatic\pragmatic\main.cpp(10): warning C4700: uninitialized local variable 'x' used
1>e:\projects\pragmatic\pragmatic\main.cpp(10): warning C4700: uninitialized local variable 'x' used
1>e:\projects\pragmatic\pragmatic\main.cpp(10): warning C4700: uninitialized local variable 'x' used
1> Finished generating code
1> pragmatic.vcxproj -> E:\Projects\pragmatic\Release\pragmatic.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
Why is the compiler ignoring my #pragma in this case. I have in the past successfully used this method to suppress the same warning code.
I copied this from https://msdn.microsoft.com/en-us/library/2c8f766e.aspx
For warning numbers in the range 4700-4999, which are the ones associated with code generation, the state of the warning in effect when the compiler encounters the open curly brace of a function will be in effect for the rest of the function. Using the warning pragma in the function to change the state of a warning that has a number larger than 4699 will only take effect after the end of the function. The following example shows the correct placement of warning pragmas to disable a code-generation warning message, and then to restore it.
So you probably need to put the pragma before the start of main, or maybe before the lambda would work, but I'm not sure about that.
Related
The following code was compiled with MSVC 19.29.30133.0
Given the following c++ line in a function
int foo = 0;
If the variable isn't referenced, MSCV will give you the following warning: warning C4189: 'foo': local variable is initialized but not referenced
However, if const is added in front of it, the warning disappears.
const int foo = 0; // <<--This line yields no warning with MSVC 19.29.30133.0
But this unused variable should still be removed, isn't it?
So why is it that MSVC gives no warning? Is it a bug, is there a specification for unused constants?
minimal reproduction:
int main() {
int a = 0;
const int b = 0;
return 0;
}
output:
Starting build...
cl.exe /Zi /EHsc /nologo /W4 /std:c++20 /Fe: F:\prog\INF1005C\tst\unused_constant.exe F:\prog\INF1005C\tst\unused_constant.cpp
unused_constant.cpp
F:\prog\INF1005C\tst\unused_constant.cpp(2): warning C4189: 'a': local variable is initialized but not referenced
Build finished with warning(s).
Im starting to learn c++ and was under the impression that by putting const is means that the value wont change but i wrote the following code:
#include<iostream>
int main()
{
const int a = 1;
a += 1;
std::cout << a << std::endl;
return 0;
}
and it prints out 2 while i thought it would have given me an error for changing a const int value.
I am using MSVS as my compiler
EDIT: I get a compiler warning saying C4530: C++ exception handler used, but unwind semantics are not enabled, specify /EHsc
It works now and gives me the correct error but does anyone know what this means
This program cannot be compiled using GNU GCC 4.8:
alioth% g++ x.cpp
x.cpp: In function ‘int main()’:
x.cpp:6:7: error: assignment of read-only variable ‘a’
a += 1;
Either your compiler is broken or you are doing something wrong (like compiling different project).
This program cannot be compiled on VS2013:
1>------ Build started: Project: SOTesting, Configuration: Release Win32 ------
1> Source.cpp
1>Source.cpp(6): error C3892: 'a' : you cannot assign to a variable that is const
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========`
The posted code:
#include<iostream>
int main()
{
const int a = 1;
a += 1;
std::cout << a << std::endl;
return 0;
}
The claim that this code compiled and produced "2" as output, is incorrect.
You can easily get the impression of something like that by inadvertently compiling a different program, or not noticing that a compilation failed and then running an existing executable.
I am new to boost and I was trying to use the isomorphism.hpp file for directed graphs.
while trying to run the code, in the boost library file I am seeing the error
1>c:\boost_1_55_0\boost\graph\isomorphism.hpp(142): error C4703: potentially uninitialized local pointer variable 'v' used
1>c:\boost_1_55_0\boost\graph\isomorphism.hpp(147): error C4703: potentially uninitialized local pointer variable 'v' used
It is being thrown from here,
BGL_FORALL_VERTICES_T(v, G1, Graph1){
f[v] = graph_traits<Graph2>::null_vertex(); //error thrown here
}
This is defined in boost/graphs/iteration_macros.hpp as seen below:
#define BGL_FORALL_VERTICES_T(VNAME, GNAME, GraphType) \
for (std::pair<typename boost::graph_traits<GraphType>::vertex_iterator, \
typename boost::graph_traits<GraphType>::vertex_iterator> BGL_RANGE(__LINE__) = vertices(GNAME); \
BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
for (typename boost::graph_traits<GraphType>::vertex_descriptor VNAME; \
BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true):false; \
++BGL_FIRST(__LINE__))
Where do we need to define this? Is this a known issue?
1>c:\boost_1_55_0\boost\graph\isomorphism.hpp(142): error C4703: potentially uninitialized local pointer variable 'v' used
Given...
BGL_FORALL_VERTICES_T(v, G1, Graph1)
...and...
#define BGL_FORALL_VERTICES_T(VNAME, GNAME, GraphType)
...we know v is known as VNAME within the macro.
The condition on the outer loop is:
BGL_FIRST(__LINE__) != BGL_LAST(__LINE__)
The condition on the inner loop is:
BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true):false;
Given the inner loop doesn't run unless the outer loop condition is satisfied, we can simplify:
true ? (VNAME = *BGL_FIRST(__LINE__), true):false;
(VNAME = *BGL_FIRST(__LINE__), true) ;
Clearly VNAME is always assigned to and never used uninitialised. Your compiler's analysis is flawed, and you should disable the warning (only for this specific code if at all possible), otherwise turn off any treat-warnings-as-errors compiler option so your build doesn't break completely or try another compiler.
Go to
Project > "ProjectName" Properties > C/C++ > General
There you should switch "SDL checks" from Yes to No.
Visual Studio 2019 (platform toolset v142) with C++20 enabled and warning level 4 gives a lot of warnings. To silence them:
#pragma warning(push)
#pragma warning(disable: 4244) // '-=': conversion from '__int64' to 'int', possible loss of data
#pragma warning(disable: 4267) // '=' : conversion from 'size_t' to 'int', possible loss of data
#pragma warning(disable: 4456) // declaration of 'xxx' hides previous local declaration
#pragma warning(disable: 4701) // potentially uninitialized local variable 'xxx' used
#pragma warning(disable: 4703) // potentially uninitialized local pointer variable 'xxx' used
#include <boost/graph/isomorphism.hpp>
#pragma warning(pop)
Consider the following code
int TestReadUninitStack()
{
printf("Reading an uninitialized stack variable\n");
int i;
volatile int j = i;
fprintf(stderr, "ERROR: Uninitialized stack variable read not detected!\n");
return 2;
}
When I compiled my project with /we4700, this piece of code fails with Compile Error
error C4700: uninitialized local variable 'i' used
To get around the compile Error, I added a pragma to temporarily disabled C4700 Warning around the code block, but unfortunately that does not seem to work
int TestReadUninitStack()
{
printf("Reading an uninitialized stack variable\n");
#pragma warning( push )
#pragma warning( disable : 4700 )
int i;
volatile int j = i;
#pragma warning( pop )
fprintf(stderr, "ERROR: Uninitialized stack variable read not detected!\n");
return 2;
}
Any idea, how to possibly deal with it?
I got this warning message.. but i dont know what/where the problem is..!
includes
#pragma warning(push)
#pragma warning(disable:4996)
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>
#pragma warning(pop)
and the warning
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2227): warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2212): Siehe Deklaration von 'std::_Copy_impl'
1> c:\users\perlig\documents\visual studio 2010\projects\restmanager\restmanager\**http.cpp(257)**: Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_OutIt std::copy<boost::archive::iterators::insert_linebreaks<Base,N>,boost::archive::iterators::ostream_iterator<Elem>>(_InIt,_InIt,_OutIt)".
1> with
1> [
1> _OutIt=boost::archive::iterators::ostream_iterator<char>,
1> Base=boost::archive::iterators::base64_from_binary<boost::archive::iterators::transform_width<const char *,6,8>>,
1> N=76,
1> Elem=char,
1> _InIt=boost::archive::iterators::insert_linebreaks<boost::archive::iterators::base64_from_binary<boost::archive::iterators::transform_width<const char *,6,8>>,76>
1> ]
the code occur in line 257 as the warning message says. but i´m not able to fix it because i not know what is wrong..
string data contains a "user:password" string for basic auth via http.
http.cpp(257):
// typdef, prepare
using namespace boost::archive::iterators;
stringstream os;
typedef
insert_linebreaks< // insert line breaks every 72 characters
base64_from_binary< // convert binary values ot base64 characters
transform_width< // retrieve 6 bit integers from a sequence of 8 bit bytes
const char *,
6,
8
>
>
,76
>
base64_text; // compose all the above operations in to a new iterator
// encrypt
#pragma warning(push)
#pragma warning(disable:4996)
copy( //<<<<<------ LINE 257
base64_text(data.c_str()),
base64_text(data.c_str() + data.size()),
boost::archive::iterators::ostream_iterator<char>(os)
);
#pragma warning(pop)
anybody got any idea?
I think you know what is the meaning of the warning but first I describe the warning and then say what to do to get rid of it. Microsoft implemented a new security enabled set of function in its CRT, STL, MFC, ... and mark old version of those functions as deprecated to provide a hint for you that you should migrate to new secure version. so it say std::copy is unsafe!! how? as follow:
char storage[ 10 ], *p = storage;
std::copy( std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), p );
Now what will happened if user input more than 10 int? the memory will be overwritten and you corrupted your memory.
Using boost::archive::iterators::ostream_iterator is perfectly safe but since it does not follow the design of safe iterators in MSVC it will considered as unsafe.
Now you should either disable this warning by -D_SCL_SECURE_NO_WARNINGS to cl input flags or add a pragma to disable this warning( as you do ), but why pragma don't work?
the reason is obvious, this pragma work on scope and the scope that you use pragma on it have nothing wrong, you must guard xutility with this pragma and every thing will work as expected.