New answers tagged

2 votes

AES implementation in C

Also, don't "free" memory in a different function than where it was "malloc"ed. Memory allocations & deallocations should be done in a common function, or at least "...
MattyBoy's user avatar
3 votes
Accepted

Read a line from a stream

A real good get line function is tricky and sadly missing in standard C. Are there any bugs in the code? See below. Are there any edge cases I have missed? See below. Does the function provide ...
chux - Reinstate Monica's user avatar
1 vote

Simple rwlock implementation in C

asserts I see two of them, essentially saying that readers never goes negative. ...
J_H's user avatar
  • 22.1k
6 votes

Scientific Calculator

Imprecise pi Use at least DBL_DECIMAL_DIG significant digits. DBL_DECIMAL_DIG is commonly 17. Check input return values Detect ...
chux - Reinstate Monica's user avatar
1 vote

Simple rwlock implementation in C

Consider a scenario: A thread t1 calls unlock, and passes the if (lock->readers == 0) ...
vnp's user avatar
  • 56.3k
-1 votes

Simple rwlock implementation in C

Your lock implementation is completely wrong. You are acquiring the lock->mutex and then mistakenly releasing it in the same function. You should only release the lock->mutex in the unlock ...
cuddly_waffle's user avatar
6 votes
Accepted

Use of double pointers and memory allocation/deallocation

Allocating. The functions of *alloc family may fail. Always test their return values. ...
vnp's user avatar
  • 56.3k
4 votes

Thread-safe strtok in C - version II

Reconsider token_t This is currently a linear linked list of tokens, surprising given the name. A strictly-evaluated linear linked list isn’t an efficient data ...
Davislor's user avatar
  • 6,706
1 vote

Basic Brainfuck Interpreter in C

char c; should be int c; on line 23, because EOF might not be detected properly if you use a ...
FFmpegEnthusiast's user avatar
5 votes

Bare bones base 64 encoding/decoding

parallel construction convert_sextet(unsigned char sextet) ... deconvert_sextet(char c) We have some private helpers. The first name is fine. The second, less so. ...
J_H's user avatar
  • 22.1k
1 vote

Bare bones base 64 encoding/decoding

You could be aiming for Brevity, Terseness, compactness, smallest code, fewest branches, fastest code, most readable, etc.. Example. ...
jmullee's user avatar
  • 161
2 votes

Writing a HTTP server in C

The BSD standard provides some goodies in err.h. If the header is available on your system, you may be able to replace your custom ...
Harith's user avatar
  • 4,083
3 votes
Accepted

A simple unit test library for C - version II

In C, void get_exit_status() declares a function that takes an unspecified number of parameters, not a function that takes none. If the function expects no ...
Harith's user avatar
  • 4,083
6 votes

Thread-safe strtok in C

Significant semantic differences from strtok() I decided to practice and write a thread-safe version of the same function. Yours is not "a thread-safe ...
John Bollinger's user avatar
4 votes

Remove all text files with non-US-ASCII text encoding from current folder on Linux

Use nftw() to recursively iterate over the directory A lot of code you wrote is spent to recursively iterate over all the directory entries. That can all be avoided ...
G. Sliepen's user avatar
  • 54.1k
8 votes

Thread-safe strtok in C

Since this is not a drop-in replacement of strtok, which iterates token, but only allows a complete tokenization of a string, I'd not expose ...
vnp's user avatar
  • 56.3k
6 votes

Thread-safe strtok in C

You don't need the inclusion of stdlib.h in com_github_coderodde_thread_safe_strtok.h. For the definition of ...
Harith's user avatar
  • 4,083
7 votes

A simple unit test library for C

The header doesn't need to include anything except <stddef.h> (to define size_t). These two externs are never used: <...
Toby Speight's user avatar
  • 74.5k
6 votes

Remove all text files with non-US-ASCII text encoding from current folder on Linux

char current = fgetc (text_file_pointer); discards the difference between an error (int -1) ...
Peter Cordes's user avatar
  • 3,266
4 votes

Brainfuck to C transpiler

There are some undefined/implementation-defined behaviors of Brainfuck that you can define. The size of the cell array shall always contain at least 9999 cells. The behavior is undefined if: The ...
Harith's user avatar
  • 4,083
7 votes

Brainfuck to C transpiler

Advice I - roll your own my_basename When you fprintf(stderr, "usage: %s [input] [output]\n", argv[0]); I suggest you ...
coderodde's user avatar
  • 27.6k
8 votes

Remove all text files with non-US-ASCII text encoding from current folder on Linux

#define SUCCESS 0 #define FAIL -1 Better to include <stdbool.h> and use its defined ...
Toby Speight's user avatar
  • 74.5k
8 votes
Accepted

Remove all text files with non-US-ASCII text encoding from current folder on Linux

I'd have a function to check if a file is ASCII compatible (a positive check just to be sure that we don't get double negatives). Then if that's not the case I'd remove the file. Then I would have a ...
Maarten Bodewes's user avatar
7 votes
Accepted

Bare bones base 64 encoding/decoding

I'd not name the functions mini64_encode and mini64_decode. In the code the user doesn't care if this is a minimal ...
Maarten Bodewes's user avatar
3 votes

A simple Vector library in C

