[ create a new paste ] login | about

Link: http://codepad.org/PYXHLYnN    [ raw code | output | fork ]

C++, pasted on Oct 8:
#include <new>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>

void* operator new (std::size_t s) throw (std::bad_alloc)
{
    void* const p = std::malloc(s);
    assert(p != 0);
    std::printf("opnew: %p, %u bytes\n"
        , p, static_cast<unsigned int>(s));
    return p;
}

void operator delete (void* p) throw ()
{
    std::printf("opdel: %p\n", p);
    std::free(p);
}

struct UserData
{
    int i; char c; double r; void* p;
    void (*fp)(); void (UserData::*mfp)();
    std::string s;
};

int main()
{
    int const N = 100;
    std::puts("Testing std::vector");
    {
        std::vector<UserData> v(N);
        std::printf("instance v: %u bytes\n"
            , static_cast<unsigned int>(sizeof(v)));
    }
    std::puts("");
    std::puts("Testing array new");
    {
        UserData* p = new UserData[N]();
        std::printf("instance p: %u bytes\n"
            , static_cast<unsigned int>(sizeof(p)));
        delete [] p;
    }
    std::puts("");
    return 0;
}


Output:
opnew: 0x8050078, 8 bytes
opnew: 0x80500a0, 20 bytes
opnew: 0x80500d8, 112 bytes
opnew: 0x8050168, 112 bytes
opnew: 0x80501f8, 48 bytes
opnew: 0x8050248, 2 bytes
opdel: 0x8050248
opnew: 0x8050268, 8 bytes
opnew: 0x8050290, 20 bytes
opnew: 0x80502c8, 112 bytes
opnew: 0x8050358, 112 bytes
opnew: 0x80503e8, 48 bytes
opnew: 0x8050248, 2 bytes
opdel: 0x8050248
Testing std::vector
opnew: 0x8050438, 3600 bytes
instance v: 28 bytes
opdel: 0x8050438

Testing array new
opnew: 0x8050438, 3604 bytes
instance p: 4 bytes
opdel: 0x8050438



Create a new paste based on this one


Comments: