c语言sizeof malloc,C语言中的sizeof和malloc

malloc分配的内存都是连续的么?

`

简短回答:是的(连续的虚拟内存,但无法保证是连续的物理内存)

malloc will fail if there isn’t a large enough contiguous block available. (A failure with malloc will return a NULL pointer)

malloc() doesn’t know or care about physical memory. No user-space program does. Only the operating system kernel knows the mapping between virtual and physical pages.

As a user-space program you have no control over physical pages. If a user-space program requests a 1GB allocation from the operating system, the result will be 1GB of contiguous virtual memory, but the individual physical pages used to satisfy that request have no requirement whatsoever. They can be a random patchwork of pages from all over the place. User-space does not know or care. This is the case regardless of the scenario, so in your example there was no guarantee that your initial eight allocations used contiguous physical pages, which it almost certainly did not.

`

https://stackoverflow.com/questions/625270/does-malloc-allocate-a-contiguous-block-of-memory

https://linux.die.net/man/3/malloc

https://www.reddit.com/r/C_Programming/comments/4t1qvz/does_malloc_allocate_contiguous_physical_memory/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值