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 <...
12
votes
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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: ...
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 <...
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 ...
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 ...
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 ...
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:
...
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 ...
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 ...
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 ...
2
votes
bucketsort with multiprocessing
bucketSort() causes a segmentation fault
Your calculation of index in bucketSort() is wrong, ...
2
votes
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 ...
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:
...
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.
...
Community wiki
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 ...
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
...
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 ...
1
vote
another Merge Sort
Code risks invoking undefined behavior:
None of the calls to malloc() have their return values checked.
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
c × 3914beginner × 552
performance × 530
strings × 392
algorithm × 322
linked-list × 257
memory-management × 184
array × 180
programming-challenge × 152
linux × 135
io × 131
reinventing-the-wheel × 125
parsing × 123
file × 120
pointers × 108
sorting × 100
homework × 93
multithreading × 84
c++ × 79
integer × 79
formatting × 77
bitwise × 73
game × 71
hash-map × 71
console × 70