Pull to refresh
231.56

C++ *

General-purpose programming language. It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation

Show first
Rating limit
Level of difficulty

How to send messages over sockets and create your own messanging protocols in C++

Level of difficulty Medium
Reading time 28 min
Views 2.8K

Network programming in C++ can be challenging. But even a greater challenge is to find educational content that will arm you with the knowledge on how to apply your networking skills in real applications.

In this article you can learn the basics of socket communication and many ways how you can design your internal messaging protocols.

Read more
Total votes 2: ↑2 and ↓0 +2
Comments 3

High-performance network library using C++20 coroutines

Level of difficulty Medium
Reading time 17 min
Views 16K

Asynchronous programming is commonly employed for efficient implementation of network interactions in C++. The essence of this approach lies in the fact that the results of socket read/write functions are not immediately available but become accessible after some time. This approach allows for loading the processor with useful work during the wait for data. Various implementations of this approach exist, such as callbacks, actors, future/promise, coroutines. In C++, these implementations are available as libraries from third-party developers or can be implemented independently.

Coroutines are the most challenging to implement as they require writing platform-dependent code. However, the recent version of the C++ 20 language standard introduces support for coroutines at the compiler and standard library levels. Coroutines are functions that can suspend their execution, preserving their state, and later return to that state to resume the function's work. The compiler automatically creates a checkpoint with the coroutine's state.

For a comprehensive understanding of C++ 20 coroutines, refer to this article. Below, we examine a code example using coroutines and describe important points applied during implementation.

Read more
Total votes 6: ↑4 and ↓2 +2
Comments 14

Writing an interpreter (virtual machine) for a simple byte-code + JIT compilation

Level of difficulty Medium
Reading time 10 min
Views 1.2K

There are two articles on Russian, the author of which writes a virtual machine (interpreter) for executing a simple bytecode and then applies different optimizations to make this virtual machine faster. Besides that, there is a compiler of a simple C-like language into this bytecode. After reading this article and getting familiar with the compiler, I thought that it would be interesting to try writing a virtual machine for this language that would be able to apply JIT-compilation to this bytecode with the libjit library. This article describes the experience of doing that.

I found several articles online that describe the usage of this library, but those that I saw, describe the compilation of concrete programs with libjit, while I was interested in compiling arbitrary bytecode. For people interested in further reading, there is an official titorial, a series of articles and a series of comparisons (in Russian).

The implementation was done in C++ because we aren`t playing games here. All my code is in my repository. The "main" branch has just the interpreter of the PigletVM bytecode; "labels-with-fallbacks" has a partial JIT compilation implementation (that doesn`t support JUMP instructions), "full-jit" has fully working JIT-compilationl; "making-jit-code-faster" makes code generated by JIT work faster and "universal-base-vm*" branches merge the interpreter and JIT-compilation implementations, by implementing a base generalised executor, which can be used for different implementations of PigletVM (both the interpreter and libjit compilation)

Read more
Total votes 3: ↑3 and ↓0 +3
Comments 10

Review of mini-book «60 terrible tips for a C++ developer»

Level of difficulty Easy
Reading time 6 min
Views 1.2K

I wrote a small e-book about terrible tips for C++ developers. Actually, it describes bad programming practices and explains why it's better to avoid them. However, every chapter of this mini-book starts with a terrible tip — just for fun.


60 terrible tips for a C++ developer


By the way, these tips may seem artificial but believe me, they are based on the real experience. In other words, the described terrible tips occur in developers' lives — that's why it's worth discussing them. First of all, this book will be useful for junior developers. But more skilled C++ developers can also find interesting and useful tips.


Even though it's a mini-book, it clearly does not fit into the Habr format. Too many words. So, I decided to write here the review. Here is the link to find the full version of the mini-book: 60 terrible tips for a C++ developer.


If you still hesitate whether to read it or not, below you will find a list of terrible tips that will be discussed in the mini-book.


View the terrible tips:

Read more →
Total votes 10: ↑7 and ↓3 +4
Comments 3

Hashing and its C++ applications

