* @brief This function allocates memory for a tuple and its elements based on the given size. * Each element is initialized to `NULL`. * * @param size The number of elements the tuple will contain. * @return A pointer to the newly created `Tuple`, or `NULL` if memory allocation fails. * * @note The function will terminate the program if memory allocation fails and `TUPLE_LOGGING_ENABLE` is
| 44 | * @note The function will terminate the program if memory allocation fails and `TUPLE_LOGGING_ENABLE` is defined. |
| 45 | */ |
| 46 | Tuple* tuple_create(size_t size) { |
| 47 | TUPLE_LOG("[tuple_create]: Entering with size: %zu", size); |
| 48 | |
| 49 | Tuple* tuple = (Tuple*)malloc(sizeof(Tuple)); |
| 50 | if (!tuple) { |
| 51 | TUPLE_LOG("[tuple_create]: Error: Memory allocation failed for tuple."); |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | tuple->size = size; |
| 56 | tuple->elements = NULL; |
| 57 | if (size > 0) { |
| 58 | tuple->elements = (TupleElement*)calloc(size, sizeof(TupleElement)); |
| 59 | if (!tuple->elements) { |
| 60 | TUPLE_LOG("[tuple_create]: Error: Memory allocation failed for tuple elements."); |
| 61 | free(tuple); |
| 62 | return NULL; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | TUPLE_LOG("[tuple_create]: Tuple created successfully with size %zu.", size); |
| 67 | return tuple; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
no outgoing calls
no test coverage detected