O'Reilly logo

Efficient C++ Performance Programming Techniques by David Mayhew, Dov Bulka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Useless Computations

One programming habit you may encounter in practice is to zero out large data structures automatically. You can do that by calling calloc() to allocate a zero-filled memory block, or you can do it yourself by invoking memset(void *block, 0, int blockLen).

In the Web server we used a buffered socket object to store incoming requests:

class BufferedStreamSocket {
private:
    int   sockfd;              // Socket descriptor
    char  inputBuffer[4096];
     ...
};

The BufferedStreamSocket constructor automatically zeros out the input buffer:

BufferedStreamSocket::BufferedStreamSocket(...)
{
    memset(buffer,0,4096);
    ...
}

We are not generally opposed to the memset() call. It has its place. We are opposed to it only when it achieves nothing.

A close inspection ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required