4
|
1 |
Memory
|
|
2 |
=============
|
|
3 |
|
|
4 |
size_t and unsigned long
|
|
5 |
-------
|
|
6 |
On windows 64bit, sizeof(unsigned long) == 32 and sizeof(size_t) == 64.
|
|
7 |
On linux 64bit, sizeof(unsigned long) == sizeof(size_t) == 64
|
|
8 |
|
|
9 |
intptr_t and uintptr_t (inttypes.h): ints that guarantee to hold a pointer.
|
|
10 |
|
|
11 |
ptrdiff_t: type returned by arithmetic differencing two pointers.
|
|
12 |
|
|
13 |
::operator new and ::operator delete
|
|
14 |
-------
|
|
15 |
[Stroustrup3rd, 19.4.5, p576]
|
|
16 |
Can be redefined, to use posix_memalign and free
|
|
17 |
The redefinition can be done as class members [Stroustrup3rd, 15.6, p421]
|
|
18 |
class A
|
|
19 |
{
|
|
20 |
static void* operator new (size_t size);
|
|
21 |
static void operator delete (void *p);
|
|
22 |
};
|
|
23 |
|
|
24 |
int main()
|
|
25 |
{
|
|
26 |
A *p = new A; // calls A::new;
|
|
27 |
delete p; // calls A::delete;
|
|
28 |
|
|
29 |
}
|
|
30 |
|
|
31 |
The redefinition can't be done on other namespace than the global, if not
|
|
32 |
done in a class.
|
|
33 |
|
|
34 |
Prototypes for the new/delete Operators
|
|
35 |
--------------
|
|
36 |
void* operator new(size_t);
|
|
37 |
void operator delete(void* );
|
|
38 |
void* operator new[](size_t);
|
|
39 |
void operator delete[](void* );
|
|
40 |
#include <new> // has set_new_handler(), for bad_alloc handling.
|
|
41 |
|
|
42 |
|
|
43 |
Exceptions
|
|
44 |
=================
|
|
45 |
<stdexcept>:
|
|
46 |
logic_error
|
|
47 |
domain_error
|
|
48 |
invalid_argument
|
|
49 |
length_error
|
|
50 |
out_of_range
|
|
51 |
runtime_error
|
|
52 |
range_error
|
|
53 |
overflow_error
|
|
54 |
underflow_error
|
|
55 |
|
|
56 |
|
|
57 |
Casting
|
|
58 |
=================
|
|
59 |
Explicit type conversion [Stroustrup3rd, 6.2.7, p130]
|
|
60 |
static_cast - converts between related types (pointer to another, enum to int,
|
|
61 |
float to int,...). Has minimal type checking, and are often portable.
|
|
62 |
reinterpret_cast - converts between unrelated types (pointer to int, pointers
|
|
63 |
to functions [7.7], ...).
|
|
64 |
No type checking. Rarely portable.
|
|
65 |
dynamic_cast - run-time checked conversion.
|
|
66 |
const_cast - for removing const qualifiers.
|
|
67 |
(Type) exp - Unspecified combination of static+reinterpret+dynamic casts,
|
|
68 |
to get the cast done.
|