且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C : malloc 似乎分配的比我请求的还多(数组)

更新时间:2023-02-09 14:53:57

调用 malloc 时,可能会使用额外的空间来存储有关已分配块的元数据,例如其大小和其他已分配块的信息.有时,实现更喜欢以特定间隔(例如,4 或 8 个字节的倍数)分配块.但是,每次调用 malloc 时,您不应该依赖于一致的空间量;仅使用您特别要求的内容,否则您可能会覆盖其他重要信息.

When calling malloc, additional space may used to store metadata about the allocated block, such as its size and information about other allocated blocks. Sometimes the implementation prefers allocated blocks to be at specific intervals (e.g., multiples of 4 or 8 bytes). However, you shouldn't depend on the amount of space being consistent each time you call malloc; use only what you have specifically requested or you may be overwriting other important information.

有关详细信息,请参阅以下问题的答案:

See the answers to the following questions for more information: