Hi all, I hope everyone of you out there are fine. Lets talk about array.
Array is used everywhere, in every language. It is easy and gives you access to a particular element in less time over linked lists. Yeah, since array elements are always indexed, you can access any of them without knocking other elements.
Now, straight to code. I have used C codes, for your better understanding of the topic (I hope so). An array is a data form that can hold several values all of one type.
To create an array, you need to indicate 3 things -
1. The type of value to be stored in each element. For example, int, char, float and even a struct.
2. The name of the array.
3. The number of the elements in the array. (We can initialize array at run-time, instead of compilation time. Hold on, we will discuss that in later posts.)
int months[12]; is an array. Here, 'int' is the type of the each element of the array 'months'. And the array 'months' can hold 12 values.
Now how to initialize that. You can initialize an array while defining in the following manner -
int months[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Even,
int months[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Compiler will count for you. But, remember you can never use like -
int months[12];
months[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
The most important thing about the array indexing is, it always starts from zero. Not from 1 or any other number. For the example I am using here, months[12] is an array of 12 elements, ranging from months[0] to months[11]. Be careful on this. Whenever you try to access a memory location in your code, the compiler will not warn you about that. Just crashed when executing, if you are not lucky enough. So, here you actually can not access months[12], anyway. It is out of the memory location you have defined for the array.
What compiler does when you are declaring an array
int months[12];
is allocating a sequence of memory location for 12 'int' type of data. First memory location is pointed by months[0] and the last memory location for data is pointed by months[11]. So, what will happen if you try to get the value at location pointed by months[12], which you have not defined for the application. It will try to access the next memory location after months[11] and will show the garbage value(the meaningless value) at that location. And, if the location is inaccessible to the application it will crash.
Let me allow, to show you the internal memory management on this matter. Consider, compiler allocates memory location 1000 to 1047 for the array int months[12]. Here, each memory location can hold one byte data. Now, 'int' needs 4 bytes of memory, and for 12 'int' it requires 48 bytes of memory. Simple enough...
In this way, months[0] uses memory location 1000 to 1003 and months[11] uses memory location 1044 to 1047. I hope you get clear everything so far. So, in next when you call for months[12] it tries to access the memory location 1048 to 1051. If, the OS allows the application to access that chunk of memory location, you get some garbage; otherwise get hanged.
If you are reading this line, I must appreciate your patients. The doc is too technical, I know. I am sorry about that. I shall try to write in less technical way, to make you understand the technical things. Have a nice coding time.
Saturday, December 4, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment