在反C的memcpy [英] C memcpy in reverse

查看:415
本文介绍了在反C的memcpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与音频数据的工作。我想在相反的方向播放样本文件。数据存储为无符号整数,包装好和紧张。有没有一种方法来调用的memcpy 将在相反的顺序复制。也就是说,如果我有存储在一个数组1,2,3,4,我可以打电话给的memcpy 神奇的逆转他们,所以我得到4,3 ,2,1。

I am working with audio data. I'd like to play the sample file in reverse. The data is stored as unsigned ints and packed nice and tight. Is there a way to call memcpy that will copy in reverse order. i.e. if I had 1,2,3,4 stored in an array, could I call memcpy and magically reverse them so I get 4,3,2,1.

推荐答案

这适用于复制 INT S IN相反:

This works for copying ints in reverse:

void reverse_intcpy(int *restrict dst, const int *restrict src, size_t n)
{
    size_t i;

    for (i=0; i < n; ++i)
        dst[n-1-i] = src[i];

}

就像的memcpy(),各地区指出,由 DST SRC 不得重叠。

Just like memcpy(), the regions pointed-to by dst and src must not overlap.

如果你想扭转就地:

void reverse_ints(int *data, size_t n)
{
    size_t i;

    for (i=0; i < n/2; ++i) {
        int tmp = data[i];
        data[i] = data[n - 1 - i];
        data[n - 1 - i] = tmp;
    }
}

上述

两者的功能是便携式的。您可能能够使用特定硬件的code,使他们更快。

Both the functions above are portable. You might be able to make them faster by using hardware-specific code.

(我没有测试code的正确性。)

(I haven't tested the code for correctness.)

这篇关于在反C的memcpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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