Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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));	
	
		}
 
Last edited:
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.
 
Code:
float **sprow;
sprow = (float **) calloc(FOCPLWIDTH , sizeof(float*));
	for (i = 0; i < FOCPLWIDTH; i++)[COLOR="Red"][B];[/B][/COLOR]
	sprow[i] = (float*) calloc(FOCPLWIDTH, sizeof(float));

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.
 
Last edited:
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.

Code:
void *new_matrix(size_t x, size_t y, size_t elem_width);
 
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);

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.
 
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.

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)
 
Please explain the fix.

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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.