Use the type generic tgmath.h. You can then use fabs() for absolute value and sqrt() for square root for either ...
TrayMan's user avatar
  • 183
8 votes

First dynamic array in C

Your code does not implement a dynamic array. It never extends its memory when elements are added, so it overflows its bounds after a number of insertions, and proceeds to overwrite memory it doesn't ...
Toby Speight's user avatar
  • 74.5k
5 votes

A simple Vector library in C

Standard library <math.h> provides fabsf(), sqrtf() and ...
Toby Speight's user avatar
  • 74.5k
9 votes

A simple Vector library in C

Use the standard library where possible: stdlib.h defines these functions: ...
Harith's user avatar
  • 4,083
11 votes

A simple Vector library in C

Identifers beginning with an underscore and a capital letter are reserved by the compiler and standard library maintainers. So, technically, _VECTORS_H and ...
Rish's user avatar
  • 1,861
2 votes

Simple tree dictionary structure in C

While the answer by Harith addresses very valid points, I feel that there is some repetition in this code that deserves to be eliminated. In particular, you see the following pattern arise a lot: <...
madmann91's user avatar
10 votes
Accepted

Simple tree dictionary structure in C

Hide the implementation: Node should be an opaque data type defined in the source file. The header file would then only contain the forward declaration, and the ...
Harith's user avatar
  • 4,083
4 votes
Accepted

Count frequency of words and print them in sorted order

Some subtle concerns. Same but different This is an advanced concern that involves portability. Consider an output where multiple words have the same frequency as with input "the the the foo ...
chux - Reinstate Monica's user avatar
5 votes

Count frequency of words and print them in sorted order

The code looks clean and structured. It's something I'd enjoy working with, so this is fine. Of course, there are a few things that could be changed, so let's dive into these... Cleanup On Termination ...
uli's user avatar
  • 1,515
4 votes

Portable lambda emulation in C

This is terrible, for several reasons. Lambdas are anonymous functions. Here nothing is anonymous. It's not type-safe. While C is not that great with type safety to begin with, this actually erases ...
G. Sliepen's user avatar
  • 54.1k
2 votes
Accepted

Advent of Code 2023 day 1: Trebuchet (Part 1 and 2) Follow-up

Since Part 2 is mostly a superset of Part 1, I'll just review that. ...
Toby Speight's user avatar
  • 74.5k
5 votes

Advent of Code 2023 day 1: Trebuchet in C (part 2)

Do not trust user input: If there were no arguments supplied, argv[1] would be NULL, and the call to ...
Harith's user avatar
  • 4,083
2 votes
Accepted

Generic map-like function

This is Inflexible It always allocates an output buffer on the heap, which the caller must free(). It cannot write to a buffer allocated by the caller. It cannot ...
Davislor's user avatar
  • 6,706
6 votes
Accepted

Advent of Code 2023 day 1: Trebuchet in C (part 2)

It's normal to write main() as the last function, so there's no need to forward-declare getDigts(). We would normally make that ...
Toby Speight's user avatar
  • 74.5k
3 votes

Generic map-like function

I'm concerned that this is too inflexible, since we can't use a function that operates on user-defined type. I'd be inclined to stick with the style of interface that traditional C (without generics) ...
Toby Speight's user avatar
  • 74.5k
1 vote

Fast Hardy-Weinberg equilibrium simulation

Indent your code to show its nesting. Although all whitespace is identical as far as the compiler is concerned, it conveys a lot of information to the more important (human) reader. It's not legal ...
Toby Speight's user avatar
  • 74.5k
3 votes

Beginner's attempt at TicTacToe

We can easily change the order of functions so we don't need to forward-declare so many (possibly none at all). That's why C programmers expect to find main() last ...
Toby Speight's user avatar
  • 74.5k
4 votes
Accepted

Beginner's attempt at TicTacToe

First thing that comes to mind is to remove goto (see SO question): checkMove you only need to check a single cell not up to 9 ...
depperm's user avatar
  • 925
3 votes

Check if options are valid to create FAT12 image

fat.h has no need to #include <stdio.h>, so remove that. However, it does require ...
Toby Speight's user avatar
  • 74.5k
2 votes
Accepted

Fetch a Quranic Verse from the Web

Just a strtoi() review. Bug: errno setting/restoration woes. errno = 0; should be done ...
chux - Reinstate Monica's user avatar
2 votes

Fetch a Quranic Verse from the Web

General Observations There are definitely some good points to this code, for example rather than write your own code to do the curl functions or the JSON manipulation you used libraries. That is ...
pacmaninbw's user avatar
  • 23.8k
1 vote

A simple shell in C

The #ifdef here adds no value: #ifdef _POSIX_C_SOURCE #undef _POSIX_C_SOURCE #endif It's perfectly permissible and normal to <...
Toby Speight's user avatar
  • 74.5k
1 vote

Binary tree struct in C

I like • not adding a parent field to node • not re-implementing "conditional descent" in ...
greybeard's user avatar
  • 6,286
3 votes
Accepted

A simple shell in C

Good job! It definitely lives up to its name of "minimal shell". And while a small codebase doesn't always translate to a bug-free codebase, there's certainly fewer places for bugs to hide. ...
J_H's user avatar
  • 22.1k

Top 50 recent answers are included