I had an interview for a job, and one question they asked was how to do something in a language I’m comfortable with.

So I told them what language would they like for me to give them a solution, I said I can do it in Java, PHP, C/C++, …, and they said that it’s not a problem, they understand everything, doesn’t mater which language it is written in, if you want to know the difference read bellow in the PHP section.

The job I was applying was for a scripting language Perl (the developers that interviewed me had experience with Perl), so in this case they said I can do it in PHP because its a scripting language too.

They wanted me to remove an item from an array, and to tell them how would I do it in the said language.

The problem is that they don’t understand the representation of array in PHP is completely different than any other language, but we will get into that in a little while.

Before we proceed lets see a little about the array data structure, and the internal workings of the array.

Array Data Structure

Arrays is a basic data structure representing a group of similar elements, accessed by an index, it can be stored effectively and accessed quite fast to the all of the elements.

An array doesn’t have an overhead per element. and any element of the array can be accessed at O(1) time by it’s index.

On the other hand, the array structure is not dynamic. Most programming languages provide a way to allocate arrays with arbitrary size (dynamically allocated arrays), the problem with that is when the space is used up, a new array will have to be generated and the old data copied into it.

The insertion and deletion of an element in an array requires to shift O(n) elements on average.

Static and dynamically allocated arrays

There are two types of arrays, and both of them are implemented differently.

Static array has a constant size and they are defined and present any time during runtime of the application.

But the dynamic array is created during runtime and may be deleted, when it’s not needed any more, and they can be quite big, even bigger than the amount of physical memory, even though they are dynamic they can’t be resized. But you can expand it by:

Creating a new array of bigger size

Copying data from old one to new one

Free the memory of the old one

Dynamic arrays

The problem when working with an array data structure is that its size cannot be changed during program run.

Internal representation

So how can we do this then ?

The application allocates memory and divides it into two parts, one part contains the data and the other one contains the free space.

In the beginning all space is free space. As you add items to the array, it will fill up, and when it has no space it will expand by creating a new larger array and copying the old contents to the new location.

storage: dynamically allocated space to store data

capacity: size of the storage

size: real size of the data

If you are worried about speed don’t be because copying the arrays is done on the hardware level and can be done very efficiently.

PHP

First of all, the arrays in PHP are not like traditional arrays, and everyone who comes to PHP or goes from PHP to other languages, are confused because arrays in PHP don’t work as expected, especially for people coming from Javascript, Java, Perl, C/C++, C#.

If you look at the syntax it is similar to other languages, so let’s take a look at a simple implementation of an array in PHP.

Why? Because it’s not an array, it’s an ordered map, which is something I pointed out to the interviewers, so I proceeded to explain to them how this works, as I was asked to do so in this language, so I should explain properly how this is handled in the language in question, and was met with skepticism, and doubt.

Just a simple explanation, for those of you that don’t know C, you see the union there, that basically means that this is not actually a structure, but a single type.

In C, variables are just labels for raw memory address.

As you can see in the data structures above, we keep track of what primitive type is the value.

I won’t delve deep into explaining this, it will take quite a while to do so, and maybe I will do so in a future article.

If we actually have this code

<?php
$a = 1;
$b =& $a;
unset($a);

In this case $a will be unset, but $b will still remain the same with the value 1.

Now let’s see this too

<?php
$a = 1;
$b = &$a;
$a = 2;
echo "$a = $b";

In the previous case when you unset, the value actually remains in $b, but in this case, if we change $a, $b will also change.

Well we strayed quite a bit from arrays, so let’s finish it here for PHP.

Java

In the heap (not on the stack), the array is a block of memory made of all the elements together, each element in the arrays is identified by a zero based index [0,1,2,3,….].

There are ways of putting objects and arrays onto the stack instead, it’s called Escape analysis, you can read some info on this link

The Java language does not offer any way to explicitly allocate an object on the stack, but this fact doesn’t prevent JVMs from still using stack allocation where appropriate. JVMs can use a technique called escape analysis, by which they can tell that certain objects remain confined to a single thread for their entire lifetime, and that lifetime is bounded by the lifetime of a given stack frame. Such objects can be safely allocated on the stack instead of the heap. Even better, for small objects, the JVM can optimize away the allocation entirely and simply hoist the object’s fields into registers.

The length of an array in Java cannot be changed if created with new, let’s take a look at this example.

type arr[]; // declare the array
arr = new type[10]; // will initialize the array to 10 elements, and store the pointer

type can be a primitive or an object.

Lets see the previous example in PHP, with primitives without using Collections, or Lists.

If you want to remove an array, you need to follow create a new array and copy it into the old one, while removing the element you don’t want.

You can use System.copyarray to do so

C

Arrays are allocated as a fixed number of contiguous elements, and there is no way to actually remove the memory used by an individual element in the array, but the elements can be shifted to fill in the hole made by the removal of the element.

As you know from above that statical arrays can not be resized, but dynamically allocated ones can be resized with realloc(), and what this will do is move the array to a new memory location, that can accomodate the whole array.

Javascript

In javascript the arrays can be sparse arrays and dense array. Javascript arrays are dynamic. So any implementation has to account for variable length of the array.

The Javascript Array wraps a simple C array allocated with a fixed number of elements. (a flat array) The elements in the C array are resized everytime access to a location beyond the current maximum length is required. Hence insertion could be expensive.

When the runtime detects that the array is Sparse, it is implemented in a similar way to an object. So instead of maintaining a contiguous array, a key/value map is built. In case of an array since the indexes are integers, this map can be efficiently implemented using a balanced Binary Search Tree or a similar structure. The performance of a sparse array is likely similar to the performance of an object.

The arrays are initialized with Flat arrays and are then later replaced with Sparse array map if necessary.

Even with the above general ideas, there is still so much leeway for browsers to tweak their implementations – the heuristic for deciding if the array is sparse/dense, internal data structures used etc.

The key lessons though, from understanding these differences are:

Never use an object, when you need an array

Never initialize arrays backwards (this will force create sparse array which is less efficient than dense arrays)

Lets look at some examples

Sparse arrays

When you define the length of the array, you are not actually allocating the entries up front, but you are just allocates a slot for all the entries.