New answers tagged

1 vote

Bellman-Ford optimisation in C with Shortest Path Algorithm (SPFA) and Small Label first (SLF)

Concerning *alloc() discussion, (not a major issue with OP's code) Recommend: No cast Size to the referenced object, not type. Check result Lead any size ...
3 votes
Accepted

Tar implementation

What’s the Deal Here Tail recursion is an extremely useful pattern. You’d normally use it to transform a loop into an equivalent function that calls itself. All the state that carries over from one ...
  • 3,446
7 votes

Tar implementation

The recursion here is not tail recursion. A tail-call is one where no further processing is needed between the called function's return and that of the calling function (this means that the current ...
  • 67.7k
5 votes

Bellman-Ford optimisation in C with Shortest Path Algorithm (SPFA) and Small Label first (SLF)

We're missing a lot of includes. At least the following standard headers: ...
  • 67.7k
12 votes

Bellman-Ford optimisation in C

for (uint _ = 0; _ < nb_nodes - 1; _++) Maybe this was python code translated into C?!? Repect the norms of the community. Even though ...
  • 8,970
6 votes

Bellman-Ford optimisation in C

A few pieces of advice you can consider: As far as I'm aware, documentation comments are conventionally immediately before the declaration, not between the signature and the body of the ...
  • 1,546
3 votes

Compute the sum of even numbers between two values

Just some corner thoughts Overflow sumOnlyEvenNumbersInRange(INT_MAX, INT_MAX) incurs signed integer overflow with ...
6 votes

Compute the sum of even numbers between two values

The Math Pardon if you already know this, but: the usual way to solve for a closed-form solution for a series like this is to note that the sum of a series that grows at a linear rate will be ...
  • 3,446
8 votes
Accepted

Compute the sum of even numbers between two values

The first implementation mixes integer and floating-point arithmetic. I definitely would avoid that. The other two versions both use >> on a signed type (<...
  • 67.7k
5 votes

In-place recursive quick sort in C

Still looks pretty good! Beware Mixing Signed and Unsigned Integers Because of the default type-conversion rules, you could get bugs like the famous 3U < -1. ...
  • 3,446
4 votes
Accepted

Damerau-Levenshtein distance performance improvements

Please write this int min_int(int arr[], size_t size) { assert(size > 0); That will avoid UB, and give the caller a nice diagnostic if he messes up his ...
  • 8,970
4 votes

In-place recursive quick sort in C

No naked loops Every loop implements an important algorithm, and therefore deserves a name. In your case, the loop ...
  • 54.6k
3 votes

In-place recursive quick sort in C

General Observations Good use of the current C programming standard. The code is basically well written, but variable names could be improved, at least in main(). I ...
  • 21.9k
2 votes

Run command in a child process

Unnedded -1 No need to subtract 1. The fgets function reads at most one less than the number of characters specified by n from ...
1 vote

Solution to Codejam 2021 1C (Closest Pick) in C

We don't really need to store and sort the entire list of gaps. We're just interested in the largest gap we can cover with two tickets (that's max_gap_size) and ...
  • 67.7k
4 votes

Solution to Codejam 2021 1C (Closest Pick) in C

Common mistakes This echoes @Haris Enable more compiler warnings: As part of a code review, when such trivial errors are found so readily, it casts an overall disappointment on code. Avoid the 3 ...
3 votes

quadratic probing hashmap and its time efficiency

Minor stuff: IsPrime(1) is usually considered false ...
4 votes

Run command in a child process

Use posix_spawn Or possibly posix_spawnp. It’s in the POSIX standard precisely to take care of this. You don’t need to reinvent ...
  • 3,446
3 votes

quadratic probing hashmap and its time efficiency

Quadratic probing uses as the \$i\$th offset from the first probe index \$\pm 1^ic_2i^2+c_1i+c_0, c_2\ne 0\$ with parentheses inserted as deemed useful. ...
  • 5,363
7 votes

Run command in a child process

Code risks invoking undefined behaviour: if (argc != 2) { printf("Usage: %s <0|1|2|3>\n", argv[0]); return EXIT_FAILURE; } Dereferencing <...
  • 807
5 votes

Run command in a child process

We need to enable more compiler warnings - these problems are trivially fixed: ...
  • 67.7k
4 votes
Accepted

Solution to Codejam 2021 1C (Closest Pick) in C

Enable more compiler warnings GCC reports some warnings: ...
  • 807
3 votes

quadratic probing hashmap and its time efficiency

General Observations Interesting question! As the comments show, a lot of room for learning! Consistency! Some header files are using Doxygen Comments, other headers are not, some header files don't ...
  • 21.9k
6 votes

Max-heap implementation in C

The biggest issue I see here, and is not yet commented on, is the call to realloc for every insertion. When you remove an element from the heap, the ...
  • 4,302
1 vote

Create a structure to store student data and display all the records in the array

Additional thoughts to add about void AssignGrade(Student s[]): Avoid the hole As is, OP's code "works" with ...
5 votes

Max-heap implementation in C

Avoid FP math for an integer problem floor(log2(max_heap->size)) is overkill and at some level risks incorrect results with less than ideal FP functions. A ...
1 vote

Follow up: A chat server using the select() API

Undefined Behavior: ...
  • 807
0 votes

Create a structure to store student data and display all the records in the array

I'm just going to look at the AssignGrade() function. ...
  • 879
10 votes

Max-heap implementation in C

General Observations Since this might be a set of library routines, it might be a good idea to move all the heap related functions to their own .c file and have a ...
  • 21.9k
14 votes
Accepted

Max-heap implementation in C

This is a max-heap datastructure, and there is very consistent nomenclature here which reflects that. I'm sure it is clear and commendable. But I wouldn't have minded seeing it written in a comment, ...
  • 8,970
3 votes

Create a structure to store student data and display all the records in the array

General Observations The code might be more readable if the indentation was 4 spaces rather than only 2 spaces. Many programmers use a tab for indentation. It is also common to have spaces between the ...
  • 21.9k
8 votes

Create a structure to store student data and display all the records in the array

Undefined Behavior s[i].name[50] = ' '; This accesses outside of the array bounds. fflush(stdin); This is undefined behavior. ...
2 votes

Follow Up: struct sockaddr storage initialization by network format-string

Improve naming uniformity Instead of .... netstring_internal.h _INTERNAL_NETSTRING_H netstr_error NETSTR_PROTO protocol_table netstring_to_addrinfo.c DOMAIN_BUFSIZ ...
0 votes

Stack using doubly linked list

A stack should be a vector (that is, a dynamically-allocated array that remembers its size and capacity). Pop by decrementing the size. Push by reallocating if necessary and incrementing the size. ...
  • 3,446
1 vote

Array List C implementation

Give Your Structures More Informative Names This is a dequeue, not a list. The Java class named ArrayList doesn’t have ...
  • 3,446
1 vote

Array List C implementation

A few more points: In al_new you could use calloc to zero list->array and avoid the explicit loop to do so. I think you ...
  • 879
1 vote

Stack using doubly linked list

Consider a uniform name space Code such as this is not used in isolation, but with other code. The names chosen readily collide (like top) with existing code and ...
4 votes
Accepted

Binary Search Tree implementation in C [1]

Fix compilation warnings If your compiler isn't telling you about these, you haven't enabled enough warnings: BST* create_bst(void) { /*🔺🔺🔺*/ <...
  • 67.7k
7 votes

Follow Up: struct sockaddr storage initialization by network format-string

Two minor points about this bit: #if(_POSIX_C_SOURCE != 200112L) #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200112L #endif First, if you define ...
  • 3,446
7 votes

Follow Up: struct sockaddr storage initialization by network format-string

Declare only where needed: //long port; //port = strtol(*str, (char **) str, ...); long port = strtol(*str, (char **) str, ...); Simplify initialization: ...
  • 807
8 votes

Follow Up: struct sockaddr storage initialization by network format-string

There's a subtle mistake in this macro definition: #define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0])) When we put parens around the expansions of macro arguments, ...
  • 67.7k
9 votes
Accepted

Follow Up: struct sockaddr storage initialization by network format-string

Why not always use getaddrinfo()? I kept the version with struct sockaddr_storage anyway Why? It is inferior in every possible ...
  • 42.5k
4 votes
Accepted

Command line calculator in C

Avoid global variables for this task The key problem, in this case, is the added difficultly in proving code correctness. Alternative: In func(), instead of ...
7 votes

Command line calculator in C

Don't write clever code: if (printf("%g\n", b(&(const char *){*argv}, 0)) < 0) You know you’re brilliant, but maybe you’d like to understand what ...
  • 807
7 votes

Command line calculator in C

General Observations I'm going to address coding style here, since the code is almost unreadable. If you are interested in getting your code reviewed you may want to pay attention to what the ...
  • 21.9k
1 vote

Brute Force Algorithm in C

This part of the code has a common beginner error: ...
  • 67.7k
7 votes
Accepted

Basic recursive descent parser in C

Portable? "I wrote this code with portability in mind," --> Standard C lacks EINVAL. longjmp(jmp, errno = EINVAL); ...
3 votes

Recursive merge sort in C

This is a Great First Program in C I don’t think I’ve seen a better one. However, I’ll suggest some improvements. Write Contracts for your Functions These can be short and sweet; names like ...
  • 3,446
4 votes
Accepted

Vectorized 16-bit addition in Standard C

You Have an XY-Problem Let’s take a look at a very naïve loop to do this in C: ...
  • 3,446
3 votes

struct sockaddr_storage initialization by network format string

Candidate compare_until_delim() simplification ...

Top 50 recent answers are included