How to copy the contents of std::vector to c-style static array,safely?
[问题]
I am getting warning when using the std copy function.
I have a
bytearray that I declare.
byte *tstArray = new byte[length];
Then I have a couple other byte arrays that are declared and initialized with some hex values that i would like to use depending on some initial user input.
I have a series of if statements that I use to basically parse out the original input, and based on some string, I choose which byte array to use and in doing so copy the results to the original tstArray.
For example:
if(substr1 == "15") { std::cout<<"Using byte array rated 15"<<std::endl; std::copy(ratedArray15,ratedArray15+length,tstArray); }
The warning i get is warning C4996: 'std::copy': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct.
A possible solution is to to disable this warning is by useing -D_SCL_SECURE_NO_WARNINGS, I think. Well, that is what I am researching.
But, I am not sure if this means that my code is really unsafe and I actually needed to do some checking?
[回答1]
https://stackoverflow.com/questions/2321908/issue-with-using-stdcopy
C4996 means you're using a function that was marked as
__declspec(deprecated). Probably using D_SCL_SECURE_NO_WARNINGS will just #ifdef out the deprecation. You could go read the header file to know for sure.
But the question is why is it deprecated? MSDN doesn't seem to say anything about it on the std::copy() page, but I may be looking at the wrong one. Typically this was done for all "unsafe string manipulation functions" during the great security push of XPSP2. Since you aren't passing the length of your destination buffer to std::copy, if you try to write too much data to it it will happily write past the end of the buffer.
To say whether or not your usage is unsafe would require us to review your entire code. Usually there is a safer version they recommend when they deprecate a function in this manner. You could just copy the strings in some other way. This article seems to go in depth. They seem to imply you should be using a std::checked_array_iterator instead of a regular OutputIterator.
Something like:
stdext::checked_array_iterator<char *> chkd_test_array(tstArray, length); std::copy(ratedArray15, ratedArray15+length, chkd_test_array);
(If I understand your code right.)
[回答2]
https://stackoverflow.com/questions/633549/how-to-copy-the-contents-of-stdvector-to-c-style-static-array-safely
The problem is that you're adding things to the vector so it ends up with more elements than were in the
myarrarray that you initialized it with.
If you want to copy the vector back into the array, you'll need to size it down:
myvec.resize( MAX_SIZE);
Or you could limit the number of elements you copy back:
copy( myvec.begin(), myvec.begin()+MAX_SIZE, myarr);
If you want the
myarrarray to contain all the elements, then it needs to be larger than
MAX_SIZE, and you've found out why people suggest to use
vectorrather than raw arrays (
vectors know how to grow, arrays do not).
Note that while you don't want 'Any answer that resembles:"You use c++, drop the c style array implementation. Use only vector for all array implementation"', you can often get away with using a
vectorand passing
&myvec[0]to routines that expect a raw array.
vectoris required to store its elements contiguously just like a raw array for just this reason.
Since you're getting the 'unsafe operation' warning, you're using Microsoft's compiler. To fix the problem safely, you're supposed to use the
checked_copyalgorithm instead of
copy. As Evgeny Lazin indicates, you can create a checked iterator for your array to pass to the
checked_copyalgorithm.
Other options to make the copy safe that do not require Microsoft extensions would be to wrap the array in a class (possibly templated) that keeps track of the array size and provides methods to copy data into the array in a safe manner. Something like STLSoft's array_proxy
template orBoost's boost::array
might help.
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- How to combine the value of multiple hashes within an array by the same key
- How to copy values of an array into another array
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- [jQuery] 如何根据名字来选取checkbox数组 -- How to get the array of checkbox via name
- How to (how to refresh/redirect the contents of one frame from another frame )
- how to accepte the array of the checkbox in asp and in php
- How to user SSE2 instructions to improve the performance of memory copy?
- How to read the contents of a remote web page
- how can i get [5, 7, 13, 2] to go to [5*0, 7*1, 13*2, 2*3] then get the sum of this array.
- How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
- how to implement the version control of static files
- How to calculate the inversion of 0/1 array? 求逆序数
- How to get the length of array of strings in C? - Yahoo! Answers
- Ruby: how to sort array of string parsing the content
- 双空结束字符串的拷贝问题 How to copy the pairs of NULL-terminated strings
- How to Fix “HTTP Error 403.14 – Forbidden The Web server is configured to not list the contents of this directory”
- How to maintain the position of the scrollbar on postbacks (across the entire site)
- How to change the default run level of a RedHat 9.0 or Fedora Core Linux system
- How to monitor the full range of CPU performance events