alloc_map - 1Fr3aK2/Cub3d GitHub Wiki

📝 alloc_map

Allocates the map pointer in the t_map structure to point to the correct position in the buffer.

⚙️ Parameters

Parameter Type Description
map t_map * Pointer to the map structure containing the buffer and map pointers.
i int * Pointer to the index in the buffer from which the map should start. Must be non-negative.

🔁 Returns

Return value Description
bool Returns true if the map pointer was successfully assigned; false if the input is invalid or allocation fails.

📖 Description

The alloc_map function initializes the map pointer inside a t_map structure to point to a specific position in the buffer. The function performs the following steps:

  • Checks that the pointers map, map->buffer, and i are not NULL, and that *i is non-negative. Returns false if any of these conditions are not met.
  • Sets map->map to point to map->buffer[*i].
  • Checks if map->map is valid (not NULL). Returns false if the allocation fails.
  • Returns true if the pointer was successfully set.

This function does not allocate memory itself but assigns a pointer within an existing buffer, ensuring the map points to the correct location.

💡 Example Usage

t_map map;
int start_index = 5;

// Assuming map.buffer has been allocated and filled previously
if (alloc_map(&map, &start_index)) {
    printf("Map pointer set successfully!\n");
} else {
    printf("Failed to set map pointer.\n");
}