Thread: Fast 1D to 2D Array Copy

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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,149
    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, 12:28 PM