首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何声明循环缓冲区?

如何声明循环缓冲区?
EN

Stack Overflow用户
提问于 2014-07-06 10:36:13
回答 2查看 309关注 0票数 0
代码语言:javascript
复制
/**
 * ring_buffer_set - update the ring buffer with the given value
 * @lq_recv: pointer to the ring buffer
 * @lq_index: index to store the value at
 * @value: value to store in the ring buffer
 */
static void ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
                   uint8_t value)
{
    lq_recv[*lq_index] = value;
    *lq_index = (*lq_index + 1) % 64;
}

/**
 * ring_buffer_set - compute the average of all non-zero values stored
 * in the given ring buffer
 * @lq_recv: pointer to the ring buffer
 *
 * Returns computed average value.
 */
static uint8_t ring_buffer_avg(const uint8_t lq_recv[])
{
    const uint8_t *ptr;
    uint16_t count = 0, i = 0, sum = 0;

    ptr = lq_recv;

    while (i < 64) {
        if (*ptr != 0) {
            count++;
            sum += *ptr;
        }

        i++;
        ptr++;
    }

    if (count == 0)
        return 0;

    return (uint8_t)(sum / count);
}

1) lq_recv[]是循环缓冲区,但它看起来像一个普通数组?

2) *lq_index = (*lq_index + 1) % 64的用途是什么

EN

回答 2

Stack Overflow用户

发布于 2014-07-06 10:38:17

  1. “循环缓冲区”是以特定方式使用的数组。它的声明与任何其他数组没有任何不同。
  2. 它更新读取的数组的索引,并将其保持在数组的边界内。
票数 5
EN

Stack Overflow用户

发布于 2014-07-06 11:00:37

  1. 循环缓冲区当然是一个普通数组,但是当数组索引器到达最后一个元素时,需要重置数组索引器。
  2. 使用模运算符(%)来确保数组索引在到达最后一个元素时被重置为0。
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24592228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档