Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std copy question

Tags:

c++

when copying arrays using std::copy is there a way to check to see if the copy went through correctly? especially if you have like hex values in an unsigned char array. I am not sure how one can verify that the copy went through and even if it did the values that were in one array copied over correctly.

like image 285
Dave Powell Avatar asked Oct 25 '25 01:10

Dave Powell


1 Answers

Virtually the only way for a copy to fail is if you're copying to something like a back_insert_iterator, in which case there's a possibility of allocation in the destination container failing. This will normally1 be reported by an exception being thrown.

1For those who want the gory details, in this case "normally" means you haven't provided your own allocator that does something other than throwing an exception in case of failure.

Edit: Perhaps I should have been more explicit: I was talking about a failure that you'd stand a chance of detecting. The precondition on std::copy says that the destination iterator must not be in the source range. Violating that leads to undefined behavior. Likewise, if you do an assignment (either directly or via an algorithm like copy or transform) that's out of range of the target type, you (again) get undefined behavior.

The definition of undefined behavior is that the standard places no requirements on the implementation. In other words, if you do this, anything can happen. Nothing about the behavior of the program is guaranteed at all. There's no sensible way to talk about how to detect such a thing having happened, because the language says that if your program does such a thing, you can't depend on anything.

Edit again: Since there seems to be some misunderstanding about undefined behavior. To do any good, if you're concerned with things that might lead to undefined behavior, you need to prevent them from happening, rather than attempting to do the copy and then try to figure out whether it succeeded. By then it's too late; if the program does something that's undefined, all the behavior of the program is undefined.

So, to deal with the possibility of the destination iterator being within the range of the source iterators, or that the source data is outside the range of the destination data type, you need to check for those up-front, and never even attempt the undefined behavior.

template <class T, class U>
safe_copy(T source_start, T source_end, U dest) { 
    if (std::less(source_start, dest) && std::less(dest, source_end))
        throw(std::range_error("Output iterator within input range");
    std::transform(source_start, source_end, dest, checked_convert);
}

For the moment, I haven't tried to define checked_convert. It might do a saturating conversion, or throw an exception if a value is out of range, etc., -- depending entirely on the type of data involved. One way or another, however, it assures that the source value fits in the destination range before assignment to the destination is attempted.

like image 158
Jerry Coffin Avatar answered Oct 27 '25 14:10

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!