• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Low Level Knowledge of Arrays

 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All we have been told as beginners to Java programming was that access to array elements must be from 0 to n-1 or else we will be served with an exception at runtime... but the question I asked myself was why

To my discovery, the reasoning behind this has to do with memory management...

Lets say we declare and initialize an array of chars:


This statement causes the runtime system to request for memory which might be placed at hypothetical locations 0x0000001A 0x0000001C 0x0000001E... the locations increment by 2 since each char should contain two bytes

This can be conceptually visualized as:
data element 1 location = 0x0000001A
data element 2 location = 0x0000001C
data element 3 location = 0x0000001E

To access the locations in memory the system uses offsets, so to access the first element the system performs the following equation to find the requested element: data[0] == 0x0000001A + (0 * 2) where 0 is the offset value and 2 is how much bytes the element contain in memory

To find the second element the equation will be: data[1] == 0x0000001A + (1 * 2) == 0x0000001C
To find the third element the equation will be: data[2] == 0x0000001A + (2 * 2) == 0x0000001E

From this it can be observed that if you perform the following requests it goes out of the designated region for data's elements where other variables data may be located, which if you were allowed to access through data will cause unpredictable results within the application:
data[-1] == 0x0000001A + (-1 * 2) == 0x00000018
data[4] == 0x0000001A + (4 * 2) == 0x00000023

Therefore an exception is thrown to protect unwanted results and hard to find bugs...
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The real reason is that it is the same as in C++. And C++ does it that way because C does it that way.

So we are off to the C/C++ forum.

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now we're in the C forum:

In C an array and a pointer to its first element are the same thing. Let's have an example.
In C there ain't no such thing as a String. There are only arrays of chars (which are different from Java® chars in that they only occupy 8 bits).
So if you have the array R-i-c-o-[null] (yes, it has 5 elements, the last being the null character), the first element, the R might be at memory location 0x0000001A. You can express that as name[0] or *(name) or *(name + 0). If you want the i, you have to go to *(name^nbsp;+ 1) or name[1]. The whole array is at the same memory location as its first element.

So you see the index is how many memory locations you have to move from the start of the array to find the element you want. In an n‑element array, you can only moven n − 1 places before you run into different memory.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't (as far as I know) get exceptions in C/C++ if you overrun the bounds of an array. You can get nasty errors or security vulnerabilities, however.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this same logic happen below the abstraction that Java has provided to provide bounds-checking at runtime?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rico Felix wrote:Does this same logic happen below the abstraction that Java has provided to provide bounds-checking at runtime?


You'd have to read the Java Language Specification to find out if the way that arrays are implemented is specified.
I suspect that many JVM implementations will use that logic, but unless it says in the JLS that that is how it has to be done, there is no guarantee that all JVMs will implement it that way.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not certain but I don't remember the JLS specifying a particular method for bounds checking. So you would have to assume it varied from implementation to implementation.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rico Felix wrote:All we have been told as beginners to Java programming was that access to array elements must be from 0 to n-1 or else we will be served with an exception at runtime... but the question I asked myself was why
To my discovery, the reasoning behind this has to do with memory management...


That's very likely the reason why C did it but, as Campbell says, the reason why Java did it was more probably because that's how C/C++ did.

And actually, once you get over the "learning hump", you'll find that having indexes start from 0 is really useful. If you start them from 1, you'll find there's all sorts of cases and calculations where you have to "remove" or "add" that 1 unnecessarily.

And for storing things like, say, month names, where "month" is a 1-based "index" - ie, 1-12 - there's nothing that says you have to use the first element of an array. Of course, then it'll have 13 elements in it, which might look a bit odd.

Winston
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
And actually, once you get over the "learning hump", you'll find that having indexes start from 0 is really useful. If you start them from 1, you'll find there's all sorts of cases and calculations where you have to "remove" or "add" that 1 unnecessarily.

And for storing things like, say, month names, where "month" is a 1-based "index" - ie, 1-12 - there's nothing that says you have to use the first element of an array. Of course, then it'll have 13 elements in it, which might look a bit odd.



Having worked in Pascal (for a few years) prior to working in C, I disliked that the C indexes always started from 0. Pascal allowed you to choose the range -- meaning the low and the high index. And IMO, that feels even more useful...

Henry
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you start using things like the % operator together with array indices, then it is much easier to start at 0.Going from Java® to RVM_FORTH which has 1‑based arrays, is awkward, I can assure you. And that is where the strange behaviour of ++ can come in useful, particularly if you can understand code like
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . Of course, then it'll have 13 elements in it, which might look a bit odd.

Winston

Anybody feeling superstitious can get a 14‑element array by using this.

Or maybe you still have thirteen
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic