Subscribe to Stuck in an Infiniteloop        RSS Feed
-----

Removing Elements From An Array -- You Can't

Icon 4 Comments
This question is beyond popular. It will pop up several times a week, without fail, as right as the rain. Or bills.

So, let's kill this once and for all (especially since I'll be linking this entry to any future inquiries on the subject matter).

I won't be using one language in particular, but the concept is the same across the board for high level, strongly typed languages (specifically, you can do some tomfoolery with say, Lua or Python, but that is out of the scope of this post).

When you create an array it looks like this in memory:

Attached Image

Now, let's look at this array using various data-types:

The concepts apply to all data-types and I won't rehash for each type, but characters get the spotlight since they are often the focus of this question.

Characters:

For this section I'll focus on C style strings since the null terminator is integral and often misunderstood, please disregard if you're using a language that doesn't have it.

We create an array "Hello!" sans quotes:

Attached Image

There are varying places in memory to put a char array (stack, heap, static, non, blah blah blah), but the important fact for this example is that the string is mutable.

Now, let's say we wanted to get rid of the exclamation mark. We could zero it out or put a blank space there, but the index remains: "it is not removed" from the array. i.e.:

Attached Image

Since C strings "end" with the null terminator '\0', we could "fake" removing it by moving the null terminator up a slot to where the exclamation mark was and zero out its previous location. I don't advocate this as it's wasteful and places many assumptions on the program. Here's what it would look like:

Attached Image

Integers:

Visualization:

Attached Image

We have similar options here, but "zeroing" something out doesn't make sense, especially when zero can be a significant digit within our data. There's also no null terminator to manipulate.

Doubles, longs, long ints, all the same deal.


So there you have it. You cannot remove elements from an array. A "typical" array, once declared, is set for its life. If you need additional functionality, might I recommend a dynamic data structure such as vectors, linked lists, dynamic arrays, trees, et al.

4 Comments On This Entry

Page 1 of 1

sarmanu 

22 March 2010 - 01:32 PM
Interesting, indeed. I really enjoyed reading this.
0

elgose 

22 March 2010 - 03:29 PM
You mention with C-style strings w/ char arrays that "faking" an ending by placing a null terminator where you would like the string to end is possible but not suggested.

I understand it's wasteful since the slots in the array following the null terminator are still reserved but will not be used, but does it really effect it's useability? Are there any situations you know of where this will result in errors or bugs?

Just wondering since I've been using char arrays quite a bit lately (current book I'm using doesn't introduce strings until a couple more chapters) and I often have to set a "fake" ending.

Otherwise, very instructional and easily understood post!
0

KYA 

22 March 2010 - 06:12 PM
Hypothetically, you could eventually "run out" of memory, but in practice (and depending on your machine specs) that won't happen.

Off the top of my head I can't think of a standard function that would fail from it. strlen(), for example, counts until the null terminator rather then the entire allocated "space" of a string. The only issue might be sizeof(), but no one would use it in lieu of other means of getting a string's length, etc...
0

elgose 

24 March 2010 - 09:00 PM
Definitely understand, though - part of the beauty of having such control over arrays is the ability to be as efficient and clean (and, likewise, as inefficient and messy) as you'd like. I would imagine, at the very least, it's a good sign to see efficiency with your resources and definitely reflects on you as a programmer to your peers when they evaluate your code.
0
Page 1 of 1

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    22 user(s) viewing

    22 Guests
    0 member(s)
    0 anonymous member(s)