Disclaimer: it's just another task for beginner so I'm good with the fact that on different compilers and on different architectures I may get different (incompatible) binaries as a result. So it's ok that it will only work only on this particular machine.
I'm writing and reading size_t from a binary file. Writing looks like:
std::string result;
result.append((char *)&block_size_, sizeof(block_size_));
and soon after that result is written to a file.
But when I read it in a same way:
map_binary.copy((char *)&block_size_, sizeof(block_size_), offset);
I get a warning
warning C4996: 'std::basic_string<_Elem,_Traits,_Alloc>::copy': 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> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(1777) : see declaration of 'std::basic_string<_Elem,_Traits,_Alloc>::copy'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
I cannot get why this code is unsafe and looking for a solution other than pretending there is no issues at all (using the -D_SCL_SECURE_NO_WARNINGS).
So, what am I missing?
PS: right now I'm learning C++ using only standard library, so solutions that use boost or something else are unacceptable.
You're not missing anything. VC++ is very keen to warn you if you copy anything to a pointer. In this case there is no guarantee that you will get the size of destination correct. After all you might write something like this map_binary.copy((char *)&block_size_, 4, offset); and then find your code fails with a 64-bit compiler.
If you're happy with your ability to avoid this kind of error then disable or ignore the warning.
Related
I'm trying to compile the cl.hpp from Khronos Groups with VS2010, and I have the following message:
1>c:\users\facundo\documents\visual studio 2010\projects\opencl\opencl\cl.hpp(4757): error C2039: 'resize' : it is not a member of 'cl::vector<T>'
1> with
1> [
1> T=cl_context_properties
1> ]
How to compile C++/OpenCL projects on VS2010 correctly?
cl::vector<> has been deprecated.
By default cl.hpp should pick std::vector<> as the default vector class.
Maybe you defined __NO_STD_VECTOR or you are using cl::vector<> yourself?
While playing around with the C++11 features of Visual Studio 2012, I encountered strange errors when including the "ppltasks.h" header file (which is included via the "future" header file:
Main.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\ppltasks.h(3306): error C2512: 'Concurrency::details::_PPLTaskHandle<Concurrency::details::_NormalizeVoidToUnitType<void>::_Type,Concurrency::task<std::pair<Concurrency::details::_Unit_type,Concurrency::details::_CancellationTokenState *>>::_ContinuationTaskHandle<_InternalReturnType,_TaskType,_Function,Concurrency::details::_FunctionTypeTraits<_Function,_ReturnType>::_Takes_task,Concurrency::details::_TaskTypeTraits<void,false>::_AsyncKind>,Concurrency::details::_ContinuationTaskHandleBase>' : no appropriate default constructor available
1> with
1> [
1> _InternalReturnType=std::pair<Concurrency::details::_Unit_type,Concurrency::details::_CancellationTokenState *>
1> , _Function=Concurrency::||::<lambda_06496b162c644bf2f90c850c3dfa7d5c>
1> , _ReturnType=std::pair<Concurrency::details::_Unit_type,Concurrency::details::_CancellationTokenState *>
1> ]
The error is longer, but you get the gist of it. Has anybody else encountered such an error message from simply including the "future" header, and is there a known solution? Thanks.
Turns out that the Project Settings must have language extensions enabled for the header to compile correctly (as of now).
In Visual C++, when a static assert is fired or there is a template error, doubly clicking on the error itself will take you to the actual template code and not the actual erroneous code that is trying to call it or instantiate it.
To see the code that instantiated it, you have to go to the output window and essentially trace the error by double clicking on the lines that contain a source file with the wordings see reference to XXXXXX being compiled with. This is all great, but when the templates are complex, it is terribly hard to find the source files in all the mess and double click on them. For example (I have changed the names so the error itself may not make sense, but this is usually what you get):
1>templateClass.h(390) : error C2079: 'STATIC_ASSERT'
1> with
1> [
1> __formal=0
1> ]
1> filename.h(390) : while compiling class template member function 'TestClass<T>::TestClass(void)'
1> with
1> [
1> T=s32
1> ]
1> anotherFilename.cpp(131) : see reference to class template instantiation 'TestClass<T>' being compiled
1> with
1> [
1> T=s32
1> ]
1> yetAnotherFileName.cpp(149) : see reference to function template instantiation 'void Test_TestClass<T>(void)' being compiled
1> with
1> [
1> T=some_policy_class
1> ]
So my question is this: Is there a shortcut to traverse the error stack (is that the right terminology?) for a single error? A shortcut similar to CTRL+SHIFT+F12, which is view next error. Would be great if there is a shortcut for view next file in error.
I am converting a large VC++ 6.0 application to VS2010 and keep running into this error for one of the projects:
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const std::basic_ofstream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
Based on the error text and similar questions asked here, I assume that the error is caused by an instance of ofstream passed directly into a functions, instead of being passed by reference.
The problem I have is, to locate the line of code in which the ofstream is passed in the wrong way. The error message only links to the fstream header included in VS2010 and my project uses ofstream all over the place in a code base of several tens of thousand lines of code (none of which written by me).
I would greatly appriciate any help / tips / strategies to locate this type of compiler error. How would you approach the problem of locating this kind of error?
How would you approach the problem of locating this kind of error?
Do a grep (regex search) in your source files for something like
\(([^,]+,)*, (std::)?of?stream [^&]
I am working on porting our C++ code from eVC 4.0 to Visual Studio 2008. Our target is Windows Mobile 6.1 and we are using the Windows Mobile 6.0 SDK for our app.
Our code used stl port when compiled in eVC 4 and we would like to continue to use stl port if at all possible.
Does anyone know what steps are necessary to incorporate STL port (5.2.1) in our application. We have set the include directories in the solution (as we did for the eVC 4.0 project), but we are seeing the errors (below) any time we try to use list push_back or vector insert commands with our own classes (below is the error with our class "TriangleBufferElement"). These commands do seem to work with native types like int, double, etc.
We have ensured all classes have the proper contructors, copy constructors, assignment operators, and comparison operators and all appear to be correct.
Any ideas?
C:\Program Files\Windows CE Tools\stlport\stl/_construct.h(119) : error C2665: 'operator new' : none of the 2 overloads could convert all the argument types
1> C:\Program Files\Windows Mobile 6 SDK\PocketPC\include\../../../Windows Mobile 6 SDK\PocketPC\Include\Armv4i/new(61): could be 'void *operator new(unsigned int,const std::nothrow_t &) throw()'
1> while trying to match the argument list '(unsigned int, TriangleBufferElement *)'
1> C:\Program Files\Windows CE Tools\stlport\stl/_construct.h(134) : see reference to function template instantiation 'void stlp_std::_Copy_Construct_aux<_Tp>(_Tp *,const _Tp &,const stlp_std::__false_type &)' being compiled
1> with
1> [
1> _Tp=TriangleBufferElement
1> ]
1> C:\Program Files\Windows CE Tools\stlport\stl/_vector.h(381) : see reference to function template instantiation 'void stlp_std::_Copy_Construct<_Tp>(_Tp *,const _Tp &)' being compiled
1> with
1> [
1> _Tp=TriangleBufferElement
1> ]
1> C:\Program Files\Windows CE Tools\stlport\stl/_vector.h(376) : while compiling class template member function 'void stlp_std::vector<_Tp>::push_back(const _Tp &)'
1> with
1> [
1> _Tp=TriangleBufferElement
1> ]
1> c:\srcdevbranch\pointlib\dtmconverter\dtm\dtmreader\.\trianglebuffer.h(47) : see reference to class template instantiation 'stlp_std::vector<_Tp>' being compiled
1> with
1> [
1> _Tp=TriangleBufferElement
1> ]
Some links you probably should see (if not seen already):
STLPort Notes:
You may experience problems with default SGI node allocator. I had no such problems, though. Default node allocator is quite fast, so I wouldn't recommend disabling it without serious reason. However, if it causes problems, define _STLP_USE_MALLOC or _STLP_USE_NEWALLOC to get bare malloc()-based or new()-based default allocator.
Though this post says it has not yet been ported to VS2008.
Have you reconfigured STLPort for the new compiler environment? Visual Studio 2008 is quite different from the old eVC++ compiler.
Try:
configure evc9
In my case, this was due to __PLACEMENT_NEW_INLINE defined