I am currently working on a virtual Filesystem. There are no actual physical folders or files, its just about practising data structures. To create a File or a Folder i have this struct:
struct FS_Object {
int32_t id;
struct FS_Object* parent;
dataType dataType;
FolderInfo folderInfo;
FileInfo fileInfo;
};
FolderInfo and FileInfo are structs that contain an ID, a name and some other attributes.
Im storing every created struct in a global array to have access to the properties:
FS_Object* dataList;
So if I want to free a previous created FS_Object (lets say stored in dataList[1]):
FS_Objects are created like this:
FS_Object fsObject = malloc(sizeof(struct FS_Object));
1. Check if the element exists
if (dataList[1] != NULL) ...
2. Free FolderInfo struct from FS_Object struct like this:
FolderInfo *folderInfo = &dataList[1]->folderInfo;
free(folderInfo);
folderInfo = NULL;
3. Free FileInfo:
fileInfo *fileInfo = &dataList[1]->fileInfo;
free(fileInfo);
fileInfo = NULL;
4. Finally free the FS_Object struct itself:
free(dataList[1]);
dataList[1] = NULL;
Is this the right approach? I am currently getting the following exception when I'm trying to free the FolderInfo struct:
*** Error in `./filesystem': free(): invalid pointer: 0x0000000001fa70e8 ***