All pastes #911016 Raw Edit

Miscellany

public cpp v1 · immutable
·published 2008-02-20 12:41 UTC
rendered paste body
#include <QByteArray>#include <string.h>class CharStarKeeper{    char *ptr;public:    CharStarKeeper() : ptr(0) { }    ~CharStarKeeper() { clear(); }    void copy(char *other, int len)    {        delete[] ptr;        ptr = new char[len];        memcpy(ptr, other, len);    }    void clear()    { delete[] ptr; ptr = 0; }};class ByteArrayKeeper{    QByteArray array;public:    void copy(const QByteArray &other)    { array = other; }    void clear()    { array.clear(); }};int main(){    static const char message[] =        "This is the message that is copied around "        "and around. The whole point is that I will "        "create a QByteArray object or a const char* "        "to hold this, then copy it, then delete it.\n"        "No more, no less";    static const int len = sizeof message;    CharStarKeeper k1;    for (int i = 0; i < 1000; ++i) {        char *ptr = new char[len];        memcpy(ptr, message, len);        k1.copy(ptr, len);        delete[] ptr;        k1.clear();    }    ByteArrayKeeper k2;    for (int i = 0; i < 1000; ++i) {        QByteArray array(message, len - 1);        k2.copy(array);        k2.clear();    }}