代码之家  ›  专栏  ›  技术社区  ›  Viv

堆/缓冲区溢出异常

  •  4
  • Viv  · 技术社区  · 14 年前

    只是好奇,是否有人或曾经有人遇到过 堆/缓冲区溢出 C中的异常

    2 回复  |  直到 14 年前
        1
  •  8
  •   Jim Mischel    14 年前

    在不安全的代码中,C中可能会导致缓冲区溢出。例如:

    public unsafe struct testo
    {
        public int before;
        public fixed int items[16];
        public int after;
    }
    
    testo x = new testo();
    x.after = 1;
    for (int i = 0; i <= 16; ++i)
    {
        unsafe
        {
            x.items[i] = 99;
         }
    }
    Console.WriteLine(x.after);
    

    上面将打印“99”,因为它溢出了缓冲区。

    如果没有不安全的代码,我不知道有什么方法可以导致不会触发异常的缓冲区溢出。

        2
  •  0
  •   Aurojit Panda    14 年前

    根据缓冲区溢出的含义,indexoutofrangeexception是由溢出引起的异常。通过访问超出其分配大小的数组索引,您可以很容易地获得它。同样,做足够的递归,您可以得到stackoverflowException。我不知道你在找什么,所以你可能想澄清一下。