Thread: Unknown woriking code on merging arrays

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    Unknown woriking code on merging arrays

    Hello,

    I am reading a code, that is working, but I think it's wrong, and it should crash, but it does not. It merges Array1 into Array2 if there is enough space in Array2 to fit it, but first moves Array2 elements enough spaces to fit the elements from Array1.

    Code:
    struct array
    {
        int size;
        int* data;
        array(int s) : size(s) { data = new int[size]; }
    };
    
    // copy elements from src to dst
    static void merge_array(const array* const src, array* dst)
    {
        if (dst->size < src->size) {
            return; // cant do that - destinatin size is smaller than source
        }
        // move the elements of destination to the end
        for(int i=0, j=dst->size; i < src->size; ++i, ++j) {
            dst->data[j] = dst->data[i];
        }
    
        // append the source to the remaining elements
        for(int i=0; i < src->size; ++i) {
            dst->data[i] = src->data[i];
        }
    }
    Why it is not crashing by the array out of index?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If it's accessing out of bounds, then it's broken.

    Just because it doesn't crash doesn't mean it isn't broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    121
    Undefined behaviour?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Yes, you're invoking undefined behavior so anything is possible.

    By the way one of your comments is wrong:
    // move the elements of destination to the end
    This, with the code provided, should read "// move the elements past the end of the array".

    How do you know how many elements the destination array has free? How do you know how many elements the source array is actually using?

    Lastly using array as a name is probably not a good idea in a C++ program since there is already a std::array class.

    Jim

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merging Arrays!
    By djwc77 in forum C Programming
    Replies: 4
    Last Post: 07-18-2013, 12:46 AM
  2. Merging Arrays
    By yigster in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2010, 03:16 AM
  3. Merging arrays help
    By key4life in forum C Programming
    Replies: 12
    Last Post: 12-05-2009, 06:46 PM
  4. merging arrays
    By acohrockz21 in forum C Programming
    Replies: 28
    Last Post: 03-09-2008, 02:28 AM
  5. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM

Tags for this Thread