Added null checks for malloc
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../globals.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
|
||||
13
src/main.c
13
src/main.c
@@ -6,12 +6,13 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "./utils/utils.h"
|
||||
#include "./snake/snake.h"
|
||||
#include "./board/board.h"
|
||||
#include "globals.h"
|
||||
|
||||
const char VERSION[] = "1.1.5";
|
||||
const char* VERSION = "1.1.6";
|
||||
|
||||
const char SNAKE_VIS = '#';
|
||||
|
||||
@@ -75,12 +76,18 @@ int main(int argc, char** argv) {
|
||||
const int sleepInterval = 200;
|
||||
|
||||
// Snake head setup
|
||||
snakePart* snakeHead = (snakePart*) malloc(sizeof(snakePart));
|
||||
snakePart* snakeHead = malloc(sizeof(snakePart));
|
||||
if (!snakeHead) {
|
||||
mallocError("snakeHead", "main.c", "main()");
|
||||
}
|
||||
snakeHead->x = randomX(initClock);
|
||||
snakeHead->y = randomY(initClock);
|
||||
snakeHead->visChar = '&';
|
||||
snakeHead->dir = 'w';
|
||||
snakeHead->order = (order*) malloc(sizeof(order));
|
||||
snakeHead->order = malloc(sizeof(order));
|
||||
if (!snakeHead->order) {
|
||||
mallocError("snakeHead->order", "main.c", "main()");
|
||||
}
|
||||
snakeHead->order->dir = snakeHead->dir;
|
||||
snakeHead->order->delay = -1;
|
||||
snakeHead->order->next = NULL;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "snake.h"
|
||||
#include "../board/board.h"
|
||||
#include "../utils/utils.h"
|
||||
|
||||
extern char SNAKE_VIS;
|
||||
extern boardInfo brdInfo;
|
||||
@@ -74,11 +76,18 @@ void addSnakePart(char board[][brdInfo.x], snakePart* head) {
|
||||
while (tail->next != NULL)
|
||||
tail = tail->next;
|
||||
|
||||
snakePart* newTail = (snakePart*) malloc(sizeof(snakePart));
|
||||
snakePart* newTail = malloc(sizeof(snakePart));
|
||||
if (!newTail) {
|
||||
mallocError("newTail", "snake.c", "addSnakePart()");
|
||||
}
|
||||
|
||||
newTail->visChar = SNAKE_VIS;
|
||||
newTail->dir = tail->dir;
|
||||
// Order head
|
||||
newTail->order = (order*) malloc(sizeof(order));
|
||||
newTail->order = malloc(sizeof(order));
|
||||
if (!newTail->order) {
|
||||
mallocError("newTail", "snake.c", "addSnakePart()");
|
||||
}
|
||||
newTail->order->dir = newTail->dir;
|
||||
newTail->order->delay = -1;
|
||||
// First order if exists
|
||||
@@ -137,7 +146,10 @@ void pushOrder(order* head, char dir, int delay) {
|
||||
while (current->next != NULL)
|
||||
current = current->next;
|
||||
|
||||
order* newOrder = (order*) malloc(sizeof(order));
|
||||
order* newOrder = malloc(sizeof(order));
|
||||
if (!newOrder) {
|
||||
mallocError("newOrder", "snake.c", "pushOrder()");
|
||||
}
|
||||
newOrder->dir = dir;
|
||||
newOrder->delay = delay;
|
||||
newOrder->next = NULL;
|
||||
@@ -179,7 +191,10 @@ void copyOrders(order* srcHead, order* destHead) {
|
||||
while (srcCurrent->next != NULL) {
|
||||
srcCurrent = srcCurrent->next;
|
||||
destPrev = destCurrent;
|
||||
destCurrent = (order*) malloc(sizeof(order));
|
||||
destCurrent = malloc(sizeof(order));
|
||||
if (!destCurrent) {
|
||||
mallocError("destCurrent", "snake.c", "copyOrders()");
|
||||
}
|
||||
destPrev->next = destCurrent;
|
||||
destCurrent->dir = srcCurrent->dir;
|
||||
destCurrent->delay = srcCurrent->delay + 1;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
@@ -37,3 +39,8 @@ int randomY(const clock_t initClock) {
|
||||
void sleep_ms(const int ms) {
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
void mallocError(const char* varName, const char* fileName, const char* functionName) {
|
||||
printf("Ran out of memory to allocate to %s in %s/%s\n", varName, fileName, functionName);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define UTILS_H_
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include "../globals.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
@@ -10,5 +11,6 @@ int randomInt(const int start, const int end, const unsigned int seed);
|
||||
int randomX(const clock_t initClock);
|
||||
int randomY(const clock_t initClock);
|
||||
void sleep_ms(const int ms);
|
||||
void mallocError(const char* varName, const char* fileName, const char* functionName);
|
||||
|
||||
#endif // UTILS_H_
|
||||
|
||||
Reference in New Issue
Block a user