struct Foo
{
static int i ;
int f;
Foo():f(i++){ cout << "in ctor:" << i << endl; }
Foo(const Foo&):f(i++){ cout << "in copy ctor:" << i << endl; }
};
int Foo::i = 1;
void tst_vec()
{
vector v;
v.reserve(10);
Foo f ;
v.push_back(f);
v.push_back(Foo());
v.push_back(Foo());
cout << "---" << endl;
Foo& temp = v[0];
}
---
with reserve:
in ctor:2
in copy ctor:3
in ctor:4
in copy ctor:5
in ctor:6
in copy ctor:7
---
without reserve:
in ctor:2
in copy ctor:3
in ctor:4
in copy ctor:5
in copy ctor:6
in ctor:7
in copy ctor:8
in copy ctor:9
in copy ctor:10
importance of vector::reserve
Not using reserve results in quite some extra work.
virtual destructors and valgrind
Was curious to know the effects of missing a virtual destructor, and ran the program on valgrind:
Valgrind reported a memory leak.
Valgrind reported a memory leak.
user@ubuntu:~/devel/effective_cpp$ g++ virt_dtor.cpp -ggdb user@ubuntu:~/devel/effective_cpp$ cat virt_dtor.cpp #includeusing namespace std; class Base{ public: ~Base() { cout << "~Base()" << endl; } }; class Der: public Base{ public: int* array; Der(){ array = new int[10]; } ~Der() { cout << "~Der()" << endl; delete[] array; } }; int main() { Base* b = new Der; delete b; } user@ubuntu:~/devel/effective_cpp$ valgrind --leak-check=full ./a.out ==2791== Memcheck, a memory error detector ==2791== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==2791== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info ==2791== Command: ./a.out ==2791== ~Base() ==2791== ==2791== HEAP SUMMARY: ==2791== in use at exit: 40 bytes in 1 blocks ==2791== total heap usage: 2 allocs, 1 frees, 44 bytes allocated ==2791== ==2791== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==2791== at 0x4025FE5: operator new[](unsigned int) (vg_replace_malloc.c:299) ==2791== by 0x80488C8: Der::Der() (virt_dtor.cpp:12) ==2791== by 0x80487A7: main (virt_dtor.cpp:18) ==2791== ==2791== LEAK SUMMARY: ==2791== definitely lost: 40 bytes in 1 blocks ==2791== indirectly lost: 0 bytes in 0 blocks ==2791== possibly lost: 0 bytes in 0 blocks ==2791== still reachable: 0 bytes in 0 blocks ==2791== suppressed: 0 bytes in 0 blocks ==2791== ==2791== For counts of detected and suppressed errors, rerun with: -v ==2791== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 19 from 8)
Different types of mutexes
Some of the different types of Mutex:
-- Fair / Unfair Mutex : A mutex which allows access in the order of locking
-- Reentrant : which allows recursive locking, helpful when there are recursive calls in the code. See also man page description of PTHREAD_MUTEX_RECURSIVE
-- Scalable Mutex
-- Sleep or spin : spin is optimal for short waits and sleep for long waits. And also while spining, there is no need for context switch.
Reference : Intel threading building blocks: outfitting C++ for multi-core processor ... By James Reinders, p.114
-- Fair / Unfair Mutex : A mutex which allows access in the order of locking
-- Reentrant : which allows recursive locking, helpful when there are recursive calls in the code. See also man page description of PTHREAD_MUTEX_RECURSIVE
-- Scalable Mutex
-- Sleep or spin : spin is optimal for short waits and sleep for long waits. And also while spining, there is no need for context switch.
Reference : Intel threading building blocks: outfitting C++ for multi-core processor ... By James Reinders, p.114
Subscribe to:
Posts (Atom)