NCL 406: Array Headers

In the previous lesson, we built arrays whose elements occupied several memory locations.

A player record with a stride of 3 might contain:

Offset Field
0 Name
1 Score
2 Wins

If the array begins at 0x100, we can calculate any field from its index, stride, and field offset:

address = base + index × stride + offset

That tells us where an element is.

But there's something our array still doesn't tell us:

How many elements does it contain?

So far, that's been something the program simply had to know.

Storing the Length

Suppose we reserve $100 through $10F for an array.

That's sixteen memory locations.

Instead of using all sixteen for elements, we'll reserve the first location for the array's length:

Address Meaning
$100 Length
$101 Element 0
$102 Element 1
$103 Element 2
... ...
$10F Element 14

There is room for fifteen elements.

If the array currently contains:

10 20 30 40

then memory might look like this:

Address Value Meaning
$100 4 Length
$101 10 Element 0
$102 20 Element 1
$103 30 Element 2
$104 40 Element 3
$105–$10F — Unused

The array has a capacity of 15, but a length of 4.

These describe different things:

  • Capacity is how many elements we have reserved room for.
  • Length is how many elements are currently part of the array.

For this array:

capacity = 15
length   = 4

The unused locations are still reserved for the array. They just don't contain elements yet.

Length counts elements

The length is the number of elements currently in the array, not the number of memory locations reserved for it.

This distinction becomes especially important when an element occupies more than one location.

A Header

The value at $100 isn't one of the array's elements.

It describes the elements that follow it.

This makes $100 a header.

Information stored alongside data to describe that data is often called metadata.

Our array now contains both:

header       elements
   ↓            ↓
[length] [10] [20] [30] [40] ...

The structure itself tells us how many of those element locations are currently meaningful.

That changes our address calculation slightly.

Previously, an ordinary stride-1 array used:

address = base + index

Now the first location belongs to the header, so:

address = base + 1 + index

For our array:

base = 0x100

so the elements begin at:

0x101

Element 0 is at $101, element 1 is at $102, and so on.

Appending an Element

The length gives us something else for free.

If an array has four elements, their indices are:

0 1 2 3

The next available index is:

4

That's exactly the current length.

So to append a value, we can calculate:

address = element base + length

For our array:

address = 0x101 + length

With a length of 4:

address = 0x101 + 4
        = 0x105

So appending 50 means writing it to $105.

#length $100
#address $000
#element [$000]

ADD #address 0x101 #length
MOVE #element 50
INC #length

Afterward:

Address Value Meaning
$100 5 Length
$101 10 Element 0
$102 20 Element 1
$103 30 Element 2
$104 40 Element 3
$105 50 Element 4

We changed both the contents of the array and its header.

When the structure changes, its metadata has to change with it.

Capacity still matters

Our length can change, but the amount of memory we reserved does not.

This array has room for fifteen elements. If its length is already 15, there is nowhere inside the reserved region to append another one.

A program that can reach that state should check the length before writing another element.

Removing the Last Element

Removing the final element is similarly simple.

Suppose the array contains:

10 20 30 40 50

with:

length = 5

The final element has index 4.

If we reduce the length first:

DEC #length

then #length becomes 4 — the index of the element we're removing.

We can calculate its address and free it:

DEC #length
ADD #address 0x101 #length
FREE #element

The array is now:

10 20 30 40

with:

length = 4

The reserved capacity is still 15. We have simply made one more location unused.

Inserting in the Middle

Appending is easy because the new element goes after everything already in the array.

What if we want to insert something in the middle?

Start with:

10 20 30 40

and insert 25 at index 2.

We want:

10 20 25 30 40

But $103, where element 2 belongs, already contains 30.

We need to make room.

First, move the final element one location toward the end:

$104 → $105

10 20 30 40 40

Then move the next element:

$103 → $104

10 20 30 30 40

Now $103 is free to receive our new element:

$103 = 25

10 20 25 30 40

Finally, the array contains one more element:

length = 5

