Hot answers tagged

13 votes

Optimise a program that outputs the earliest date

The first thing I would do would be to break the code up into some meaningful functions instead of having one long stream of code in main. Personally, I'd imagine <...
Jerry Coffin's user avatar
  • 33.5k
12 votes

Image Processing Application in C

Enable more compiler warnings: ...
Harith's user avatar
  • 2,354
11 votes

Image Processing Application in C

Size to the object, not type Rather than sizeof(Pixel), use sizeof image->pixels[0]. It is easier to code right, review and ...
chux - Reinstate Monica's user avatar
10 votes
Accepted

Image Processing Application in C

Let's talk about the API. I've worked with multiple imaging libraries over the past ~25 years, and have significant experience designing them too. API: Let's start with the image container, the core ...
Cris Luengo's user avatar
  • 5,178
8 votes

Boolean conversion for fscanf()

Ooh, unit tests! You know how to make me happy! Let's look at the tests first, as that's valuable documentation of the expected use cases. A purist might argue that these are not unit tests because ...
Toby Speight's user avatar
8 votes

char* to integer implementation (atoi with saturation to INT_MIN or INT_MAX on overflow)

The code is not bad and easy to follow. Here are some suggestions: Use pointers rather than indexing The i variable isn't really needed. Instead, you could simply ...
Edward's user avatar
  • 65.3k
8 votes
Accepted

C Bit Utility Functions: Popcount, Trailing Zeros Count, Reverse All Bits

These look familiar to me, but they are apparently "different enough" that GCC and Clang fail to recognize them as idioms. Well, the bit-reversal comes out about as well as can be expected ...
harold's user avatar
  • 9,129
8 votes

Optimise a program that outputs the earliest date

else if (month == earliest_month) Repeatedly testing each component is tedious and error prone. Dealing with a 3-tuple is less convenient than a single ...
J_H's user avatar
  • 17.1k
8 votes

Optimise a program that outputs the earliest date

Don't ignore runtime errors: What happens if the user enters something that's not a valid date format (e.g. cnaisbciasvbiqe)? scanf() will return 0, but we treat ...
Harith's user avatar
  • 2,354
5 votes

Optimise a program that outputs the earliest date

Since you asked for suggestions / optimizations: You can map y/m/d tuples to 32-bit integers that monotonically increase with increasing dates. This reduces the problem to finding the ...
Peter Cordes's user avatar
  • 2,966
5 votes
Accepted

char* to integer implementation (atoi with saturation to INT_MIN or INT_MAX on overflow)

Uncommon bug: is...() is...() expects to receive an int with a value in the ...
chux - Reinstate Monica's user avatar
5 votes

Image Processing Application in C

The other contributors made an excellent review of your code. I just want to comment on the philosophy or rationale behind this library choices. Let's talk about the aim of computing a Sobel's filter: ...
Costantino Grana's user avatar
4 votes

Simple Key Value Database using C

Review .h include needs Do not rely on other code to include the files for a given .c, .h file. set.h needs <stdio.h> for <...
chux - Reinstate Monica's user avatar
4 votes

Image Processing Application in C

I second Cris Luengo's recommendations especially regarding API design. Additionally: Back in the day I wrote C, it was frowned upon to make a function that allocates space and does something, without ...
Zizy Archer's user avatar
4 votes

Image Processing Application in C

Everyone has pretty good answers here already. But I'd like to add - There's no concept of a negatively sized image, so consider use unsigned integer sizes, or ...
Kingsley's user avatar
  • 161
4 votes

C Element based FiFo-Buffer Implementation

Naming things The names you give to types and functions are very long. Maybe if you have an editor or IDE that allows tab completion it is not so bad, but having to type the 48 characters of ...
G. Sliepen's user avatar
  • 50.6k
4 votes

Simple hash table written in C with REPL

Unnecessary casts You cast a lot of value unnecessarily, sometimes even to the type they already have! Casting should only be done if really necessary. Some things that don't need casting: ...
G. Sliepen's user avatar
  • 50.6k
3 votes

Optimise a program that outputs the earliest date

I have only a few suggestions. Many other good ideas are in the other answers. Rather than initializing the earliest_* family to 0, initialize them to an impossibly large value (like ...
David G.'s user avatar
  • 1,248
3 votes
Accepted

another Merge Sort

Three things pop out to me: This is not a stable sort. Probably does not matter, as you currently only support integers. This only support integer sorts. If you look at the standard qsort you will ...
Martin York's user avatar
  • 94.3k
3 votes

Simple Key Value Database using C

Seems you are looping through index to get to your key position...at this point id suggest looking at hashtables/dictionaries, since this is what first comes to mind when talking about key-value data ...
Perdi Estaquel's user avatar
2 votes

bucketsort with multiprocessing

bucketSort() causes a segmentation fault Your calculation of index in bucketSort() is wrong, ...
G. Sliepen's user avatar
  • 50.6k
2 votes

bucketsort with multiprocessing

Enable more compiler warnings: ...
Harith's user avatar
  • 2,354
2 votes
Accepted

Iterating the octree while keeping track of the positive neighboring nodes

Using the right tool for the job When I first wrote this, I insisted on using an iterative approach, because I was rote taught that iteration is faster than recursion. However, an octree is a ...
user16217248's user avatar
2 votes

Library for managing CLI flags

The pragma directive is non-portable: Though supported by most modern compilers, it is not standard C. Consider using include guards: ...
Harith's user avatar
  • 2,354
2 votes

char* to integer implementation (atoi with saturation to INT_MIN or INT_MAX on overflow)

Not an answer but a sample test harness using standard library code. Adjust to ignore errno compare. ...
2 votes

C Bit Utility Functions: Popcount, Trailing Zeros Count, Reverse All Bits

Small stuff: Missing include Add #include <stdint.h>. Inverse? Rather than inverse, which can sound like a bit-wise complement, consider bit_reverse or the ...
chux - Reinstate Monica's user avatar
2 votes

Image Processing Application in C

You have some great reviews of the C code, so I'll look at the compilation script: #!/bin/bash gcc improc.c main.c -O3 -g -Wall -Wpedantic -lm -Wextra -o main ...
Toby Speight's user avatar
1 vote

Optimise a program that outputs the earliest date

Many of the answers consider packing the year, month and day into a single integer. Usually when I do this, I use decimal values (year * 100 + month) * 100 + day ...
Pete Kirkham's user avatar
1 vote

another Merge Sort

Code risks invoking undefined behavior: None of the calls to malloc() have their return values checked. ...
Harith's user avatar
  • 2,354
1 vote

another Merge Sort

Is there any other good practices that I've missed to follow in my try? Sort stability Agree with @Martin York code has stable sort concerns. A stable sort keeps the array elements in the same order ...
chux - Reinstate Monica's user avatar

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