mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
fix(solutions): memory issues thanks to -fsanitize=address flag with gcc
This commit is contained in:
@ -1,8 +1,5 @@
|
||||
#include "character.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void character_append(char* string, char character) {
|
||||
size_t length = strlen(string);
|
||||
string[length] = character;
|
||||
|
@ -1,6 +1,9 @@
|
||||
#ifndef __CHARACTER__
|
||||
#define __CHARACTER__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @brief Append a character to a string, assuming string points to an array
|
||||
* with enough space.
|
||||
|
@ -1,10 +1,5 @@
|
||||
#include "input.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
char* input() {
|
||||
char character;
|
||||
size_t length = 1;
|
||||
|
@ -1,6 +1,11 @@
|
||||
#ifndef __INPUT__
|
||||
#define __INPUT__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
/**
|
||||
* @brief Read a line from stdin.
|
||||
*
|
||||
|
@ -20,7 +20,7 @@ bool is_integer(char* string) {
|
||||
|
||||
int main() {
|
||||
char* string = input();
|
||||
struct Stack* stack = stack_initialization();
|
||||
struct stack* stack = stack_initialization();
|
||||
char* token = strtok(string, " ");
|
||||
while (token != NULL) {
|
||||
if (is_integer(token)) {
|
||||
@ -45,5 +45,7 @@ int main() {
|
||||
token = strtok(NULL, " ");
|
||||
}
|
||||
printf("%ld\n", (intptr_t)stack_pop(stack));
|
||||
free(string);
|
||||
stack_free(stack);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@ -1,11 +1,9 @@
|
||||
#include "stack.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Stack *stack_initialization() {
|
||||
struct Stack *stack = malloc(sizeof(*stack));
|
||||
struct stack *stack_initialization() {
|
||||
struct stack *stack = malloc(sizeof(struct stack));
|
||||
if (stack == NULL) {
|
||||
perror("Error (stack_initialization)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
stack->first = NULL;
|
||||
@ -13,9 +11,15 @@ struct Stack *stack_initialization() {
|
||||
return stack;
|
||||
}
|
||||
|
||||
void stack_push(struct Stack *stack, void *data) {
|
||||
struct Node *node_new = malloc(sizeof(*node_new));
|
||||
if (stack == NULL || data == NULL) {
|
||||
void stack_push(struct stack *stack, void *data) {
|
||||
if (stack == NULL) {
|
||||
errno = EINVAL;
|
||||
perror("Error (stack_push)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
struct stack_node *node_new = malloc(sizeof(struct stack_node));
|
||||
if (data == NULL) {
|
||||
perror("Error (stack_push)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
node_new->data = data;
|
||||
@ -24,11 +28,13 @@ void stack_push(struct Stack *stack, void *data) {
|
||||
stack->length = stack->length + 1;
|
||||
}
|
||||
|
||||
void *stack_pop(struct Stack *stack) {
|
||||
void *stack_pop(struct stack *stack) {
|
||||
if (stack == NULL) {
|
||||
errno = EINVAL;
|
||||
perror("Error (stack_pop)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
struct Node *node = stack->first;
|
||||
struct stack_node *node = stack->first;
|
||||
void *data = NULL;
|
||||
if (node != NULL) {
|
||||
stack->first = node->next;
|
||||
@ -38,3 +44,18 @@ void *stack_pop(struct Stack *stack) {
|
||||
stack->length = stack->length - 1;
|
||||
return data;
|
||||
}
|
||||
|
||||
void stack_free(struct stack *stack) {
|
||||
if (stack == NULL) {
|
||||
errno = EINVAL;
|
||||
perror("Error (stack_free)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
struct stack_node *node = stack->first;
|
||||
while (node != NULL) {
|
||||
struct stack_node *node_next = node->next;
|
||||
free(node);
|
||||
node = node_next;
|
||||
}
|
||||
free(stack);
|
||||
}
|
||||
|
@ -1,23 +1,54 @@
|
||||
#ifndef __STACK__
|
||||
#define __STACK__
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// LIFO = Last In First Out
|
||||
struct Stack {
|
||||
struct Node *first;
|
||||
/**
|
||||
* @brief Stack structure => LIFO (Last In First Out).
|
||||
*/
|
||||
struct stack {
|
||||
struct stack_node *first;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
/**
|
||||
* @brief Stack node structure.
|
||||
*/
|
||||
struct stack_node {
|
||||
void *data;
|
||||
struct Node *next;
|
||||
struct stack_node *next;
|
||||
};
|
||||
|
||||
struct Stack *stack_initialization();
|
||||
/**
|
||||
* @brief Stack initialization.
|
||||
*
|
||||
* @return struct stack*
|
||||
*/
|
||||
struct stack *stack_initialization();
|
||||
|
||||
void stack_push(struct Stack *stack, void *data);
|
||||
/**
|
||||
* @brief Push data to stack.
|
||||
*
|
||||
* @param stack
|
||||
* @param data
|
||||
*/
|
||||
void stack_push(struct stack *stack, void *data);
|
||||
|
||||
void *stack_pop(struct Stack *stack);
|
||||
/**
|
||||
* @brief Pop data from stack.
|
||||
*
|
||||
* @param stack
|
||||
* @return void*
|
||||
*/
|
||||
void *stack_pop(struct stack *stack);
|
||||
|
||||
/**
|
||||
* @brief Frees the stack.
|
||||
*
|
||||
* @param stack
|
||||
*/
|
||||
void stack_free(struct stack *stack);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user