Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 595479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:04:00+00:00 2026-05-13T16:04:00+00:00

when copying arrays using std::copy is there a way to check to see if

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:04:00+00:00Added an answer on May 13, 2026 at 4:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Copying a File using a straight-forward approach in Python is typically like this: def
I particularly like the simplicity of using STL containers in the straightforward way. I
Suppose I have an array unsigned char arr[]= {0,1,2,3,4,5,6,7,8,9}; Is there a way to
A std::vector can be constructed using a C-array like so: std::vector<int> vec(ary, ary +
How could I accomplish copying one jagged array to another? For instance, I have
I'm copying ListViewItems from one ListView to another, sth. like: foreach (ListViewItem item in
I'm having trouble copying a row from one table to another using sqlite3 in
I looked into the GCC STL (4.6.1) and saw that std::copy() uses an optimized
Code: #include <iostream> #include stdafx.h #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut;
I am using a datatype of std::vector<std::vector<T> > to store a 2D matrix/array. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.