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