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?
Related
I have run this code on another Linux environment where it works, but when I run this code on my machine it shows an error.
The code is:
void *functionC(void* ptr)
{
dint* pointer=(int*)ptr;
pthread_mutex_lock( &mutex1 );
int i;
for( i=pointer[0]; i <= pointer[1]; i++ )
{
sum += myarray[i];
}
pthread_mutex_unlock( &mutex1 );
}
The error I got in my machine is:
aftab#aftab-VirtualBox:~/Downloads$ gcc -o out done1.c -lpthreads
done1.c: In function ‘functionC’:
done1.c:59:2: error: unknown type name ‘dint’
dint* pointer=(int*)ptr;
The error that gcc is complaining about is that there is an unknown type name ‘dint’.
There is no basic type dint in c++, so it must be declared somewhere.
In the version of the code you are copying from there is probably either a typedef or a header file that you haven't included.
From a little looking, I don't see any reference to a dint in pintos, so it's probably defined somewhere in the original file.
Look for a line like:
typdef int dint;
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.
I have a macro in my C++ code, macro has an unused variable. I am getting warning for that variable
the macro is to print the class and method name
#define LOG_ENTER(func_name, message) \
LOG_SET_METHOD(#func_name) \
LOG_MOD_INTERNAL(TC_TAG(ENTER) << message)
#define LOG_SET_METHOD(name) static const char LOG__METHOD__[] = "::" name "() ";
We are using gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC).
"warning: unused variable "LOG__METHOD__" "
How to suppress this warning? It causing more noise!!
The usual way of silencing this warning is to use the variable in a dummy expression :
int main() {
int i;
i;
}
However, this triggers "Warning : statement has no effect", because i has no side effect and its value is not used. To silence this one, we explicitly ignore the value :
int main() {
int i;
(void)i;
}
And there goes the warning.
A way to disable warning:
template <typename T>
void UnusedVar(const T&) {}
And then use:
UnusedVar(my_var);
Casting to void is also a common way (but doesn't work for all compiler):
(void) my_var; // or static_cast<void>(my_var)
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)
I'm trying to use a simple #pragma omp parallel for under Visual Studio 10 and I get an error I don't understand
Here's what I do :
#pragma omp parallel for
for (int i(0); i < size; ++i)
{
// do some stuff
}
And I get these errors when compiling :
error C2059: syntax error : 'constant' // on the for() line
error C2059: syntax error : ';' // on the for() line
error C2143: syntax error : missing ';' before '{'
// repeat previous error for every { or } in file
fatal error C1004: unexpected end-of-file found // on last line of file
openmp support is activated in the compiler options. This code compiles and runs perfectly fine without openmp instructions.
I tried to nest the for loop in braces like this :
#pragma omp parallel for
{
for (int i(0); i < size; ++i)
{
// do some stuff
}
}
but then compiler tells me he expects a for loop right after the #pragma instruction.
Does anyone see what I can be doing wrong here ? It drives me crazy since I have already successfully used OpenMP within the same conditions in other programs.
I don't think object style initialisers are supported inside the for loop control block when OpenMP is active. You should rewrite your code as:
for (int i = 0; i < size; ++i)
In the second case the error is due to the fact that omp for requires an immediately following for loop and not a code block.