C# 数据完整性问题

发布于 2024-07-16 23:08:16 字数 1587 浏览 6 评论 0原文

我的问题是下面的decodedProxyExcerpt2的分配覆盖了decodedProxyExcerpt1,我不知道为什么。

有什么线索吗?

提前致谢。

        DecodedProxyExcerpt decodedProxyExcerpt1 = new DecodedProxyExcerpt(stepSize);
        if (audiofactory.MoveNext(stepSize))
        {
            decodedProxyExcerpt1 = audiofactory.Current(stepSize);
        }
        // At this point decodedProxyExcerpt1.data contains the correct values.

        DecodedProxyExcerpt decodedProxyExcerpt2 = new DecodedProxyExcerpt(stepSize);
        if (audiofactory.MoveNext(stepSize))
        {
            decodedProxyExcerpt2 = audiofactory.Current(stepSize);
        }
        // At this point decodedProxyExcerpt2.data contains the correct values.
        // However, decodedProxyExcerpt1.data is overwritten and now holds the values of decodedProxyExcerpt2.data.


public class DecodedProxyExcerpt
{
    public short[] data { get; set; } // PCM data

    public DecodedProxyExcerpt(int size)
    {
        this.data = new short[size];
    }

}

来自音频工厂:

    public bool MoveNext(int stepSize)
    {
        if (index == -1)
        {
            index = 0;
            return (true);
        }
        else
        {
            index = index + stepSize;
            if (index >= buffer.Length - stepSize)
                return (false);
            else
                return (true);
        }
    }

    public DecodedProxyExcerpt Current(int stepSize)
    {
        Array.Copy(buffer, index, CurrentExcerpt.data, 0, stepSize);
        return(CurrentExcerpt);
    }}

my problem is that the assignment of decodedProxyExcerpt2 below overwrites decodedProxyExcerpt1 and I do not know why.

Any clues?

Thanks in advance.

        DecodedProxyExcerpt decodedProxyExcerpt1 = new DecodedProxyExcerpt(stepSize);
        if (audiofactory.MoveNext(stepSize))
        {
            decodedProxyExcerpt1 = audiofactory.Current(stepSize);
        }
        // At this point decodedProxyExcerpt1.data contains the correct values.

        DecodedProxyExcerpt decodedProxyExcerpt2 = new DecodedProxyExcerpt(stepSize);
        if (audiofactory.MoveNext(stepSize))
        {
            decodedProxyExcerpt2 = audiofactory.Current(stepSize);
        }
        // At this point decodedProxyExcerpt2.data contains the correct values.
        // However, decodedProxyExcerpt1.data is overwritten and now holds the values of decodedProxyExcerpt2.data.


public class DecodedProxyExcerpt
{
    public short[] data { get; set; } // PCM data

    public DecodedProxyExcerpt(int size)
    {
        this.data = new short[size];
    }

}

From AudioFactory:

    public bool MoveNext(int stepSize)
    {
        if (index == -1)
        {
            index = 0;
            return (true);
        }
        else
        {
            index = index + stepSize;
            if (index >= buffer.Length - stepSize)
                return (false);
            else
                return (true);
        }
    }

    public DecodedProxyExcerpt Current(int stepSize)
    {
        Array.Copy(buffer, index, CurrentExcerpt.data, 0, stepSize);
        return(CurrentExcerpt);
    }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仲春光 2024-07-23 23:08:17

我向一位朋友询问了这个问题,他给了我提示,我可能一直在考虑在 C++ 中,数组的赋值创建一个副本,而不是 C# 中,数组的赋值创建一个引用。

如果这是正确的并且

已解码ProxyExcerpt1 = audiofactory.Current(stepSize);

如果设置一个引用(而不是副本),那么覆盖是完全可以理解的。

I asked a friend about this who gave me the hint that I might have been thinking in C++ where assignment of an array creates a copy, instead of C# where assignment of an array creates a reference.

If that is correct and

decodedProxyExcerpt1 = audiofactory.Current(stepSize);

is setting a reference (not a copy) then the overwrite is completely understandable.

他不在意 2024-07-23 23:08:16

从表面上看,audiofactory.MoveNext(stepSize) 保持在相同的参考位置。 这导致 audiofactory.Current(stepSize) 停留在同一地址。

因此,decodedProxyExcerpt1decodedProxyExcerpt2 指向相同的引用,因此对一个引用的更改会传播到另一个。

因此,问题出在您的 AudioFactory 类中。

From the looks of it audiofactory.MoveNext(stepSize) is staying at the same reference. This is causing audiofactory.Current(stepSize) to stay at the same address.

For this reason, but decodedProxyExcerpt1 and decodedProxyExcerpt2 point to the same reference and therefore changes to one propagate to the other.

So, the problems lies in your AudioFactory class.

别想她 2024-07-23 23:08:16

类的实例作为引用存储。

解码代理Excerpt1和解码代理Excerpt2都是对同一对象的引用——audiofactory.CurrentExcerpt。

Instances of classes are stored as references.

decodedProxyExcerpt1 and decodedProxyExcerpt2 are both references to the same object -- audiofactory.CurrentExcerpt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文