Good implementation. Seems you're absolutely confident with C.
Below are some comments on possible ways to improve the code yet more
GL_BaseCamp/01-L-Introduction
|
#define WIDTH 9 |
|
#define HEIGHT 7 |
|
|
|
int gridArray[WIDTH][HEIGHT] = { |
Now we have C11 and it could be done with variable-size declaration:
int grid_array[][] = {
{ 0, -1, -1, -1, -2, -1, -2 },
{ -2, -1, -1, -1, -1, -2, -2 },
{ -2, -1, -1, -1, -1, -2, -2 },
{ -2, -2, -1, -1, -1, -2, -1 },
{ -1, -2, -2, -2, -1, -1, -2 },
{ -2, -1, -1, -1, -1, -2, -2 },
{ -2, -2, -1, -1, -1, -2, -2 },
{ -2, -2, -1, -1, -1, -2, -2 },
{ -2, -2, -1, -3, -1, -2, -2 }
};
const unsigned int h = sizeof grid_array / sizeof grid_array[0];
const unsigned int w = sizeof grid_array[0] / sizeof grid_array[0][0];
|
int gridArray[WIDTH][HEIGHT] = { |
Pay attention to naming scheme. The CamelCaseNames shouldn't be used unless you're declaring a class-like object. (per
CodingStyle)
The
void func() means to compiler that arguments are unknown at compile-time. This could lead to weird obscure errors. It's better to explicitly use
void func(void) if function takes no arguments
What happens here. You declare a 2-byte string, and it gets assigned the int 0,0 value, which essentially equals '\0', '\0'. It's better to do it explicitly and assign
symbol an empty string. Also it exists only during the { code block } time, so it's better to reserve more space to allow for any int to fit, i.e.
size = 1 + int(floor(log10(INT_MAX)));
|
for (int x = 0; x < WIDTH && !foundEnd; ++x) { |
The person reading your code should keep in memory the whole
operator precedence table. To avoid this, better explicitly place () braces.
|
int* north = &(gridArray[x][y + 1]); //ptr to grid |
Better to assign directly to array and avoid using pointer here
|
} |
|
else if (*west == -1) { |
CodingStyle says it should be done like shown below
} else if (*west == -1) {
McCabe complicity too high here
|
struct timespec time_now, time_after; |
Error here: timespecs get created, but not filled with
clock_gettime() value. It results in difference of junk being calculated by diff instead of time. (
malloc in contrast to
calloc does not clear the allocated memory)
GL_BaseCamp/02-P-Dev_Tools
|
main.o: main.c |
|
gcc -c -o $(DEPS) $(SRC) |
This stuff is not required. The
$(TARGET) should depend on
$(TARGET:=.c) file and that's all what needed. Take a look at this Makefile, its representative of a yet simple but good approach:
TARGET=imfilter
DEPS=libimf
LIBS=
CC=gcc
CFLAGS=-O2 -Wall -Wextra -Wpedantic -Werror
LIBFLAGS:=-lm
ifneq ("$(LIBS), "")
LIBFLAGS+=$(shell pkg-config --cflags --libs $(LIBS) | sed -e 's/^[[:space:]]*//')
endif
DEPS:=$(addsuffix .o, $(DEPS))
.PHONY: help all clean
help: ## display this message
@echo Available options:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
all: clean | $(TARGET) ## clean & build all
$(TARGET): $(DEPS) ## build target executable
$(CC) $(CFLAGS) $(LIBFLAGS) $(addsuffix .c, $(TARGET)) -c
$(CC) $(CFLAGS) $(LIBFLAGS) $(DEPS) $(addsuffix .o, $(TARGET)) -o $@
%.o: %.c
$(CC) $(CFLAGS) $(LIBFLAGS) -c $<
clean: ## tidy build directory
@echo Cleaning up...
-rm -f $(DEPS) $(TARGET) $(addsuffix .o, $(TARGET))

Wow, visual representation is beautiful!
Good implementation. Seems you're absolutely confident with C.
Below are some comments on possible ways to improve the code yet more
GL_BaseCamp/01-L-Introduction
GL_BaseCamp/01-L-Introduction/main.c
Lines 16 to 19 in 59705ea
Now we have C11 and it could be done with variable-size declaration:
GL_BaseCamp/01-L-Introduction/main.c
Line 19 in 59705ea
Pay attention to naming scheme. The CamelCaseNames shouldn't be used unless you're declaring a class-like object. (per CodingStyle)
GL_BaseCamp/01-L-Introduction/main.c
Line 32 in 59705ea
The
void func()means to compiler that arguments are unknown at compile-time. This could lead to weird obscure errors. It's better to explicitly usevoid func(void)if function takes no argumentsGL_BaseCamp/01-L-Introduction/main.c
Line 36 in 59705ea
What happens here. You declare a 2-byte string, and it gets assigned the int 0,0 value, which essentially equals '\0', '\0'. It's better to do it explicitly and assign
symbolan empty string. Also it exists only during the { code block } time, so it's better to reserve more space to allow for any int to fit, i.e.size = 1 + int(floor(log10(INT_MAX)));GL_BaseCamp/01-L-Introduction/main.c
Line 61 in 59705ea
The person reading your code should keep in memory the whole operator precedence table. To avoid this, better explicitly place () braces.
GL_BaseCamp/01-L-Introduction/main.c
Line 73 in 59705ea
Better to assign directly to array and avoid using pointer here
GL_BaseCamp/01-L-Introduction/main.c
Lines 102 to 103 in 59705ea
CodingStyle says it should be done like shown below
GL_BaseCamp/01-L-Introduction/main.c
Lines 119 to 124 in 59705ea
McCabe complicity too high here
GL_BaseCamp/01-L-Introduction/main.c
Line 137 in 59705ea
Error here: timespecs get created, but not filled with
clock_gettime()value. It results in difference of junk being calculated by diff instead of time. (mallocin contrast tocallocdoes not clear the allocated memory)GL_BaseCamp/02-P-Dev_Tools
GL_BaseCamp/02-P-Dev_Tools/Makefile
Lines 18 to 19 in 59705ea
This stuff is not required. The
$(TARGET)should depend on$(TARGET:=.c)file and that's all what needed. Take a look at this Makefile, its representative of a yet simple but good approach:Wow, visual representation is beautiful!