filling an array in a loop

佐手、 提交于 2020-01-07 09:23:23

问题


I have a function that constantly gets called. I have 3 smaller arrays that I need to pack into a larger array each loop

float* a;
float* b;
float* c;
float* abc;

a = calloc(1, sizeof(float)*3);
b = calloc(1, sizeof(float)*3);
c = calloc(1, sizeof(float)*3);
abc = calloc(1, sizeof(float)*9);


void update() {

   for(int i = 0; i < 3; i++) {
     fill_a(a);
     fill_b(b)
     fill_c(c);

     abc[0] = a[0];
     abc[1] = b[0];
     abc[2] = c[0];

     abc[0] = a[1];
     abc[1] = b[1];
     abc[2] = c[1];

     abc[0] = a[2];
     abc[1] = b[2];
     abc[2] = c[2];
   }
}

free(a);
free(b);
free(c);
free(abc);

the implementation above has problems because the values get overwritten on subsequent loops.

I've tried adding an offset to the values like this:

   for(int i = 0; i < 3; i++) {
     fill_a(a);
     fill_b(b)
     fill_c(c);

     abc[(i*9)+0] = a[0];
     abc[(i*9)+1] = b[0];
     abc[(i*9)+2] = c[0];

and while that seem to work, if I try to add one by one that also doesn't work.

I've also tried adding offsets via an index value but it counts off to infinity.

int idx = 0;

void update() {

   for(int i = 0; i < 3; i++) {
     fill_a(a);
     fill_b(b)
     fill_c(c);

     abc[++idx] = a[0];
     abc[++idx] = b[0];
     abc[++idx] = c[0];

     abc[++idx] = a[1];
     abc[++idx] = b[1];
     abc[++idx] = c[1];

i've also tried filling the first arrays in one for loop. Then later in the update loop putting those values into the larger array.

int idx;

idx = 0;
void update() {

   for(int i = 0; i < 3; i++) {
     fill_a(a);
     fill_b(b)
     fill_c(c);
   }
  int tidx = idx;
  abc[++tidx] a[0];
  abc[++tidx] b[0];
  abc[++tidx] c[0];
  ....
  idx = tidx;
 }

but again the idx runs off to infinity. How can I arrange this algorithm so that I can fill the larger array inside that loop with the values from the smaller array while also keeping the offsets in order?

The values of the larger array is always going to be overwritten ever loop so after it's filled, it's consumed elsewhere then overwritten the next call to update.

edit I have tried gdb debugging stepping through the code one variable at a time.

The expected output is something like this:

a = { 0, 1, 2 };
b = { -3, -2, -1 };

abc = { 0, -3, 11.
        1, -2, 22 };

Here's a fully working example from actual code.

    int count = 3;
    float* v_buff;
    float* c_buff;

    size_t vs = sizeof(float) * 6);
    v_buff = calloc(1, (vs));


void update() {
     for(int i = 0; i < count; i++) {
         float a[3] = { 0, 1, 2}; //these will be new every update
         float b[3] = { -3, -2, -1};
         int idx = 0;
         v_buff[++idx] = a[0];
         v_buff[++idx] = a[1];
         v_buff[++idx] = a[2];

         v_buff[++idx] = b[0];
         v_buff[++idx] = b[1];
         v_buff[++idx] = b[2];
     }
}

That sample code compiles but the same thing applies. trying the offset:

v_buff[(i * 3) + (++idx)] = a[0];

causes my counter to run off to infinity or just overwrites the first set of values in v_buff.


回答1:


For your abc array, the first approach doesn't work because you set the first 3 entries of the array to the contents of a, then overwrite them with the contents of b, then again with the contents of c.

Your second approach is close but not quite because you're multiplying i by 9 instead of 3, so the index goes out of bounds after the first iteration.

Your third approach is also close but is issue there is that you're using a preincrement (++idx) instead of a postincrement (idx++) so your indexes go from 1 to 9 instead of 0 to 8.

Also, your update function is calling fill_a, fill_b, and fill_c in a loop. Without seeing those functions, I'm guessing that it's filling them with the same contents each time, so those probably don't need to be in a loop.

So assuming that the allocation and deallocation for each of the 4 arrays occurs outside of update, the function should look like this:

void update()
{
    int i;

    fill_a(a);
    fill_b(b)
    fill_c(c);

    for(int i = 0; i < 3; i++) {
        abc[(i*3)+0] = a[i];
        abc[(i*3)+1] = b[i];
        abc[(i*3)+2] = c[i];
    }
}

On the first iteration of the loop, i is 0, so the three lines evaluate to:

abc[0] = a[0];
abc[1] = b[0];
abc[2] = c[0];

On the second iteration, i is 1, and the body evaluates to:

abc[3] = a[1];
abc[4] = b[1];
abc[5] = c[1];

On the third iteration, i is 2, and the body evaluates to:

abc[6] = a[2];
abc[7] = b[2];
abc[8] = c[2];

You could also implement it like this:

void update()
{
    int i, idx;

    fill_a(a);
    fill_b(b)
    fill_c(c);

    for(int i = 0, idx = 0; i < 3; i++) {
        abc[idx++] = a[i];
        abc[idx++] = b[i];
        abc[idx++] = c[i];
    }
}


来源:https://stackoverflow.com/questions/34855447/filling-an-array-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!