Course contentsShow
← C
Lesson 1216 of 1,21916. Practice and ProjectsFree lesson

Adding and Deleting records safely

Adding and Deleting records safely

What you'll learn: How to manage an array of structs by using a counter to track active records and preventing memory overflows.

In this project, we aren't just creating variables; we are building a mini-database. Imagine your program as a physical filing cabinet. To manage this cabinet effectively, you need two things: a fixed number of drawers (your array size) and a sticky note on the front telling you how many drawers are currently full (your counter).

Tracking the "End" of Data

Because C doesn't automatically shrink or grow arrays, we usually define a maximum capacity, like MAX_RECORDS 100. However, just because we have 100 slots doesn't mean we have 100 items.

We use a simple integer variable, often called record_count, to track the next available empty slot. When you add a record, you place it at index record_count and then increment the counter. When you delete a record, you have to decide how to fill the gap so your counter stays accurate.

Safe Addition and Deletion

Before adding a record, you must check if record_count < MAX_RECORDS. If you don't, you'll experience a "buffer overflow," where you try to shove a file into a cabinet that doesn't exist, likely crashing your program.

For deletion, the easiest method in a basic data store is the "Swap and Pop" technique. Instead of shifting every single record up to fill a gap (which is slow), you take the very last record in your list and move it into the spot of the record you want to delete. Then, you simply decrement your counter.

#include <stdio.h>
#include <string.h>

#define MAX_RECORDS 5

typedef struct {
    int id;
    char name[20];
} Record;

Record database[MAX_RECORDS];
int record_count = 0;

void add_record(int id, const char* name) {
    if (record_count < MAX_RECORDS) {
        database[record_count].id = id;
        strncpy(database[record_count].name, name, 20);
        record_count++;
        printf("Record added. Total: %d\n", record_count);
    } else {
        printf("Error: Database full!\n");
    }
}

void delete_record(int index) {
    if (index >= 0 && index < record_count) {
        // Swap the target with the last active record
        database[index] = database[record_count - 1];
        // "Pop" the last record by decreasing the count
        record_count--;
        printf("Record deleted. Total: %d\n", record_count);
    }
}

Keeping it Clean

Safety in C is about boundaries. By always checking your record_count against your MAX_RECORDS, you ensure that your data store remains stable. When deleting, the "Swap and Pop" method keeps your data contiguous, meaning there are never any "empty holes" in the middle of your array that could confuse your search functions later.

Key takeaway

Always validate your array bounds before adding data, and use the "Swap and Pop" method to delete records without leaving gaps in your memory.