Fast 1D to 2D Array Copy

This is a discussion on Fast 1D to 2D Array Copy within the C++ Programming forums, part of the General Programming Boards category; Can anyone suggest a fast method for copying a 1d array to a 2d array, I'm currently doing the following ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Fast 1D to 2D Array Copy

    Can anyone suggest a fast method for copying a 1d array to a 2d array, I'm currently doing the following but seems slow and would like to speed it up some? Is it possible to use memcpy to do this same thing?


    Code:
        
    buffer_p = malloc(MAX_CAPTURE_LENGTH);
    x = 0;
        for (j = 0; j < captureLength16; j+=2)        
        {
            data_matrix[0][x] = *(buffer_p+j);
            data_matrix[1][x] = *(buffer_p+j+1);
            x++;
        }

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    In the memory, an 1d array is same as a 2d array.
    Correct me if I'm wrong...but you can use 2d indices wrt to any array.. Wouldn't that eliminate the need for copying?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    What are the data types?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In this case, memcpy isn't going to be good for you, since you are changing the order of things. If you get your 2D array switched so that the x was first and the 0/1 was second, then you'd be somewhere.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    Why are you using malloc in C++?

    Why do you need to copy?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fast Array Init help.
    By kramerr in forum C Programming
    Replies: 14
    Last Post: 06-11-2010, 03:32 PM
  2. Fast memory copy and realloc()
    By intmail in forum Linux Programming
    Replies: 3
    Last Post: 10-27-2006, 03:46 PM
  3. FAST method to copy char array?
    By MitchellH in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2005, 08:19 AM
  4. Fast dynamic allocation of 3D array
    By Micko in forum C Programming
    Replies: 17
    Last Post: 07-22-2005, 07:10 AM
  5. array help, fast!
    By recluse in forum C Programming
    Replies: 8
    Last Post: 12-06-2004, 11:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21