简体   繁体   English

malloc的返回值是虚拟地址还是物理地址?

[英]Is the return value of malloc a virtual or physical address?

Is the address returned by malloc a virtual address or is it the actual physical address where the memory is allocated? malloc返回的地址是虚拟地址还是分配内存的实际物理地址?

Edit: 编辑:

I read somewhere "In some implementations, calling free() releases the memory back to the system and in others it is released back to the process". 我在某处读到“在某些实现中,调用free()会将内存释放回系统,而在其他实现中,它会被释放回进程”。 Does "releasing back to the system" imply that the memory is the actual physical memory and "releasing back to the process" mean it is virtual memory? “释放回系统”是否意味着内存是实际的物理内存并且“释放回进程”意味着它是虚拟内存?

It's an address valid in the current process. 这是在当前流程中有效的地址。 Whether it's virtual address or physical address depends on the underlying platform. 它的虚拟地址还是物理地址取决于底层平台。

There is no concept of real or virtual memory in the C standard so the question is meaningless. C标准中没有真实或虚拟内存的概念,因此问题毫无意义。 An implementation is free to do it however it wishes. 无论如何,实现都可以自由地执行。

In a virtual-memory OS, you will almost certainly get a virtual address. 在虚拟内存操作系统中,您几乎肯定会获得虚拟地址。 In a non-virtual-memory OS, you probably won't. 在非虚拟内存操作系统中,您可能不会。

What you will get in both cases is an address you can use for all the usual things C provides addresses for, such as de-referencing, freeing, reallocating and so forth. 在这两种情况下,您将获得的地址是您可以用于C提供地址的所有常用内容的地址,例如取消引用,释放,重新分配等等。 That is your only guarantee, and also the only thing you usually need to concern yourself with. 是您唯一的保证,也是您通常需要关注的唯一事项。

It's free to give you a list of sequential IDs (1, 2, 3, ...) if it wishes, provided all the expected operations still work as advertised. 如果所有预期的操作仍然像宣传的那样工作,它可以随意给你一个顺序ID列表(1,2,3,...)。 Granted that may not be very efficient in the current architectures but it's still workable. 当然,在当前的架构中可能效率不高,但它仍然可行。

The only reason I can think of that you would care about a physical memory address is if you're trying to talk directly to some piece of memory-mapped hardware... in which case you need something much more down-to-the-metal than malloc() . 我能想到你关心物理内存地址的唯一原因是,如果你试图直接与某些内存映射硬件对话......在这种情况下你需要更多的内容金属比malloc() You'd have to use a kernel or driver interface, or barring that, go behind the operating system's back using something like UNIX's /dev/mem -- or, better, write a driver yourself to map the needed physical memory into your application's virtual memory. 您必须使用内核或驱动程序接口,或者禁止使用UNIX的/dev/mem等操作系统的后面 - 或者更好的是,自己编写驱动程序以将所需的物理内存映射到应用程序的虚拟内容中记忆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM