free_file - 1Fr3aK2/Cub3d GitHub Wiki
📝 free_file
This function releases resources associated with a t_file structure, specifically closing the file descriptor if it is open.
⚙️ Parameters
| Parameter | Type | Description |
|---|---|---|
file |
t_file* |
A pointer to a t_file structure to be freed. |
🔁 Returns
| Return value | Description |
|---|---|
void |
This function does not return a value. It closes the file descriptor if open. |
📖 Description
The free_file function checks whether the given t_file pointer is valid. If the structure exists and its file descriptor (fd) is open (i.e., greater than or equal to 0), the function closes the file descriptor using close() and sets it to -1 to indicate it is no longer valid.
- Ensures that no invalid file descriptor remains open.
- Prevents resource leaks by closing the file if it is still open.
- Safe to call with a
NULLpointer; in that case, the function does nothing.
💡 Example Usage
t_file myfile;
myfile.fd = open("example.txt", O_RDONLY);
free_file(&myfile); // Closes the file descriptor and sets fd to -1
printf("File descriptor after free_file: %d\n", myfile.fd); // Should print -1