java 您如何确定机器的堆栈在内存中是增大还是减小?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1163977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would you find out if a machine’s stack grows up or down in memory? (JAVA)
提问by Roman
I have a C program to check whether the machine stack is growing up or down in memory in.
我有一个 C 程序来检查机器堆栈是在内存中增大还是减小。
It goes like that :
它是这样的:
#include <stdio .h>
void sub(int *a) {
int b;
if (&b > a) {
printf("Stack grows up.");
} else {
printf("Stack grows down.");
}
}
main () {
int a;
sub(&a);
}
Now i want to do the same in Java. :-)
现在我想在 Java 中做同样的事情。:-)
Any one knows a solution without writing any native code ???
任何人都知道不编写任何本机代码的解决方案???
Thanks
谢谢
回答by Greg Hewgill
If you're not doing any native code, then I can't imagine a situation where it could possibly matter in pure Java code. After all, the Java stack might be allocated in any direction at all instead of being a strictly contiguous block of memory (like the machine stack).
如果您没有编写任何本机代码,那么我无法想象在纯 Java 代码中它可能很重要的情况。毕竟,Java 堆栈可以在任何方向分配,而不是严格连续的内存块(如机器堆栈)。
回答by Gregory Mostizky
Java source code compiles to Java byte-code which is an assembly like language that runs on the JVM. JVM is a virtual machine and so it will look exactly the same by definitionboth on machines that use stack-up and stack-down.
Java 源代码编译为 Java 字节码,Java 字节码是一种在 JVM 上运行的类似程序集的语言。JVM 是一个虚拟机,因此根据定义,它在使用堆栈向上和堆栈向下的机器上看起来完全相同。
Because of this it is not possible to know whether on a specific machine stack grows up or down from Java code.
因此,不可能知道特定机器上的堆栈是从 Java 代码增长还是下降。
回答by sigjuice
This cannot be done in Java code. It cannot be done in C code either. The code you posted invokes undefined behavior (&b > a). According to the standard, the result of comparing two pointers is undefined unless the pointers point to elements within the same array. The standard says nothing about the direction of stack growth or whether a stack even exists.
这不能在 Java 代码中完成。它也不能用 C 代码完成。您发布的代码调用了未定义的行为(&b > a)。根据标准,除非指针指向同一数组中的元素,否则比较两个指针的结果是未定义的。该标准没有说明堆栈增长的方向或堆栈是否存在。
回答by Newtopian
woah, you will not be able to get any usefull information out of such simple code in Java, least not that I know of.
哇,您将无法从 Java 中这样简单的代码中获得任何有用的信息,至少不是我所知道的。
The code you have makes a lot of assumptions that, even in C actually, may or may not be true. It will depend on the platform and OS that is running your program.
你的代码做了很多假设,即使在 C 中,实际上也可能是也可能不是。这将取决于运行您的程序的平台和操作系统。
In Java you will be completely dependent on the JVM's implementation for addressing and as such will not be able to do this.
在 Java 中,您将完全依赖于 JVM 的寻址实现,因此将无法做到这一点。
My first answer would be to use a profiler. You can also create your own profiling agent using the API provided (JVMTI)for this purpose. It is a lot more complex certainly than your approach but you shouldbe able to get what you need.
我的第一个答案是使用分析器。为此,您还可以使用提供的API (JVMTI)创建自己的分析代理。它肯定比您的方法复杂得多,但您应该能够获得所需的东西。
There is also this pageat IBM that can be of help.
IBM 的这个页面也可以提供帮助。
This is pretty much all I have on the subject, I hope it will help you
这几乎是我关于这个主题的全部内容,我希望它会帮助你

