Vous êtes sur la page 1sur 6

2/12/2015

ArrayLimitationsWhyLinkedList?SimplifiedCoding

SIMPLIFIED CODING

HOME

ABOUT

CONTACT

ARRAY LIMITATIONS WHY LINKED LIST?


Posted by Belal Khan in: array limitations c c++ data structures
In this post I will introduce you about the array limitations. In the
previous post I explained you about data structures. From this post
we are actually going to start learning data structures starting with
linked list data structure. But before going to linked list data
structure we need to learn why we need a linked list data structure
when we have an easy alternative array. To understand this first
we will be various array limitations.

The story about Array Limitations


I am going to tell you a story to understand these array limitations.
Let us say this is computer's memory. And each partition you can
see below is one byte of memory and each byte or partition is
having an address.

http://www.simplifiedcoding.net/2015/02/arraylimitationswhylinkedlist.html

1/10

2/12/2015

ArrayLimitationsWhyLinkedList?SimplifiedCoding

Now come back to our story. Memory is a crucial resource in


computer and all the application keep asking for memory. So Mr.
Computer has given this job of managing memory to one of his
component who is Memory Manager. Memory manager keep track
of what part of the memory is free and what part of the memory is
allocated. So anyone who needs memory need to talks to Memory
Manager.

So the Hero of our story is suppose Me :P. Ok so I am Belal is the


hero in this story. And I am developing an application and I need to
store some data. So I need to talk with the memory manager. I am
talking with the memory manager using C / C++. I said the memory
manager that I need to store an integer int x.

Memory manager said that ok, you need to store an integer


variable and I need to give you 4 bytes of memory (An integer take
4 bytes in a typical architecture). So the memory manager looks for
4 bytes of free space in the memory, lets assume that the memory
manager gave us the address 217 for our integer x. 217 is the
address of first byte of the block that is assigned for our integer x
http://www.simplifiedcoding.net/2015/02/arraylimitationswhylinkedlist.html

2/10

2/12/2015

ArrayLimitationsWhyLinkedList?SimplifiedCoding

because address of a block of memory is the address of the first


byte of that block of memory.

Memory manager now told me that I have assigned address 217


for your integer x. Now you can store your value over there.
Suppose I have stored value 8 in x.

Now I need to store a list of numbers. So I need to create an array


of integers. I thought the maximum number of integers in my list
would be 4. So I asked the memory manager for an integer array of
size 4 named A.
You know that array is always stored in memory as one contiguous
block of memory.
So memory manager says that, Ok I need to look at a free block of
memory of 16 bytes for this array A.
Memory manager allocates the memory from address 201 to 216
for my array A, which is an array of 4 integers (4 bytes x 4 bytes =
16 bytes).

http://www.simplifiedcoding.net/2015/02/arraylimitationswhylinkedlist.html

3/10

2/12/2015

ArrayLimitationsWhyLinkedList?SimplifiedCoding

Because array is a contiguous block of memory and memory


manager knows the starting address of the block, whenever my
application tries to access any element of the array the application
knows which element is to be accessed. Lets suppose my
application tries to access the last element of the array
i.e. A[3] = 2;
My application knows where to write the value because it knows
the base address of array A. And using the index and the base
address it calculates the address where the value has to be written.
i.e. 201 + (3 x 4) = 213
And thats why to access any of the element in the array the
application takes constant time.

And this is an awesome thing about array that irrespective of the


size of the array the application can access any of the element from
the array in constant time. So I completed my list by storing all the
numbers I needed.

But now I felt that I need to store one more value in my list. I had
declared an array of size 4, but now I need a 5th element, so I asked
to the memory manager that, Hey I want to extend my array A, is
http://www.simplifiedcoding.net/2015/02/arraylimitationswhylinkedlist.html

4/10

2/12/2015

ArrayLimitationsWhyLinkedList?SimplifiedCoding

it possible to do so?
The memory manager replies, Hey, when I allocate a block, I do
not expect that you will ask for an extension so I use whatever
PA: 1

mR: 0.00

0 links

DA: 10

memory available adjacent to that block for other variables. In

some cases I may extend the same block but in your case I have a
variable X already declared next to your array, so I cannot give you
an extension, this you can consider as one of array limitations,
sorry
This was one of the major array limitations that it cannot be
extended.
I ask the memory manager again, What all options do I have?.
Memory manager says, You have to create a new block and then
we will have to copy all the elements from the previous block into
the new block.
Now this time I declared a large size of array and copied all the
previous element and then stored my 5th element.

But, I felt bad that my list is too small and I am wasting some
memory, and if my still grow too large I may need to re-create a
new array. :(

Turn off hotspots.

This was the array limitations that made me find


an alternative
x Close
this tutorial.
that can sort out these array limitations.
I was desperately seeking a solution for this problem.

The Solution of the Array


Limitations
And finally I found the solution with a data structure named Linked
List.
http://www.simplifiedcoding.net/2015/02/arraylimitationswhylinkedlist.html

5/10

2/12/2015

ArrayLimitationsWhyLinkedList?SimplifiedCoding

But before going to know what the Linked List is point out the
array limitations which are:
Array Limitations
Fixed size and cannot be extended, this is one of major array
limitations that you cannot extend the size after creating it.
If we declare a large size of array and store only some
variables memory is getting wasted. This is also one of the
array limitations that if you don't use the whole block memory
is getting wasted.
Memory may not be available for a very large block of array
(This is the very rare case but this is also a possibility and one
of the array limitations).
Now for the above given array limitations the solution is to use
Linked List data structure. And this will be discussed in the next
post friends. Thank You :)

Like
Share

Tweet

6
1

1
7

Suggested Articles For You:

DataStructures
usingCIntroduct...

Howtowriteand
compileC/C++
Prog...

http://www.simplifiedcoding.net/2015/02/arraylimitationswhylinkedlist.html

6/10