堆/缓冲区溢出异常 [英] Heap / buffer overflow exception

查看:133
本文介绍了堆/缓冲区溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很好奇,有没有人遇到C#中的堆/缓冲区溢出异常?

Just curious, Is there or has anyone ever come across a heap / buffer overflow exception in C#?

推荐答案

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

You can cause a buffer overflow in C# in unsafe code. For example:

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,因为它溢出了缓冲区。

The above will print "99" because it overflowed the buffer.

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

Absent unsafe code, I do not know of any way to cause a buffer overrun that doesn't trigger an exception.

这篇关于堆/缓冲区溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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