Hi,
I am creating a new programming language, basically a simplified Java.
I am wondering how Java keeps track of the size of arrays. Does create add a hidden "arraySize" which would double the size of the array pointer (eg 4 bytes for pointer, 4 bytes for "size")?
Also does Java do bounds checking on arrays at run time? Would this reduce performance a noticable amount?
If there are any relevant links please let me know!
Compiler question
-
Nav
RE:Compiler question
Actually in java array can be enlarged during run time and it will take memory from the heap part like c and c++ do in malloc() and new operations
-
carbonBased
RE:Compiler question
Uhm... a _Vector_ can be resized at run-time... an array, however, cannot be... at least as far as I remember (granted, it's been a while since I did any java, but this is pretty consistant umong _any_ language).
As for Java's bounds checking, yeah, I'm pretty sure it's done at run-time. It would pretty much have to. There's way to much strain on the compiler to check for _every_ possible attempt on an array.
And yes, run-time checking will cause speed degredation. I doubt much on current hardware, but, as with most compilers, and I'm sure Java as well (like I said... it's been a while), you can probably turn those features off.
If you're developing your own language, I'd suggest you use run-time checking as an option... mostly for testing. And then, once finished and fully tested, compile the final program without these run-time checks. Must like C with its assert or, even better, Eiffel's design-by-contract.
Where to put the array length is beyond me; I have no idea where they put it. It would make sense to me to use the first 4 bytes to hold the array length, and return only the first available entry to the user. Even if the user tries something devious like "myArray[-1] += 50" you'll be able to catch it in your bounds checking code.
Cheers,
Jeff
As for Java's bounds checking, yeah, I'm pretty sure it's done at run-time. It would pretty much have to. There's way to much strain on the compiler to check for _every_ possible attempt on an array.
And yes, run-time checking will cause speed degredation. I doubt much on current hardware, but, as with most compilers, and I'm sure Java as well (like I said... it's been a while), you can probably turn those features off.
If you're developing your own language, I'd suggest you use run-time checking as an option... mostly for testing. And then, once finished and fully tested, compile the final program without these run-time checks. Must like C with its assert or, even better, Eiffel's design-by-contract.
Where to put the array length is beyond me; I have no idea where they put it. It would make sense to me to use the first 4 bytes to hold the array length, and return only the first available entry to the user. Even if the user tries something devious like "myArray[-1] += 50" you'll be able to catch it in your bounds checking code.
Cheers,
Jeff