Level of difficulty Medium
Reading time 6 min
Views 3.8K

Hash, salt, SHA-1, SHA-2, std::hash.. To a non-programming person that may come up as some kind of a recipe that just does not seem to add up. In a sense, this is indeed supposed to be a gibberish to any third party and a strong, helpful mechanism for us, programmers. 

At the start of writing this article, I had one clear idea to get across the table: to finally unveil this mystery of hashing in C++ for beginners. I, a beginner myself, also wanted to solidify my knowledge in this area; so let’s get started.

Read more
Total votes 2: ↑2 and ↓0 +2
Comments 0

Exploring a possible implementation of non-blocking IO by writing a server on pure syscalls

Reading time 11 min
Views 2.1K

How do people usually write a server if they don't really care about performance? A program starts, then starts accepting incoming connections from clients and starts a new thread for each client, which is engaged in servicing this client. If you use framework, like Spring or Flask or Poco there, then it does something like this inside itself - the only difference is the threads can be reused, that is, taken from a certain pool. It's all quite convenient, but not too effective (and Spring is bad). Most likely, your threads serving clients do not live long and most of the time they are waiting either to receive data from the client or to send it to the client - that is, they are waiting for some system calls to return. Creating an OS thread is quite an expensive operation, as is context switching between OS threads. If you want to be able to serve a lot of customers efficiently, you need to come up with something else. For example, callbacks, but they are pretty inconvenient (though there are different opinions on this).

Another option is to use non-blocking I/O in combination with some kind of implementation of user-space threads (fibers). In this article I will show you how to write all this with your own hands.

Read more
Total votes 1: ↑1 and ↓0 +1
Comments 2

On the difference between regular functions and Lambdas

Level of difficulty Medium
Reading time 11 min
Views 2.5K

The point of this article is to explore Lambda functions, their dirrerences from regular functions and how they are implemented, based on C++, Python and Java programming languages.

Throughout this article I will be using godbolt.org to compile code and see machine code or byte code.

Read more
Total votes 2: ↑2 and ↓0 +2
Comments 2

Technical Note. From C++1998 to C++2020

Reading time 1 min
Views 1.7K

This technical note is devoted to covering information regarding all primary C++ programming language standards: C++03/98/11/14/17/20.

I am glad to share a technical note with some details regarding the C and all primary C++ programming language standards based on my experience and materials from the Reference Section of this document.

As of August 15, 2022, this technical note in PDF format consists of 72 pages.

Read more
Total votes 6: ↑2 and ↓4 -2
Comments 2

Electron + web camera (cpp-ffmpeg)

Reading time 8 min
Views 3.5K

An example of using Electron + React JS and a native ffmpeg addon to access a webcamera

This guide may be helpful to someone who is trying to find a way
to work with Electron if they need to use a c++ library or code

I was looking for a more realistic example than a simple 'hello world' and i didn't succeed

Here are the links in advance:

- electron - https://github.com/khomin/electron_camera_ffmpeg

- addon - https://github.com/khomin/electron_ffmpeg_addon_camera

So let me share my experience...

Read more
Total votes 3: ↑3 and ↓0 +3
Comments 0

How PVS-Studio prevents rash code changes, example N4

Reading time 2 min
Views 1.1K

Blender, PVS-Studio, std::clamp
If you regularly use a static code analyzer, you can save time on guessing why the new code doesn't work as planned. Let's look at another interesting error — the function broke during refactoring, and no one noticed that. No one — except for PVS-Studio that can automatically scan the project and email the report to us.

Read more →
Total votes 2: ↑1 and ↓1 0
Comments 0

PVS-Studio checks the code of Flipper Zero dolphin

Reading time 12 min
Views 1.6K

Flipper Zero + PVS-Studio


Flipper Zero is an open-source multi-tool for geeks and penetration testers. It so happened that the Flipper Zero project and the PVS-Studio analyzer crossed paths. A philosophical question: should we check the project, if the project developers have already started fixing errors? Let's try to do this.

Read more →
Total votes 6: ↑5 and ↓1 +4
Comments 0

Authors' contribution