Hot answers tagged

26

Your implementation is going to be slow, and the excuse "I need it to take a fixed amount of time" does not justify this. Using plain tables smells like cargo culting as well. So I'm not tackling what you did wrong in your code, but what you did wrong in even thinking about your implementation. First, google how to implement cosine on a micro ...


23

You should keep your game loop as simple as possible with only an update and draw method and specifically in that order. To ensure your game loop is iterating at the same speed everytime, you should calculate the duration of your update and draw methods and then subtract that from how long each iteration should last. i.e.: MD = 10ms // max duration for an ...


22

Efficient file I/O By default, files opened with fopen() are buffered, meaning that not every call to fread() or fwrite() will result in a system call. Instead, the C library has an internal buffer and will try to read and write larger chunks at a time. However, you are still paying for the overhead of a regular function call each time you call fread() and ...


22

log is already declared in <math.h>. You don't need to declare it yourself. In fact, it could be harmful. As stated in another answer, do not use floating point math. In fact, you don't need to know the exact position of the leftmost bit. For your purposes, the value of codepoint is enough. For example, bitPos <= 7 is equivalent to codepoint < (...


21

The code you've written is more or less a state machine, written the way that one might be constructed in assembly language. A technique like that technically works, but it doesn't scale well and you can wind up with problems that are extremely hard to debug. Your code only needs a small tweak to use the more traditional C-language way to implement a state ...


20

The goto debate is ancient, from the year 1966 when Edgar Dijkstra came up with a famous paper called "Go To Statement Considered Harmful". This was controversial and the debate is still going on to this day. Still, most of his conclusions are also valid to this day and most uses of goto is considered harmful spaghetti programming. However, there's ...


18

Welcome to Code Review, we wish you the best. General Observations Congratulations on getting this to work, one function that is 356 lines long and almost 17K of text is a bit large and very hard to code and debug. Complexity The function main() is too complex and should be broken into functions. A general rule of thumb in all programming is that a function ...


17

Keep names and enum values in sync enum algs and alg_names[] are declared quite a bit apart from each other. That makes it hard to spot if they are in sync. It is best to declare the enum and the array with the names right next to each other, and use designated initializers to ensure the compiler will enforce the correct order, like so: enum algs { FCFS, ...


17

Overall Observations An interpreter should be able to read from standard in as well as from a file, this would break the entire tape model. The user could also redirect an input file to standard in. If you are going to program in C then you need to get comfortable with pointers. In the case of file input I would use an algorithm that reads a line at a time, ...


17

I recommend some additional compiler warning options: gcc -std=c17 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses -Wpedantic -Warray-bounds -Wstrict-prototypes -Wconversion 252015.c -o 252015 252015.c:22:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes] ll_LinkedList* ll_create (); ^~~~~~~~~~~~~ 252015.c:60:16: ...


15

On the whole this is nicely done for an application made during your first week of C programming. The functions are generally reasonably-sized, the code is comprehensible and the design decisions are understandable given the size and purpose of the application. My remarks mostly center around improving the design to support new features and avoid bugs. The ...


15

(This didn't fit in the comment field, not really an answer, more like advice) I get the allure of rolling your own format and writing the code, you wanna do it right, you wanna squeeze every last ounce of performance out of the CPU, you wanna create something awesome, maybe even learn something along the way, but here's some advice from a professional ...


15

void main is a really bad habit. It must be int main. Always check what scanf returns. For example, try to enter a non-numeric input, and see your program entering the infinite loop. Avoid conio.h (and hence getch). It is very non-portable.


14

Here are some things that may help you improve your code. Use all required #includes The code uses memcpy, so it should #include <string.h>. It might still compile on your machine, with your compiler, but it's not portable. Think about potential errors As one of the comments correctly notes, if one of the entries has the value of zero, this line will ...


13

Return the condition When we test for equality, a == b is either 1 if they're equal or 0 if they're not (see C11 §6.5.8 Relational operators). Therefore we can simply return the expression instead of an if-else construct: bool fifo_is_full(struct fifo * fifo) { return fifo->elements == fifo->size; } bool fifo_is_empty(struct fifo * fifo) { ...


12

Watch your memory allocations and deallocations. In both cases, you've got defangIPaddr returning a const char * to heap-allocated memory, which needs to be freed by the caller... but it can't be freed, because free expects a non-const void* as its argument. Functions that return ownership-of-a-heap-allocation to the caller should (A) return char*, not const ...


12

General Observations There is a real lack of communications with the user of the program. There is no error checking performed on the possible input. While the code really doesn't do that much it is too complex and very hard to maintain. Communications With the User Unless the user knows exactly how to call the program, the program will crash with no ...


12

Design Your list includes the head of the list. struct ll_LinkedList { ll_LinkedListNode* head; }; You could make a lot of your code a lot simpler by including two other values (the tail and the length). struct ll_LinkedList { ll_LinkedListNode* head; ll_LinkedListNode* tail; size_t size; }; This would make sure you don't have to ...


12

Redundant code As the 4 functions are nearly identical, consider coding helper functions. At a minimum, I recommend a printf_prefix(const char *ansi_color, const char *pass); To form a helper function of the first part with a va_list parameter is tricky. See below. Time I'd code for a fixed width time // %d:%d:%d %02d:%02d:%02d Flush For the more critical ...


11

This program reads 4 byte codepoints (in BIG ENDIAN) from a file strictly called "input.data" and creates another file called "ENCODED.data" with the relative encoding in UTF8. Needless to say, that's a weird way of storing code points. I know UTF-16, but UTF-32BE (just the code point in big endian form) is not widely used, although Python seems to use ...


11

My requirement requires me to have indentation. I am unsure how to apply that indentation in my code. Just indent your C code exactly the same way you'd indent Python code. Start at the left margin (column 0), and then each time you "go in a level" (in the body of a function, or an if or while or for, or when breaking an expression across multiple lines), ...


11

Making shebang line more portable Fixing the perl binary to /usr/bin/perl in the shebang will not work if you are using perlbrew instead of the system perl. Using /usr/bin/env perl will be a more portable alternative. use strict and warnings pragmas to catch error at an early stage By adding warnings you will get quite a few warnings, as discussed below. ...


11

Please check it if you can, and give your opinion! Enable more warnings warning: this statement may fall through [-Wimplicit-fallthrough=] unused variables `int a, b, i;` Spell check funtion Do not re-enter main() Yes, it is possible, yet makes code review and debug a bear and hard to spin this code off into its own TicTacToe() function. Don't do that. ...


11

The SIMD code has type errors. The problem is currently a bunch of floats are read, assigned to a SIMDi anyway, added as floats (and remember, these were integers, reinterpreted as floats), assigned to a SIMDi again, and then stored as floats (into an array of integers). The type warnings are not just noise, it's also actually wrong: integers are being ...


11

Disclaimer I don't have a working C compiler on my work computer, nor do I have Xlib available to me (I'm also not familiar with it, at all). That being said, given that we know our image is a square, you can do this with a single loop. Use a simpler algorithm The basic idea is that we know each corner of our square, and then by stepping through the length ...


11

Only include the header files that you need Looking at tape.h, it only contains declarations and no definitions. As such, the header files only serve to bloat up the source code and increase compilation time. You should move them to tape.c. Use static methods if possible If I look at main.c, the only functions that are utilized are initializeTape, interpret ...


11

Anyway, I want to get tips and criticism (Especially about my format). I think the fixed-size 4-byte things are great, much better than text. They're easy to use both for humans and computers, the identifiers are easy to identify in a hex view, and nothing misaligns the data stream so it stays in nice columns in hex view as well. However, I don't think it's ...


11

Avoid code duplication Your code is more verbose than necessary, because you are repeating a lot of things unnecessarily, or write things in a more complex way than necessary. For example: (index + (index + length)) / 2 This looks really weird, and is actually equivalent to: index + length / 2 Which makes much more sense. Even with that change, that ...


10

could I get some tips on optimization, etc. Alignment loss free_ptr = (char*) free_ptr + size; simply increases the next available allocation to so many bytes later. This differs from malloc() whose allocations meets all possible system alignment needs. Either document that bump_alloc() does not provide aligned allocations or change code to do so. ...


10

Portability There is no guarantee that this code will be using ASCII so it would be better to use '0' rather than 48 which is something of a magic number. Using '0' makes it more readable and easier to understand. lc_atof Doesn't Handle String Termination or End Of Line Correctly This code doesn't handle a NULL terminated string or an end of line character. ...


Only top voted, non community-wiki answers of a minimum length are eligible