The important detail is the direction in which we moved the existing elements.

We started at the end and worked backward.

If we had copied $103 to $104 first, the 30 would have replaced the 40 before we had copied the 40 anywhere.

By starting at the end, every value is moved to safety before its old location can be overwritten.

Inserting an element

To insert an element into a contiguous array:

  1. Make sure there is unused capacity.
  2. Starting at the end, shift every element from the insertion point onward toward the end.
  3. Write the new element into the gap.
  4. Increase the length.

Deleting from the Middle

Deleting an element has the opposite problem.

Suppose we start with:

10 20 25 30 40

and remove element 2:

10 20 __ 30 40

This time, we close the gap by moving later elements toward the beginning:

30 moves into element 2
40 moves into element 3

giving:

10 20 30 40 40

The final 40 is now outside the new end of the array, so we can free that location and reduce the length:

10 20 30 40

with:

length = 4

Insertion and deletion therefore shift in opposite directions:

  • Insertion works backward from the end.
  • Deletion works forward toward the end.

Both operations must also update the length header.

What About Records?

So far, we've used elements with a stride of 1.

But in the previous lesson, our player records had a stride of 3:

Offset Field
0 Name
1 Score
2 Wins

The same idea still works.

Suppose we reserve $100 through $10F again.

One location holds the length, leaving fifteen locations for player records:

1 header + 5 records × 3 locations = 16 locations

Our layout becomes:

Address Meaning
$100 Length
$101–$103 Player 0
$104–$106 Player 1
$107–$109 Player 2
$10A–$10C Player 3
$10D–$10F Player 4

The capacity is now five players, not fifteen.

Suppose three records are currently present:

Player Name Score Wins
0 "NEMI" 42 7
1 "ALICE" 17 3
2 "BOB" 31 5

The header contains:

length = 3

Even though the three records occupy nine memory locations, there are still only three elements in the array.

Now let's insert a new player at index 1:

Player Name Score Wins
0 "NEMI" 42 7
1 "CHARLIE" 23 4
2 "ALICE" 17 3
3 "BOB" 31 5

Just as before, we have to make room.

But this time, making room for one element means making room for an entire record.

First, Bob moves forward by one stride:

$107 → $10A
$108 → $10B
$109 → $10C

Then Alice:

$104 → $107
$105 → $108
$106 → $109

Now the three locations beginning at $104 are available for Charlie:

$104 = "CHARLIE"
$105 = 23
$106 = 4

Finally:

length = 4

The operation hasn't fundamentally changed.

We still shifted every element from the insertion point onward toward the end.

But now an element has a stride of 3, so moving one element means moving three values.

The length still counts records, not memory locations.

This is one of the costs of keeping an array contiguous.

If we know an index, we can calculate exactly where its record begins:

address = base + header + index × stride

We don't have to visit any earlier records.

But inserting a record in the middle may require moving every record that follows it.

That's a trade-off we'll return to very soon.

Do real data structures use headers?

Yes.

A block of memory often contains metadata describing the data stored with it. Depending on the structure, a header might contain information such as its length, capacity, element size, type, flags, or other information needed to interpret the memory that follows.

The exact layout depends on the data structure and the system using it.

Our one-location length header is deliberately simple, but the same basic idea scales much further.

Try It

An array occupies $200 through $20F.

$200 contains its length, and $201 through $20F provide capacity for fifteen stride-1 elements.

#value contains a new value to append.

Write code that appends #value to the array and updates its length.

You may assume the array is not full.

Show me one solution
#value $000
#address $001

#length $200
#element [$001]

ADD #address 0x201 #length
MOVE #element #value
INC #length

The current length is also the index of the next unused element.

After writing the new value there, incrementing #length makes that element part of the array.

If we could not assume there was unused capacity, we would need to check #length before performing the write.


We've seen how an array can store its own length, and how that length changes as elements are appended, inserted, or removed.

In the next lesson, NCL 407: Linked Lists, we'll build a collection where inserting a record doesn't require shifting everything that follows.