gpt4 book ai didi

c# - Java 和 C# 中的内存管理有何不同?

转载 作者:搜寻专家 更新时间:2023-11-01 01:38:56 24 4
gpt4 key购买 nike

我正在通读 2010 CWE/SANS Top 25 Most Dangerous Programming Errors其中一个条目是 Buffer Copy without Checking Size of Input .它建议使用具有功能的语言来防止或减轻此问题,并说:

For example, many languages that perform their own memory management, such as Java and Perl, are not subject to buffer overflows. Other languages, such as Ada and C#, typically provide overflow protection, but the protection can be disabled by the programmer.

我不知道 Java 和 C# 在内存管理方面存在任何有意义的差异。为什么 Java 不会发生缓冲区溢出,而 C# 只能防止溢出?以及如何在 C# 中禁用此保护?

最佳答案

java不支持裸指针(严格来说不支持指针运算)

在 C# 中,您可以使用 unsafe code and pointers和非托管内存,这使得缓冲区溢出成为可能。参见 unsafe关键词。

To maintain type safety and security, C# does not support pointer arithmetic, by default. However, by using the unsafe keyword, you can define an unsafe context in which pointers can be used. For more information about pointers, see the topic Pointer types.

关于c# - Java 和 C# 中的内存管理有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2459712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com