alloc_temp_map - 1Fr3aK2/Cub3d GitHub Wiki

📝 alloc_temp_map

This function allocates memory for a temporary map (temp_map) in a t_map structure, based on the size of the main map (map->map). The temporary map is a 2D boolean array used for internal operations.

⚙️ Parameters

Parameter Type Description
map t_map* Pointer to the map structure where the temporary map will be allocated.

🔁 Returns

Return value Description
bool Returns true if the temporary map was successfully allocated, or false if an error occurred.

📖 Description

The alloc_temp_map function calculates the dimensions of the main map (map->map) and allocates a corresponding 2D boolean array (map->temp_map). This temporary map can be used for internal logic such as pathfinding, validation, or marking visited cells.

  • First, it checks if the map pointer is valid. If not, it returns false.
  • It counts the number of rows in the main map.
  • It allocates memory for the array of boolean pointers (temp_map) with one extra slot for safety (NULL termination).
  • For each row, it allocates a boolean array of the same length as the corresponding row in the main map.
  • If any memory allocation fails, the function returns false.
  • Finally, it calls set_bool(map) to initialize the temporary map values (presumably setting all booleans to a default value).

💡 Example Usage

t_map map = {0}; // Initialize the map structure
// Assume map.map is already filled with the main map

if (alloc_temp_map(&map)) {
    printf("Temporary map allocated successfully!\n");
} else {
    printf("Failed to allocate temporary map.\n");
}