The growing array's promise is that indexes 0 up to length - 1 are the active elements, with no holes. Removing an element from the middle breaks that promise unless something fills the hole. You could mark the slot as "deleted", but then every loop must skip markers, and totals, searches and reports all get a new way to be wrong. The usual approach is to close the gap: shift every later element one slot to the left and reduce the length by one.
Predict and run
Read the program before pressing Run. Write down its output and work out how many bytes memmove copies.
#include <stdio.h>
#include <string.h>
int main(void) {
int values[4] = {10, 20, 30, 40};
size_t length = 4;
size_t index = 1;
memmove(&values[index], &values[index + 1], (length - index - 1) * sizeof *values);
--length;
for (size_t i = 0; i < length; ++i) { printf("%d\n", values[i]); }
return 0;
}Check your prediction
10
30
40Two elements follow index 1, so memmove copies 2 * sizeof *values bytes from the slot of 30 into the slot of 20. The array now holds 10, 30, 40, 40, and the length of 3 makes the final 40 inactive. Its memory still contains a value, but it is no longer a record.
Why memmove and not memcpy
Both functions from <string.h> copy bytes. memcpy requires that the source and destination do not overlap; if they do, the behavior is undefined. Here they overlap: the destination starts one element before the source, and both ranges share the middle elements. A memcpy may happen to work on your machine and fail with another library or optimization level, because a fast implementation may copy in large chunks or in a different direction.
memmove is defined to work as if the bytes were first copied to a temporary buffer, so overlap is safe. Use memcpy only when you know the ranges are separate, such as copying a whole array into a newly allocated block.
Check the index before any arithmetic
The count of following elements is length - index - 1. With unsigned size_t, that subtraction wraps if index >= length: removing index 5 from a 3-element array would compute a huge count and memmove would read and write far beyond the array. So the first statement of a removal function is the bound check, before the subtraction:
if (index >= *length) { return STATUS_INVALID; }
size_t following = *length - index - 1; /* now between 0 and length - 1 */Removing the last element gives following == 0. Nothing needs to move; reducing the length is enough. A zero-byte memmove with valid pointers is allowed, but skipping the call makes the case obvious to a reader.
A faster removal that changes order
If the order of elements does not matter, you can fill the hole with the last element instead of shifting everything. That costs one copy instead of up to length - 1:
#include <stdio.h>
#include <stddef.h>
int remove_unordered(int data[], size_t *length, size_t index) {
if (index >= *length) { return 0; }
data[index] = data[*length - 1];
--*length;
return 1;
}
int main(void) {
int values[5] = {10, 20, 30, 40, 50};
size_t length = 5;
remove_unordered(values, &length, 1);
for (size_t i = 0; i < length; ++i) { printf("%d ", values[i]); }
printf("\n");
return 0;
}10 50 30 40The analyzer keeps insertion order, because a user who removes the second record expects the others to stay where they were. So it shifts with memmove. Choose by contract, not by speed alone.
Indexes and pointers after removal
After a shifting removal, the same index refers to a different record: what was at index 2 is now at index 1. A pointer saved before the removal still points into valid memory, so using it is not undefined behavior, but it now names a different record, or an inactive slot if it pointed at the old last element. The project's header treats such pointers as expired after any removal. Look elements up again after changing the array.
Practice
Your turn
Write size_t remove_below(int data[], size_t *length, int minimum) that removes every value smaller than minimum, keeps the rest in their original order, and returns how many it removed. Do it in a single pass with no memmove. The starter must print: removed 2: 25 40 10, then removed 0: (an empty array), then removed 3: (all values too small).
#include <stdio.h>
#include <stddef.h>
/* Write remove_below here. */
static void show(int data[], size_t length, int minimum) {
size_t removed = remove_below(data, &length, minimum);
printf("removed %zu:", removed);
for (size_t i = 0; i < length; ++i) { printf(" %d", data[i]); }
printf("\n");
}
int main(void) {
int mixed[5] = {25, 5, 40, 3, 10};
int empty[1] = {0};
int small[3] = {1, 2, 3};
show(mixed, 5, 10);
show(empty, 0, 10);
show(small, 3, 10);
return 0;
}Hint 1: the idea
Use two indexes. One reads every element; the other marks where the next kept element should go. The write index never passes the read index, so you never overwrite a value you have not read yet.
Hint 2: a first step
Trace the first starter array on paper with two columns, one per index, before writing code: after each element, where is each index? Then write the loop exactly as your trace moves, and compute the return value from the two lengths.
Show one solution
#include <stdio.h>
#include <stddef.h>
size_t remove_below(int data[], size_t *length, int minimum) {
size_t kept = 0;
for (size_t i = 0; i < *length; ++i) {
if (data[i] >= minimum) {
data[kept] = data[i];
++kept;
}
}
size_t removed = *length - kept;
*length = kept;
return removed;
}
static void show(int data[], size_t length, int minimum) {
size_t removed = remove_below(data, &length, minimum);
printf("removed %zu:", removed);
for (size_t i = 0; i < length; ++i) { printf(" %d", data[i]); }
printf("\n");
}
int main(void) {
int mixed[5] = {25, 5, 40, 3, 10};
int empty[1] = {0};
int small[3] = {1, 2, 3};
show(mixed, 5, 10);
show(empty, 0, 10);
show(small, 3, 10);
return 0;
}removed 2: 25 40 10
removed 0:
removed 3:Because kept <= i at every step, each element is read before its slot can be overwritten, and the kept values stay in order. The subtraction *length - kept cannot wrap, since kept counts a subset of the elements. This takes one pass, while calling a shifting removal for each small value could move the same elements many times. The checkpoint next combines growth, validation and cleanup into one collection.
Completion is saved in this browser only. It does not measure mastery.