中填充阵列中的环 [英] filling an array in a loop

查看:165
本文介绍了中填充阵列中的环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是不断被调用的函数。我有3个更小的数组,我需要打包成一个更大的阵列中的每个环

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

我也试过在一个for循环填充第一阵列。再后来在更新循环把这些值转换成更大的阵列。

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

但再次IDX逃跑到无穷远​​。我该如何安排这个算法,使我可以填补从较小的阵列,同时也保持了偏移,以?的值是循环内的较大的阵列

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.

修改
我试图gdb调试通过code,一次踩着一个变量。

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

预期的输出是这样的:

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

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

下面是实际code完全工作的例子。

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];
     }
}

这样code编译,但同样适用。
试图偏移:

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

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

导致我背道而驰关到无穷大或只是覆盖第一组值在v_buff。

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

推荐答案

有关你的 ABC 阵列,第一种方法不起作用,因为你设置的前3个表数组 A 的内容,然后用的内容b 再次的内容覆盖它们,那么 C

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.

您第二个方法是接近,但并不完全因为你乘 I 9,而不是3,所以该指数第一次迭代后超出范围。

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.

您第三种方法也接近,但存在的问题是,您使用的是preincrement( ++ IDX ),而不是后增( IDX ++ ),使您的指标去从1到9,而不是0-8。

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.

另外,你的更新函数调用 fill_a fill_b fill_c 在一个循环。如果没有看到这些功能,我猜,它的每一次填充它们具有相同的内容,所以那些可能并不需要在一个循环。

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.

所以,假设每个4阵列的分配和释放发生更新之外,功能应该是这样的:

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];
    }
}

在循环的第一次迭代, I 0,因此三线计算为:

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

在第二次迭代, I 1,身体的计算结果为:

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

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

在第三次迭代, I 2,身体计算结果为:

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

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

您也可以实现这样的:

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];
    }
}

这篇关于中填充阵列中的环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