memcpy from an array.

Discussion in 'Mac Programming' started by farmerdoug, Feb 18, 2012.

  1. farmerdoug, Feb 18, 2012
    Last edited: Feb 18, 2012

    macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    FOUND THE ERROR. SEEMS TO BE WORKING.

    Array is a one dimensional array.
    I want to copy 100 points that follow array[40] into another_array.
    Can I do it without restructuring array?

    memcpy(another_array, ??, 100*sizeof(float));

    I tried memcpy(another_array, &array[40], 100*sizeof(float));
    Actual code.
    Code:
    float **sprow;
    sprow = (float **) calloc(FOCPLWIDTH , sizeof(float*));
    	for (i = 0; i < FOCPLWIDTH; i++);
    	sprow[i] = (float*) calloc(FOCPLWIDTH, sizeof(float)); 
    
    for(fpy = 0; fpy < FOCPLWIDTH; fpy++)
    	{	
    	memcpy(sprow[fpy], &dbuf[lensy*FOCPLWIDTH], FOCPLWIDTH*sizeof(float));	
    	
    		}
    	
    
     
  2. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    Code:
    void *
         memcpy(void *restrict s1, const void *restrict s2, size_t n);
    Why are you passing it a pointer to a pointer for its second argument when it requires simply a pointer ?

    &dbuf[] translates to a void **...

    Also, sprow is a float **, which again you're passing as a void *, which now points to a float * instead of a float. But you're specifying to copy something the size of a float into it... Are you sure float * and float are the same size on your system ?

    :confused:

    A lot wrong with that code.
     
  3. chown33, Feb 18, 2012
    Last edited: Feb 18, 2012

    macrumors 603

    Joined:
    Aug 9, 2009
    #3
    Misplaced semicolon hilited in red. Result is equivalent to:
    Code:
    float **sprow;
    sprow = (float **) calloc(FOCPLWIDTH , sizeof(float*));
    i = FOCPLWIDTH;
    sprow[i] = (float*) calloc(FOCPLWIDTH, sizeof(float)); 
    

    The sensible solution is to stop writing this code over and over again. Write it once, in a function, in a separate file. Then include that file in every project you ever make, and call the function.
    Code:
    float ** new_array_float_2d( int width, int height );
    
    Write a corresponding function that frees these arrays:
    Code:
    void free_array_float_2d( float ** array );
    
    The goal is to apply some forethought, write the code once, make sure it works, then reuse known-working code. Instead of making foolish avoidable mistakes over and over again.
     
  4. macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    And with that in mind let's make a generic 2d array function that is not tied to any specific type.

    Code:
    void *new_matrix(size_t x, size_t y, size_t elem_width);
    
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    Now you have to explain how to get from void* to float**, or whatever the correct type is for elem_width. Or how to write macros that expand to the correct function call for a type.

    Considering the OP, I think a single-purpose function is more understandable. Frankly, considering the OP also leads me to think nothing will change.
     
  6. macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    Eh, in the same way you can assign void* from malloc to what ever you want.

    Elem size is the size of the type so something like sizeof(int) would probably be passed as argument here.

    Code:
    void *new_matrix(size_t x, size_t y, size_t elem_width) {
        char **p = calloc(sizeof(void*), y);
        if(!p) return NULL;
    
        for(int i = 0; i < x; i++) {
            p[i] = calloc(elem_width, x);
        }
    
        return (void*)p;
    }
    
    Edit: void **p obviously, sorry about that (not that it matters here though)
     
  7. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #7
    Please explain the fix.
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    Code:
    for (i = 0; i < n; ++i)[COLOR="Red"];[/COLOR]
        dosomething (i);
    
    The extra semicolon means that the loop body is just an empty statement, so "dosomething" is called just once with i = n, which in this case will quite likely crash.
     
  9. thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #9
    Thanks for all the replies. I nor sure I understand much of what was said but then in is 5 in the morning and I didn't sleep.
    Backing up: I have a float array(dbuf) of size nxn defined as a one dimensional array by an external library but its actually 2-d image data. I need to processed each row separately. I created sprow - a 2-d array to store the results of the processing.

    sprow[fpy] = singlespline(sprow[fpy]);

    Does this make my code less egregious?

    I do, however, want to take certain repetitive operations out of some functions.

    for (i = 0; i < n; i++)
    x = i;
    is performed many times in many places. I guess I want to make x a global variable. Do I use a separate function to fill x? I'll give it a shot.
     

Share This Page