mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
fix(solutions): fix: more memory issues thanks to -fsanitize=address flag with gcc
This commit is contained in:
@ -13,48 +13,29 @@ void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_col
|
||||
}
|
||||
|
||||
int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) {
|
||||
int **array = malloc(sizeof(int) * 2);
|
||||
*number_of_rows = 1;
|
||||
*number_of_columns = 1;
|
||||
array[0] = malloc(*number_of_columns * sizeof(int));
|
||||
array[0][0] = 0;
|
||||
char character;
|
||||
size_t length = 1;
|
||||
char *string = malloc(length * sizeof(char));
|
||||
*string = '\0';
|
||||
while ((character = getchar()) != EOF) {
|
||||
if (character == '\n') {
|
||||
int number = atoi(string);
|
||||
array[*number_of_rows - 1][*number_of_columns - 1] = number;
|
||||
length = 1;
|
||||
memset(string, 0, length * sizeof(char));
|
||||
*string = '\0';
|
||||
*number_of_rows = *number_of_rows + 1;
|
||||
*number_of_columns = 1;
|
||||
array = realloc(array, *number_of_rows * sizeof(int *));
|
||||
array[*number_of_rows - 1] = malloc(*number_of_columns * sizeof(int));
|
||||
} else {
|
||||
if (character == ' ') {
|
||||
int number = atoi(string);
|
||||
array[*number_of_rows - 1][*number_of_columns - 1] = number;
|
||||
length = 1;
|
||||
memset(string, 0, length * sizeof(char));
|
||||
*string = '\0';
|
||||
*number_of_columns = *number_of_columns + 1;
|
||||
} else {
|
||||
length++;
|
||||
string = realloc(string, length * sizeof(char));
|
||||
character_append(string, character);
|
||||
}
|
||||
*number_of_rows = 0;
|
||||
*number_of_columns = 0;
|
||||
int **array = malloc(sizeof(int *));
|
||||
char *line = input();
|
||||
while (string_get_length(line) != 0) {
|
||||
char **integers_string = string_split(line, ' ', number_of_columns);
|
||||
array[*number_of_rows] = malloc(*number_of_columns * sizeof(int));
|
||||
for (size_t column = 0; column < *number_of_columns; column++) {
|
||||
array[*number_of_rows][column] = atoi(integers_string[column]);
|
||||
free(integers_string[column]);
|
||||
}
|
||||
free(integers_string);
|
||||
free(line);
|
||||
line = input();
|
||||
*number_of_rows += 1;
|
||||
array = realloc(array, (*number_of_rows + 1) * sizeof(int *));
|
||||
}
|
||||
int number = atoi(string);
|
||||
array[*number_of_rows - 1][*number_of_columns - 1] = number;
|
||||
free(line);
|
||||
return array;
|
||||
}
|
||||
|
||||
int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns) {
|
||||
int **rotated_array = malloc(*number_of_rows * sizeof(int));
|
||||
int **rotated_array = malloc(*number_of_rows * sizeof(int *));
|
||||
for (size_t row = 0; row < *number_of_rows; row++) {
|
||||
rotated_array[row] = malloc(*number_of_columns * sizeof(int));
|
||||
}
|
||||
|
@ -6,6 +6,8 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
#include "input.h"
|
||||
#include "string.h"
|
||||
|
||||
/**
|
||||
* @brief Prints a 2D array of integers.
|
||||
|
@ -9,6 +9,7 @@ int main() {
|
||||
size_t number_of_columns = 0;
|
||||
char *direction = input();
|
||||
int **array_input = array_2D_int_input(&number_of_rows, &number_of_columns);
|
||||
size_t initial_number_of_rows = number_of_rows;
|
||||
|
||||
int **array;
|
||||
if (strcmp(direction, "clockwise") == 0) {
|
||||
@ -19,6 +20,10 @@ int main() {
|
||||
array_2D_int_print(array, number_of_rows, number_of_columns);
|
||||
|
||||
free(direction);
|
||||
for (size_t row = 0; row < initial_number_of_rows; row++) {
|
||||
free(array_input[row]);
|
||||
}
|
||||
free(array_input);
|
||||
for (size_t row = 0; row < number_of_rows; row++) {
|
||||
free(array[row]);
|
||||
}
|
||||
|
@ -0,0 +1,64 @@
|
||||
#include "string.h"
|
||||
|
||||
size_t string_get_length(const char* string) {
|
||||
size_t length = 0;
|
||||
while (string[length] != '\0') {
|
||||
length++;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
char* string_copy(const char* string) {
|
||||
size_t source_length = string_get_length(string);
|
||||
char* copy = malloc(sizeof(char) * (source_length + 1));
|
||||
if (copy == NULL) {
|
||||
perror("Error (string_copy)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
size_t index;
|
||||
for (index = 0; index < source_length; index++) {
|
||||
copy[index] = string[index];
|
||||
}
|
||||
copy[index] = '\0';
|
||||
return copy;
|
||||
}
|
||||
|
||||
char** string_split(const char* string, char separator, size_t* result_size) {
|
||||
size_t string_length = string_get_length(string);
|
||||
size_t index_string = 0;
|
||||
size_t index_current = 0;
|
||||
size_t index_result = 0;
|
||||
char* current = malloc(sizeof(char) * (string_length + 1));
|
||||
char** result = NULL;
|
||||
if (current == NULL) {
|
||||
perror("Error (string_split)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
while (index_string < string_length) {
|
||||
if (string[index_string] == separator) {
|
||||
current[index_current] = '\0';
|
||||
result = realloc(result, sizeof(char*) * (index_result + 1));
|
||||
if (result == NULL) {
|
||||
perror("Error (string_split)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
result[index_result] = string_copy(current);
|
||||
index_result++;
|
||||
index_current = 0;
|
||||
} else {
|
||||
current[index_current] = string[index_string];
|
||||
index_current++;
|
||||
}
|
||||
index_string++;
|
||||
}
|
||||
current[index_current] = '\0';
|
||||
result = realloc(result, sizeof(char*) * (index_result + 1));
|
||||
if (result == NULL) {
|
||||
perror("Error (string_split)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
result[index_result] = string_copy(current);
|
||||
free(current);
|
||||
*result_size = index_result + 1;
|
||||
return result;
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
#ifndef __STRING__
|
||||
#define __STRING__
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* @brief Return the length of a string (excluding '\0').
|
||||
*
|
||||
* @param string
|
||||
* @return size_t
|
||||
*/
|
||||
size_t string_get_length(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Return the copy of a string.
|
||||
*
|
||||
* @param string
|
||||
* @return string_t
|
||||
*/
|
||||
char* string_copy(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Split a string into substrings using the specified separator and return them as an array and update the pointer `result_size` to the resulting size of the created array.
|
||||
*
|
||||
* @param string
|
||||
* @param separator
|
||||
* @param result_size
|
||||
* @return string_t*
|
||||
*/
|
||||
char** string_split(const char* string, char separator, size_t* result_size);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user