-1

I have written a function, which binary copies one array of structures to another. The problem is that it doesn't work properly, and I don't know why. Everything seems to be OK to me.

  • pixel is a structure with 3 unsigned char's, sizeof(pixel) = 3,
  • pixarr1 is a pointer to two dimensional DYNAMIC array [8][2],
  • pixarr2 is a pointer to two dimensional DYNAMIC array [8][n]. For example, n = 8, thats not important.

I need to do something like this:

for(int i=0; i<8; i++) {
    for(int y=0; y<2; y++) {
        arr2[i][y] = arr1[i][y];
    }
}

But i want to do it "binary". Like this:

void copyStruct(pixel*** pixarr1, int startline, int height, pixel*** pixarr2) {

    pixel** arr1 = *pixarr1;
    pixel** arr2 = *pixarr2;
    unsigned char buffer[16];

    int sizeofstruct = height*3*8;
    int padding;

    for(int i=0; i<=sizeofstruct; i+=16) {
        if ( i+16 > sizeofstruct) {
            padding = sizeofstruct-i;
            memcpy(buffer, arr1+i, padding);
            memcpy(arr2+(startline*8)+i, buffer, padding);
        } else {
            memcpy(buffer, arr1+i, 16);
            memcpy(arr2+(startline*8)+i, buffer, 16);
        }
    }
}

What mistake am I making?

PS. sorry for that problems with source, this editor is ..

PPS. Please dont focus on use of pointers. I must use them. If I print pointers outside and inside the function, I see the same address, so it's good (in my logic).

George Skoptsov
  • 3,831
  • 1
  • 26
  • 44
marxin
  • 3,692
  • 3
  • 31
  • 44
  • Did you actually print out sizeof(pixel) and it was 3? – Kaganar Apr 10 '12 at 19:40
  • 6
    What does "doesn't work properly" mean? – Timo Geusch Apr 10 '12 at 19:40
  • Start by the most simple case, startline = 0 and a small array. Does it work? What is the result you get? Can you post the source and destination array? – J_D Apr 10 '12 at 19:47
  • 2
    Too much hard-coding. Even if you think it's 3 (unlikely IMHO), use `sizeof`. Remember, this is evaluated at compile time, so there's no performance impact. It should also be used, directly or indirectly, for the various strides (* 8, etc.). – Matthew Flaschen Apr 10 '12 at 19:47
  • As far as I can see, you are copying pointers around. Is this what you intended? – celtschk Apr 10 '12 at 19:49
  • possible duplicate of [C: create a pointer to two-dimensional array](http://stackoverflow.com/questions/1052818/c-create-a-pointer-to-two-dimensional-array) – Bo Persson Apr 10 '12 at 19:49
  • 1
    Arghhh my eyes hurt: pixel*** pixarr1. Please pass a reference to a std::vector. Thanks :-). – Robinson Apr 10 '12 at 19:53
  • BTW, could you please write some *high-level* code which does the same your low-level code does, so we have an idea of what you want to achieve? (And after you've done that, I'm sure the best fix to your problem is to just use that high-level code; most probably it's even more efficient than your low-level binary copying) – celtschk Apr 10 '12 at 20:01
  • There is no such thing as C/C++. From the code I see that you are using C, so I'll delete the C++ tag. – Jens Gustedt Apr 10 '12 at 20:04
  • Please read the C FAQ about pointers versus arrays. – Jens Gustedt Apr 10 '12 at 20:06
  • 1
    Every time you pass a triple-pointer, God kills a kitten. – Adam Liss Apr 10 '12 at 20:08
  • Jens Gustedt, im creating arrays with new, so i is c++ actually. But, im using c functions, so its c/c++ ;p. Kaganar, sizeof is 3. I checked it of course. celtschk, of course. I need address of array. Its good construction (rather from c, but its good). I updated my post, so u will know what i mean. – marxin Apr 11 '12 at 07:23
  • Please post the call you're making to this function; I can see it working in some corner cases and not in others. – std''OrgnlDave Apr 11 '12 at 21:11

1 Answers1

0

You can pass an array to a function using a pointer, but you can't pass a multidimensional array using a pointer-to-pointer. You can read more about creating pointers to multidimensional arrays here:

Edit: We really need more information here. What exactly do you mean by "doesn't work properly"? The data is copied but the last byte is missing? The program crashes? The machine bluescreens and starts smoking? Please tell us exactly what you are seeing the program do and how it differs from what you expect.

Given the amount of pointer manipulation being done here, I highly recommend stepping through the code in a debugger and double-checking every pointer every step of the way. That should clearly indicate where things start to go wrong, plus give strong hints as to why.

If the higher-level (non-memcpy) version code works, I recommend examining the assembly emitted for that function and comparing it to the assembly that is emitted for the memcpy-based version. Some libraries implement memcpy with a loop of byte-sized copies, which would make your binary version practically equivalent to the higher-level version. If that is the case, then you probably aren't gaining much by using the memcpy version other than added complexity.

Community
  • 1
  • 1
bta
  • 43,959
  • 6
  • 69
  • 99
  • This is a dynamic array, i forgot to write it. So i can of course. – marxin Apr 11 '12 at 07:20
  • I recognized some problems in my code already, and i will post there a solution when i will finally solve it. Now i havent much time, but soon i will, and i will write what misstakes ive done. – marxin Apr 12 '12 at 06:38