42

I decided to find out if there is practical sense in writing ++iterator instead of iterator++ when handling iterators. My interest in this question arouse far not from my love to art but from practical reasons. We have intended for a long time to develop PVS-Studio not only in the direction of error search but in the direction of prompting tips on code optimization. A message telling you that you'd better write ++iterator is quite suitable in the scope of optimization.

But how much relevant is this recommendation nowadays? In ancient times, for instance, it was advised not to repeat calculations. It was a good manner to write:

TMP = A + 10;
X = TMP + B;
Y = TMP + C;

instead of

X = A + 10 + B;
Y = A + 10 + C;

Such subtle manual optimization is meaningless now. The compiler would handle this task as well. It's just unnecessary complication of code.
Andrey2008 30 january 2012, 13:18

Copy-Paste, Ctrl-C, Ctrl-V
I create the PVS-Studio analyzer detecting errors in source code of C/C++/C++11 software. So I have to review a large amount of source code of various applications where we detected suspicious code fragments with the help of PVS-Studio. I have collected a lot of examples demonstrating that an error occurred because of copying and modifying a code fragment. Of course, it has been known for a long time that using Copy-Paste in programming is a bad thing. But let's try to investigate this problem closely instead of limiting ourselves to just saying "do not copy the code".
Andrey2008 19 january 2012, 18:52

"C++11 feels like a new language." - Bjarne Stroustrup

Recently, Herb Sutter has opened on its website a new page “Elements of Modern C++ Style”, where he describes the advantages of the new standard and how they will affect the code.

C++11 standard introduces many new features. Next, we focus specifically and only on those features that make C++11 really feel like a new language compared to C++98, namely:

• They change the styles and idioms you will use when writing C++ code, often including the way you will design C++ libraries. For example, you will see more smart pointer parameters and return values, and functions that return big objects by value.
• They will be used so pervasively that you will probably see them in most code examples. For example, virtually every five-line modern C++ code example will say “auto” somewhere.
Tags: C++, C++0x, С++11
Skull 13 january 2012, 17:45

Introduction


C ++ is unmanaged language, because the programs can escape without saving the user’s data and give the error messages, etc. For example, it takes only to get into an uninitialized memory. For example:

void fall()
{
char * s = "short_text";
sprintf(s,"This is very long text");
}

or

void fall()
{
int * pointer = NULL;
*pointer = 13;
}
It would be better if we could "catch" a program crash just like in java we are catching exceptions, as well we could do anything before the program will crash (save the user’s document, display a dialog with the error message, etc.)

A task does not have a general solution, as C ++ does not have its own model of exception handling that is work-related with the memory. Nevertheless, we will consider two methods that are using the features of the operating system that caused an exception.
Skull 25 december 2011, 13:26

Unlike most assemblers, the result of multiplication has the same number of digits as the multipliers in C / C + +. In order not to lose the accuracy, sometimes it is needed to perform additional operations for the multiplication.
Let us consider the task. The system determines the time since starting the program in the microprocessor’s ticks by calling the function:

unsigned long long getTickCount ();
Length unsigned long long - 64 bits. Converting to the physical units of time in the system, there is a constant:

const unsigned long long TICKS_PER_SECOND = 1999000000ULL;
It is required to determine the function of transferring ticks in a nanoseconds getNsec (unsigned long long ticks) with semantics:

unsigned long long getNsecNaive (unsigned long long ticks) {
static const unsigned long long NSEC_PER_SECOND = 1000000000ULL;
unsigned long long nsec = NSEC_PER_SECOND * ticks / TICKS_PER_SECOND;
return nsec;
}
Pirat 22 september 2011, 11:17
1 2 3