tags:

views:

1319

answers:

4

Hi Everybody

I have a C program to check whether the machine stack is growing up or down in memory in.

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. :-)

Any one knows a solution without writing any native code ???

Thanks

+10  A: 

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).

Greg Hewgill
Just so you realize what he means - there's no rule that says it has to go up *or* down. If you wanted, you could store each call frame at a heap-allocated position and go all over the place. With some GC/JIT cooperation, you could even move the stack frames around while the program is running. *The ability to support this flexibility came by removing the programmer's ability to directly examine it.*
280Z28
"you could store each call frame at a heap-allocated position". I've worked with an implementation which did this. Actually it didn't make an allocation for every frame, it used up a chunk and then chained onto another. So in a simple test (if one were possible) it would have appeared to go down, but actually went down for a bit, then "sideways" to a new block, then down again. Reason was that the JVM was designed to work on architectures lacking virtual memory. So the only way to reserve a large contiguous range for stack would have been to allocate it all upfront for every thread.
Steve Jessop
... I don't know whether or not this strategy is common, since it's only that one implementation that I've ever had the lid off. But we certainly didn't fail any conformance tests, so you can't rule it out.
Steve Jessop
RISC OS (APCS-R) did that. It was mostly used for C.
Tom Hawtin - tackline
A: 

woah, you will not be able to get any usefull information out of such simple code in Java, least not that I know of.

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.

In Java you will be completely dependent on the JVM's implementation for addressing and as such will not be able to do this.

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.

There is also this page at IBM that can be of help.

This is pretty much all I have on the subject, I hope it will help you

Newtopian
+2  A: 

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 definition both on machines that use stack-up and stack-down.

Because of this it is not possible to know whether on a specific machine stack grows up or down from Java code.

Gregory Mostizky
+1  A: 

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 or struct. The standard says nothing about the direction of stack growth or whether a stack even exists.

sigjuice