3

I'm using visual studio 2012 and I isolated a problem in my code to this, but I can't solve it. When running it in release mode it works perfect, but I get an error if I run it in debug. The code is:

#include "stdafx.h"

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

typedef boost::numeric::ublas::matrix<double> BMT; 

BMT fun()
  { 
  BMT mym;
  mym.resize(3,3);
  for(int i = 0; i<9;++i) mym(i/3,i%3)=i; 
  std::cout << mym << std::endl;
  return mym; 
  }

int main(int argc, char* argv[])
  {
  fun();
  //closing message
  std::cout<<std::endl<<"press enter to exit."<<std::endl;
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
  }

and the error in debug is the following:

1>------ Build started: Project: myproject, Configuration: Debug x64 ------
1>  myapp.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory(348): error C4996: 'std::_Uninitialized_copy0': 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 11.0\VC\include\xmemory(333) : see declaration of 'std::_Uninitialized_copy0'
1>          C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/storage.hpp(94) : see reference to function template instantiation '_FwdIt std::uninitialized_copy<const double*,double*>(_InIt,_InIt,_FwdIt)' being compiled
1>          with
1>          [
1>              _FwdIt=double *,
1>              _InIt=const double *
1>          ]
1>          C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/storage.hpp(89) : while compiling class template member function 'boost::numeric::ublas::unbounded_array<T>::unbounded_array(const boost::numeric::ublas::unbounded_array<T> &)'
1>          with
1>          [
1>              T=double
1>          ]
1>          C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/matrix.hpp(160) : see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<T>::unbounded_array(const boost::numeric::ublas::unbounded_array<T> &)' being compiled
1>          with
1>          [
1>              T=double
1>          ]
1>          C:\thirdparty\vs2012\x64\boost_1_53_0\boost/numeric/ublas/matrix.hpp(100) : see reference to class template instantiation 'boost::numeric::ublas::unbounded_array<T>' being compiled
1>          with
1>          [
1>              T=double
1>          ]
1>          junkApp1.cpp(10) : see reference to class template instantiation 'boost::numeric::ublas::matrix<T>' being compiled
1>          with
1>          [
1>              T=double
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Do you know what can the problem be?

German Capuano
  • 5,183
  • 6
  • 23
  • 35

2 Answers2

2

I was having a similar issue using ublas. I am not sure what the cause might be but I suppose it's got to do with ublas' copy on write / copy on demand optimizations. Would it be an option to just use the define -D_SCL_SECURE_NO_WARNINGS and be done with it? I have that set globally as I consider 90% of those warnings OS specifc BS anyway.

  • This removes the error and it works, thanks. Still I'm very curious about this working in release config. – German Capuano Jun 11 '13 at 19:27
  • It won't make any difference. As was stated in the other answer, this is just a warning that Microsoft made up. In my opinion to fortify their own 'extensions' and specializations to C and C++ std libs. For example, they will warn if you use snprintf(). Unless you suppress it with said defines. The only way to do something like snprintf which they won't complain about is a derivate of snprintf that, surprise! is MS specific. So if people use that to get rid of the warning, all of a sudden their code will be MS specific and not compile on other platforms, say Linux, anymore. Just disable that. –  Jun 11 '13 at 20:01
  • what I meant is that for some reason it was working in release config and without defining anything. What you told me is enough to solve the problem, but I'm just curious about it. Thanks. – German Capuano Jun 11 '13 at 20:21
  • It may be that a lot of those check features are only active in debug mode. Which is why you see the warning there instead of in release. Much like an assert for example that would also only be active in Debug but won't alter your program's behavior. –  Jun 12 '13 at 09:07
1

The problem is that you are compiling with warnings treated as errors.

EDIT: Microsoft has decided that certain parts of C++ (and C) are deprecated, and the compiler reports use of those parts as errors unless _SCL_SECURE_NO_WARNINGS (respectively _CRT_SECURE_NO_WARNINGS) is defined.

Casey
  • 41,449
  • 7
  • 95
  • 125
  • The configuration I have is the following: Common Properties -> C/C++ -> General -> Treat Warnings As Errors: No (/WX-) – German Capuano Jun 11 '13 at 19:09
  • My mistake - what used to be "deprecation" *warnings* are now *errors* in VS2012 (See [MSDN](http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/ad38eb97-adad-46bd-86ad-ed4983184a0f/)) – Casey Jun 11 '13 at 19:22
  • Thanks for that. So it means that those functions are not deprecated, but Microsoft considers them deprecated? Also, what's the difference between your solution (_CRT_SECURE_NO_WARNING) and Moose's (-D_SCL_SECURE_NO_WARNINGS)? – German Capuano Jun 11 '13 at 19:36
  • 1
    It appears that [_CRT_SECURE_NO_WARNINGS](http://msdn.microsoft.com/en-us/library/8ef0s5kh(v=vs.110).aspx) applies to the C standard library deprecations, and [_SCL_SECURE_NO_WARNINGS](http://msdn.microsoft.com/en-us/library/aa985974(v=vs.110).aspx) to the C++ standard library. **Thank you, Microsoft, for creating a compiler that is not standard conforming by default.** – Casey Jun 11 '13 at 19:48