New answers tagged

4 votes
Accepted

Multithread execution times are slow on Ubuntu and fast on Windows

Use C++'s random number generators instead of rand() Not only is this good advice in general, it's actually the main source of your performance problems. You use <...
G. Sliepen's user avatar
  • 55.6k
5 votes

Observer pattern written in C++

Overuse of abstract base classes In your code, you have way too many abstract base classes. Most of them are not necessary. In fact, you only need a base class for the observer. Its ...
G. Sliepen's user avatar
  • 55.6k
2 votes

Given an array of N elements, answer Q queries asking if there exist two elements from two specified range with a given XOR value

As you observed, the naive approach scales poorly. Since we have one input array that's common to all the queries, it's worth spending some time turning it into a form that makes the queries faster ...
Toby Speight's user avatar
  • 75.1k
3 votes
Accepted

initializing a timezone database and getting all timezone names (before main())

The code looks correct and well presented. Most of my comments are matters of style. There are a lot of headers included, in a strange order. It would be easier to comprehend in alphabetical order. ...
Toby Speight's user avatar
  • 75.1k
2 votes

Given an array of N elements, answer Q queries asking if there exist two elements from two specified range with a given XOR value

Check for invalid input: int n,q; cin>>n>>q; for(int i =0;i<n;i++) { cin>>a[i]; } Input can fail, your code doesn't ...
Harith's user avatar
  • 5,824
6 votes
Accepted

C++ - Find all duplicate files (follow-up)

Error handling There are several things wrong with the way you handle errors. Error handling can be an annoying thing to do, but it is very important you do it correctly. Here are some of the issues: ...
G. Sliepen's user avatar
  • 55.6k
3 votes

Looking for feedback on a flavour of Radix tree I wrote. This is version 2 of the class original posted on this forum

That's a lot of code! This kind of large code review whould probably include tests to prove that it is working correctly. The complexity here is large enough that I can not spot that kind of thing ...
Martin York's user avatar
  • 95.1k
5 votes
Accepted

C++ - Find all duplicate files

There's a clear bug in that files are considered to be equal if they have the same MD5 hash. Since hash collisions are definitely a thing, files with equal hashes need to be further compared to ...
Toby Speight's user avatar
  • 75.1k
4 votes

Data Base Management System in C++ mimicking SQL based DBMS's (not for parsing SQL syntax)

Design review Beware of duplicated information Your entry type is basically a discriminated union: ...
indi's user avatar
  • 11.1k
3 votes

Data Base Management System in C++ mimicking SQL based DBMS's (not for parsing SQL syntax)

Separate your data structures from the query language I strongly recommend you separate your code into the parts that actually implement the data structures from the code that interprets statements. ...
G. Sliepen's user avatar
  • 55.6k
2 votes

Portable periodic/one-shot timer thread - follow-up

It would have been nice to have an example program to experiment with. I don't like the mix of durations and bare numbers (milliseconds) in the interface (or in the implementation, but that's a ...
Toby Speight's user avatar
  • 75.1k
2 votes

Convert program arguments to std::wstring

I think std::vector is not a good fit for this purpose. std::span would fit a lot better. Here are the features that ...
Incomputable's user avatar
  • 9,484
4 votes

Convert program arguments to std::wstring

Frame Challenge You probably don’t want to use wstring internally. MS Windows has supported UTF-8 since Windows 10 version 1803. On Linux, a wide character is ...
Davislor's user avatar
  • 7,371
1 vote

Convert program arguments to std::wstring

argv[0] can legally be a null pointer, depending on the platform and the way that your program is invoked. If it is null, then constructing a ...
Toby Speight's user avatar
  • 75.1k
1 vote

Simple TCP port scanner

If you want to scan thousands of ports per second it must be done using UDP packets instead of TCP, as UDP packets do not need to wait for confirmation that the packet was delivered successfully or ...
Amrita Bithi's user avatar
10 votes

Simple TCP port scanner

From atoi()'s man page: BUGS errno is not set on error so there is no way to distinguish between 0 as an error and as the ...
Harith's user avatar
  • 5,824
8 votes
Accepted

Simple TCP port scanner

Skipping blocked ports quickly It is possible to reduce the timeout for connect() when trying to connect to unresponsive ports, by setting the ...
G. Sliepen's user avatar
  • 55.6k
3 votes

Portable periodic/one-shot timer thread - follow-up

I have spotted one more scenario for deadlock in addition to calling clear from the timer. This scenario arises when the conditional wait timer is last one. In this case, the lock obtained by timer....
Vinod Kumar Singh's user avatar
5 votes

Dimacs parser written in C++

You have already recieved a review for the C++ part, so I'd not repeat that. fseek(file, 0L, SEEK_END); size_t filesize = ftell(file); The ...
Harith's user avatar
  • 5,824
5 votes
Accepted

Dimacs parser written in C++

As others have already mentioned, your implementation is more 'C with std::vectors` than C++. Don't use C headers <cstdio>, ...
Rish's user avatar
  • 2,286
1 vote

Alternative control flows to highlight happy-path

I use the following style for medical C code, where multiple returns and goto are forbidden: ...
canton7's user avatar
  • 216
4 votes

Alternative control flows to highlight happy-path

Are you open to extensions? One feature that standard C++ critically lacks is a way to create expressions that can embed control-flow instructions in a practical manner. GCC solves the issue elegantly ...
Matthieu M.'s user avatar
  • 5,137
3 votes

Scene Hierarchy with Translation, Rotation and Scale

One observation is the use of "world" vs "local". This appears to be leading to an inappropriate hierarchy, where the children insert themselves into their parent's business. The ...
John Deters's user avatar
3 votes

Alternative control flows to highlight happy-path

Just to add some more c++ style on the way the 17 version. Instead of ...
camp0's user avatar
  • 825
6 votes
Accepted

Alternative control flows to highlight happy-path

Some suggestions: Write code in paragraphs. Group related lines, and put empty lines between those groups in the same way that English text is written. If possible, avoid two stage initialization: <...
user673679's user avatar
  • 11.7k
5 votes

My C++ implementation of the Factory Pattern

You have multiple sources of undefined behaviour, I’m afraid. Before we get to that, though, we should talk about the pattern. Is the method part necessary? The “factory pattern” is often called the “...
indi's user avatar
  • 11.1k
6 votes
Accepted

C++ - Lexer for the Monkey Programming Language from the book "Writing An Interpreter In Go"

For someone new to C++ and OOP, your code is pretty good! Can you really lex one line at a time? Your Lexer only processes one line at a time, at least that's how ...
G. Sliepen's user avatar
  • 55.6k
5 votes
Accepted

Multithreading message queue system

Use wait() with a predicate I recommend that you always pass a predicate to wait() that checks whether the condition you are ...
G. Sliepen's user avatar
  • 55.6k
0 votes
Accepted

recursive_remove and recursive_remove_if Template Function with Unwrap Level Implementation in C++

Naming You named it recursive_remove_if(), but std::remove_if() does something very different from your code: it doesn't ...
G. Sliepen's user avatar
  • 55.6k
3 votes
Accepted

Print and compare structure comprising two integers

Neither function should be modifying the contents of the object, so they should both be declared const: ...
Toby Speight's user avatar
  • 75.1k
6 votes

C++ - Secretary Problem using dynamic programming

Minus the call to std::cout, you have a program that is all C. So why not use C and a C compiler instead of running almost C code under a C++ compiler? Martin ...
Harith's user avatar
  • 5,824
8 votes
Accepted

C++ - Secretary Problem using dynamic programming

Overview About your use of C-arrays: you need to justify this. Sure using them to learn is fine, but here you are not really using them in that way. In C++ we tend ...
Martin York's user avatar
  • 95.1k
6 votes
Accepted

Function composition in the context of data processing pipelines

Issues: There's a technical issue in the implementation is the design of the type trait InvokeResult. The problem lies in the usage of ...
ALX23z's user avatar
  • 2,328
3 votes

Optical flow implementation on esp32

transpose() makes a copy of its input The first parameter of transpose() is passed by value, which means it makes a copy of <...
G. Sliepen's user avatar
  • 55.6k
2 votes
Accepted

A recursive_copy_if Template Function Implementation with Unwrap Level Implementation in C++

Range is not just an input range This is something I missed in my reviews of your previous questions. The parameter input is ...
G. Sliepen's user avatar
  • 55.6k
4 votes
Accepted

My C++ Implementation of the Observer Pattern

About the observer design pattern The design patterns from the "Gang of Four" focus heavily on solving problems using classes. However, in modern C++, there are other ways to solve the ...
G. Sliepen's user avatar
  • 55.6k
3 votes

Multi Threaded High Performance txt file parsing

One performance idea: Do not use branching (if possible by any trick). It is possible to parse text without branching. Have a look at 1BRC (java code base for inspiration, but there is also C, rust, .....
LvargaDS's user avatar
2 votes

Image Rotation and Transpose Functions Implementation in C++

G. Sliepen already pointed out the three sheer technique, the need to iterate over the output pixels rather than the input, and the expense of doing large rotations 90 degrees at the time. There are a ...
Cris Luengo's user avatar
  • 5,573
5 votes
Accepted

Image Rotation and Transpose Functions Implementation in C++

Avoid trigonometric functions where possible Trigonometric functions like atan2(), sin() and ...
G. Sliepen's user avatar
  • 55.6k
5 votes

Rock Paper Scissors Game feedback

Issues: My main issue is your large piece of code to decide who wins: ...
Martin York's user avatar
  • 95.1k
3 votes

Rock Paper Scissors Game feedback

Your program is fine as I couldn't spot any logic flaws in it. However, making sure that there are no logic flaws took me a little more time than it would have done had you used the fact that: If <...
Ted Lyngmo's user avatar
3 votes

Rock Paper Scissors Game feedback

Your indentation is all over the place. Consider using GNU Indent or clang-format to automatically format it. What's the problem with "using namespace std;"?. When using C headers in C++, ...
Harith's user avatar
  • 5,824
4 votes
Accepted

Terminal based game: Part 3 (Using templated game)

Just make a runGame() function You want to avoid classes, but you are still introducing one here: a GameTemplate object is ...
G. Sliepen's user avatar
  • 55.6k

Top 50 recent answers are included