Are all local variables indexed consecutively?
Dear All,
In JVM, each frame contains an array of variables addressed by indexing. I am wondering if all local variables must be stored consecutively in the array.
For example, is it possible for this scenario to happen:
The length of the local variable array is 10, and a compiler translates 3 local variables of type Object to be located at index 3, 5, and 8 respectively (assuming no parameters in their enclosing method)?
The reason I need to know this is because I am using ASM to transform bytecode and need to introduce my own local variables. If all local variables' indices must be consecutive, I can get the max. number of locals (3 in the above example) of the original method and give my new locals indices starting from 4, ... But if they are not consecutively indexed, I have to use other ways.
Your help is much appreciated,
-- Sunny
Hi,In general, they are indexed consectutively... but with 2 major exceptions.Any Long or Double variable takes up 2 places in the variable table.The same is true of longs & doubles in the classes constant pool.regards,Owen
Hi Owen,
Thanks for your reply.
Yes I am aware that Long and Double types take 2 positions in the array of local variables.
Therefor my other question is: does max_locals represent the length of the local variable array, or does it mean the number of local variables that are actually exist in the array? Thanks again.
-- Sunny
> Therefor my other question is: does max_locals
> represent the length of the local variable array, or
> does it mean the number of local variables that are
> actually exist in the array? Thanks again.
It gives the number of local variables in the local variable array but long and double are each considered to reserve two local variables and contribute two units toward the max_locals value.
YoGeea at 2007-7-14 22:10:07 >
