December 2018
Intermediate to advanced
552 pages
12h 18m
English
Classical C++ doesn't distinguish between who owns a pointer (that is, the code or object responsible for releasing the memory associated with a pointer) and who is simply accessing memory using a pointer, for example:
void init(int *p){ *p = 0;}int main(void){ auto p = new int; init(p); delete p;}// > g++ scratchpad.cpp; ./a.out//
In the preceding example, we allocate a pointer to an integer, and then pass that pointer to a function called init(), which initializes the pointer. Finally, we delete the pointer after it has been used by the init() function. If the init() function were located in another file, it would not be clear whether the init() function should delete the pointer. Although in this simple example it might ...
Read now
Unlock full access