SightBack
SightBack

Reputation: 81

How to remove an entry in a fixed length array in C?

I am working on a program that resembles the Unix File System. I am working in C. I am stuck on the following problem:

typedef struct {
    int16_t iNodeNumber;
    char Filename[14];
} Entry;

Note that the memory of the array is on the stack, not the heap.

What I want : Remove a specific entry from the array while keeping the length of the array

What I have done : I replace the current position (the one which I have to delete the info) by the content of the next index until I reach the end of the array. Thus, I can make sure that upon deletion, every entry that has an empty block in the next array position is actually the last one. (No fragmentation)

However this does not work if I want to remove the last entry of the array (position 13). Also I thought about setting the Filename to an empty string but what about the iNodeNumber? It is a number that ranges from 2 to 23; 0 and 1 are specific iNode numbers reserved for other usage.

Thanks!

EDIT : Here are more explanations to the problem : Say we have a directory, let's take this directory.

/example

In UFS, as each file is represented by an Inode and not a name, this directory would be represented by this struct:

typedef struct {
stat iNodestat;
int16_t dataBlock[14]
} iNodeEntry

The stat struct can be omitted it's simply a struct which contains informations on the inode like the number, the mode, the file size, etc.

This dataBlock is an array of integers. Those integers simply reference a memory block. A regular file iNodeEntry may have 14 blocks of data. In the case of a directory, it only has one block of data. This sole block number references a memory block which contains the Array that is the subject of this post.

This array is composed of the Entry struct shown above. It contains exactly 14 of these structs.

To conclude, here's a concrete example. Consider :

It would then have two entries filled with information out of the 14 available in the array :

Upvotes: 0

Views: 239

Answers (0)

Related Questions