Hot answers tagged

12 votes

C++11 Custom Vector Implementation

This is not a bad attempt at making a vector-like container, except it is lacking support for const and makes unnecessary copies (see below for a more detailed ...
user avatar
  • 34.8k
9 votes

C++ template class to calculate CRC

Here are some things that may help you improve your code. Understand the nature of templates In modern C++, putting headers in a header file and the implementation in a ...
user avatar
  • 64.5k
8 votes

C++11 Custom Vector Implementation

T internal_buffer[BUFFER_SIZE]; This isn't what you want for types that are expensive to construct, or can't be default-constructed. Your interviewer probably ...
user avatar
  • 58.1k
7 votes

C++11 Custom Vector Implementation

This isn't bad, but it's not stellar either. Names matter. Stable is a very strange name here. It evokes pointer/iterator stability, a term used to indicate that ...
user avatar
  • 3,503
7 votes

C++11 Custom Vector Implementation

Debug-code The proper destination for debug-output is std::clog (which goes to STDERR by default, ...
user avatar
  • 17.7k
6 votes
Accepted

Custom implementation to provide an immutable range of a vector without copying it

You are implementing std::span What you are trying to implement is basically C++20's std::span. It might be worthwhile to look ...
user avatar
  • 34.8k
6 votes
Accepted

Time duration conversion with units

Avoid defining your own constants There's no need to use your own constants for the duration of a millisecond, second, minute and so on; just use the right ...
user avatar
  • 34.8k
6 votes

Time duration conversion with units

Looks pretty good; these comments are mainly nitpicking: Error messages should be streamed to std::cerr, not std::cout. Whilst ...
user avatar
  • 58.1k
5 votes

C++ template class to calculate CRC

use a function, not a class A CrcCal class instance has no state (non-static member variables). The class therefore has no reason to exist. We can implement the ...
user avatar
5 votes

Better rand() API

It's not clear to me why we need to downgrade from std::random_device to a timestamp-based seed on Intel platforms. If we have good reason to distrust a particular ...
user avatar
  • 58.1k
4 votes

std::vector<bool> workaround in C++

If you aren’t reinventing the wheel as a learning exercise, and want to code this productively, the simplest options are to use the Boost version of ...
user avatar
  • 2,293
4 votes

C++11 Custom Vector Implementation

if(0u < used_elements) This is confusing to read and takes more time to understand than if(used_elements > 0u) I can ...
user avatar
  • 1,158
4 votes

How to avoid excessive for loops in C++

Documentation Add documentation. I had no idea what the goal of this program is, going in, and at the end of the review I still don't, so I can't comment on whether the code is correct or efficient. ...
user avatar
4 votes
Accepted

Accessing multiple dynamic libraries with the same extern C methods

The code looks correct. It looks like a weird mix of C and C++ though. Assumptions There was a statement that they share the same base class, but I am not sure what it means if they all use C style ...
user avatar
  • 9,171
4 votes
Accepted

C++ template and inheritance - Generic Sqrt Decomposition

Design review Your design is mostly sound. It can be simplified a little bit, and optionally made a bit more rigorous, but the basic design is not bad. The name for what you’re doing is the “Policy” ...
user avatar
  • 8,626
3 votes

Accessing multiple dynamic libraries with the same extern C methods

...
user avatar
  • 11.1k
3 votes

Custom implementation to provide an immutable range of a vector without copying it

You are asking how you can guard against invalid iterators; I assume you are most concerned about iterators that have become invalid after the vector had to re-allocate when it outgrew its capacity. ...
user avatar
3 votes

How to avoid excessive for loops in C++

General points Why does compareDNA return a double when the inputs are both ints and the output thus can only ever be an int. ...
user avatar
  • 20.3k
3 votes
Accepted

C++11(14/17/20) Thread Management

Thread management in C++20 In C++20 (which you mention in the title of the question), there is std::jthread which basically does most of what you are doing in your ...
user avatar
  • 34.8k
3 votes
Accepted

AEC-to-WebAssembly compiler in C++

How to create an alias for an enum I see you are declaring 5 constants, like i32, to avoid having to write out a whole ...
user avatar
  • 34.8k
3 votes

std::vector<bool> workaround in C++

Makefile review: All makefiles should have .DELETE_ON_ERROR: to ensure erroneous output doesn't satisfy dependencies. The targets ...
user avatar
  • 58.1k
3 votes

Better rand() API

Some things to think about: different PRNG algorithms have different trade-offs. The MT19937 algorithm is very fast, but it has a huge internal state (19937 bits to be exact, which is how it got its ...
user avatar
  • 34.8k
3 votes
Accepted

Lock-free, thread-safe trie container

Make it more generic Your Trie class is templated on the type of values stored in the trie, but it's hardcoded what the arity is. Now you can only use it for tries ...
user avatar
  • 34.8k
3 votes

Count the number of objects created statically and dynamically in C++

malloc is used but never defined. You probably want std::malloc, from the <cstdlib> ...
user avatar
  • 58.1k
2 votes

Implement 3 stacks using a single array in C++

Don't use three separate named variables for the three stack tops. You should be able to have a single indexable piece of code when doing the pushing/popping rather than a ...
user avatar
  • 11.1k
2 votes

Still an observer pattern?

As for the Subject class it makes sence to replace std::vector by std::set, which would prevent from adding the same Observer twice or even more times also this makes removing an Observer much easier.
user avatar
  • 373
2 votes

Event system using callback functions in C++

I'm going to update to C++20 as we go along, beginning with using modern Standard Library rather than Boost. We need to include some headers: ...
user avatar
  • 58.1k
2 votes

C++11(14/17/20) Thread Management

Pass arguments by reference You are passing SharedData by a pointer. It would have been much cleaner to pass it by reference: ...
user avatar
  • 2,262
2 votes

Simple terminal TicTacToe

Looking at the headers first, I see that term.h is missing include guards, and colors.h is using a reserved identifier for its ...
user avatar
  • 58.1k
2 votes
Accepted

Decorator Pattern with member functions

Without knowing the context, it’s impossible to say whether this design is a good idea. It seems over-engineered, but…. 🤷🏼 I will point out that you could get the same effect much, much easier, by ...
user avatar
  • 8,626

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