简体   繁体   English

程序地址范围

[英]Program address range

When I write a simple program using C / C++ , what's the address range I get? 当我使用C / C ++编写一个简单的程序时,我得到的地址范围是多少? I mean, I can point to wherever I want.. like : 我的意思是,我可以指向我想要的任何地方。

void* ptr = (int*)0xFFFFFFFF;

Where does that pointer actually point to? 该指针实际指向何处? I guess its not the real address in the main memory but just the "cover" address of my program. 我猜它不是主存储器中的实际地址,而只是我程序的“覆盖”地址。

Can anyone explain that to me? 谁能向我解释? What's the address range I get (in windows for example) when running my own C program? 运行自己的C程序时,我得到的地址范围是多少(例如,在Windows中)? Can I really access other program's address range if I want to? 如果愿意,我真的可以访问其他程序的地址范围吗?

Thanks! 谢谢!

Your program runs in a virtual address space, and pointers point to locations within virtual memory. 您的程序在虚拟地址空间中运行,并且指针指向虚拟内存中的位置。 So no, you cannot expect to conjure up a pointer with the same numeric value as one in another program and have them both point to the same actual memory. 因此,不能,您不能期望将一个指针与另一个程序中的指针具有相同的数值,并使它们都指向相同的实际内存。 On the contrary, if you do that your program will likely crash or otherwise misbehave (but beware: it's undefined behavior, so anything could happen and it's platform-dependent). 相反,如果这样做,您的程序很可能会崩溃或行为异常(但请注意:这是未定义的行为,因此任何事情都可能发生,并且取决于平台)。

You CAN'T access another program's memory. 您无法访问其他程序的内存。 The OS keeps them separate. 操作系统将它们分开。 Only possible way is through some formal mechanism like shared memory or some kind of physical memory mapping. 唯一可行的方法是通过某种形式化的机制,例如共享内存或某种物理内存映射。

有关Windows操作系统中虚拟地址空间的更多信息: http : //www.tenouk.com/WinVirtualAddressSpace.html

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

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