尝试分配通用数组时,C#程序崩溃

时间:2011-06-17 08:19:58

标签: c# .net arrays stack-overflow allocation

请考虑以下代码:

    public class LimitedBuffer<T> {

    /// <summary>
    /// 
    /// </summary>
    public const int DefaultSize = 10;

    /// <summary>
    /// 
    /// </summary>
    private T[] buffer;

    /// <summary>
    /// 
    /// </summary>
    private int poe;

    /// <summary>
    /// 
    /// </summary>
    private int pow;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="size"></param>
    public LimitedBuffer(int size = 10) {
        // Buffer instantiation
        this.buffer = new T[10];
        // Value initialization
        for (int i = 0; i < this.buffer.Length; i++)
            this.buffer[i] = default(T);
        this.pow = 0;
        this.poe = size - 1;
    }

    /// <summary>
    /// 
    /// </summary>
    private void ShiftCursors() {
        if (this.pow == this.buffer.Length - 1) {
            this.pow = 0;
            this.poe = this.buffer.Length - 1;
        } else
            this.poe = this.pow++;
    }

    /// <summary>
    /// 
    /// </summary>
    public T Value {

        get {
            return this.buffer[this.poe];
        }

        set {
            this.buffer[this.pow] = value;
            this.ShiftCursors();
        }

    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override string ToString() {

        string ret = "LimitedBuffer = { ";

        foreach (T el in this.buffer) {
            ret += this.ToString() + "; ";
        }

        return ret + "}";

    }

} /* LimitedQueue */
class Program {
    static void Main(string[] args) {
        LimitedBuffer<float> lb = new LimitedBuffer<float>(4);
        lb.Value = 1.0f;
        lb.ToString();
        lb.Value = 2.0f;
        lb.ToString();
        lb.Value = 3.0f;
        lb.ToString();
        lb.Value = 4.0f;
        lb.ToString();
        lb.Value = 5.0f;
        lb.ToString();
        lb.Value = 6.0f;
        lb.ToString();
        lb.Value = 7.0f;
        lb.ToString();
        lb.Value = 8.0f;
        lb.ToString();
        lb.Value = 9.0f;
        lb.ToString();

        System.Threading.Thread.Sleep(20000);
    }

好吧,在我使用“HERE”概述的那一点上,程序就会死掉。

我确信因为,运行调试器,当它到达该行并处理它时,在处理该行之后一切都崩溃了......我真的不知道为什么!

你能帮我吗?

三江源

2 个答案:

答案 0 :(得分:7)

错误在您的ToString方法中。你在其中调用this.ToString导致无限递归和StackOverflowException

public override string ToString()
{

    string ret = "LimitedBuffer = { ";

    foreach (T el in this.buffer)
    {
        ret += this.ToString() + "; ";
    }

    return ret + "}";

}

你可能想要:

   public override string ToString()
    {

        string ret = "LimitedBuffer = { ";

        foreach (T el in this.buffer)
        {
            ret += el.ToString() + "; ";
        }

        return ret + "}";

    }

答案 1 :(得分:1)

问题是你以递归方式调用ToString

public override string ToString()
{
    string ret = "LimitedBuffer = { ";
    foreach (T el in this.buffer)
    {
        ret += this.ToString() + "; ";
    }
    return ret + "}";
}