Skip to content

Some notes #1

Description

@thodnev

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)

void DrawGrid()

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

char symbol[2] = {};

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))

img

Wow, visual representation is beautiful!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions