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:

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:

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.:

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:

Integers:
Visualization:

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.
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:

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:

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.:

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:

Integers:
Visualization:

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
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!
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!
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.
Page 1 of 1
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
Recent Comments
Search My Blog
22 user(s) viewing
22 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



4 Comments









|