Analyzers
Support for Analyzers (Intel VTune™ Profiler, Intel Advisor, Intel Inspector)
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
4682 Discussions

False 'unitialized memory access' for vectors?

Jeffrey_H_
Beginner
162 Views

I'm using VS2010.  The C++ console app is 32 bit running on Windows 7 x64.

I'm using the Inspector XE update 8.

In our code I am getting 'uninitialized memory access' errors and found this code example on MSDN that mimics the errors in the same locations as our code.

http://msdn.microsoft.com/en-us/library/ecdecxh1(v=vs.100).aspx

// alg_sort.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>      // For greater<int>( )
#include <iostream>

// Return whether first element is greater than the second
bool UDgreater ( int elem1, int elem2 )
{
   return elem1 > elem2;
}

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator Iter1;

   int i;
   for ( i = 0 ; i <= 5 ; i++ )
   {
      v1.push_back( 2 * i );
   }

   int ii;
   for ( ii = 0 ; ii <= 5 ; ii++ )
   {
      v1.push_back( 2 * ii + 1 );
   }

   cout << "Original vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   sort( v1.begin( ), v1.end( ) );
   cout << "Sorted vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   // To sort in descending order. specify binary predicate
   sort( v1.begin( ), v1.end( ), greater<int>( ) );
   cout << "Resorted (greater) vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   // A user-defined (UD) binary predicate can also be used
   sort( v1.begin( ), v1.end( ), UDgreater );
   cout << "Resorted (UDgreater) vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;
}
 

The first error occurs on this line:

sort( v1.begin( ), v1.end( ), greater<int>( ) );

The other error occurs in the first "for" loop on this line (in the xutility file):

v1.push_back( 2 * i );

Can these errors be flagged as "not an issue"?  is it a flaw in the C++ code or a flaw in the Inspector XE?

Thanks.

0 Kudos
1 Reply
Mark_D_Intel
Employee
162 Views

It's a false positive.

I get the uninitialized read flagged on this line: sort( v1.begin( ), v1.end( ), greater<int>( ) );

The cause is the class template 'greater' has no data members, but classes have a minimum size of one (so the address of an object is meaningful).  The class is loaded (via a one-byte load instruction, if you look at the assembly) to pass as a parameter to sort, but the load is flagged as uninitialized because there is no data in the class to be defined.

IXE has some heuristics to suppress reporting these cases, but not all occurrences get caught.

Reply