PDA

View Full Version : memcpy from an array.




farmerdoug
Feb 18, 2012, 06:15 AM
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.

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));

}



KnightWRX
Feb 18, 2012, 06:22 AM
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.

chown33
Feb 18, 2012, 10:16 AM
float **sprow;
sprow = (float **) calloc(FOCPLWIDTH , sizeof(float*));
for (i = 0; i < FOCPLWIDTH; i++);
sprow[i] = (float*) calloc(FOCPLWIDTH, sizeof(float));


Misplaced semicolon hilited in red. Result is equivalent to:
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.
float ** new_array_float_2d( int width, int height );

Write a corresponding function that frees these arrays:
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.

subsonix
Feb 18, 2012, 12:52 PM
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.

And with that in mind let's make a generic 2d array function that is not tied to any specific type.


void *new_matrix(size_t x, size_t y, size_t elem_width);

chown33
Feb 18, 2012, 01:04 PM
And with that in mind let's make a generic 2d array function that is not tied to any specific type.


void *new_matrix(size_t x, size_t y, size_t elem_width);


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.

subsonix
Feb 18, 2012, 01:07 PM
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.

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.


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)

xStep
Feb 18, 2012, 01:37 PM
FOUND THE ERROR. SEEMS TO BE WORKING.

Please explain the fix.

gnasher729
Feb 18, 2012, 03:06 PM
Please explain the fix.

for (i = 0; i < n; ++i);
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.

farmerdoug
Feb 19, 2012, 04:17 AM
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] = i;
is performed many times in many places. I guess I want to make x[i] a global variable. Do I use a separate function to fill x[i]? I'll give it a shot.