Online Book Reader

Home Category

Professional C__ - Marc Gregoire [376]

By Root 1305 0
delete. However, you can still have memory leaks in this example! If the go() method throws an exception, the call to delete will never be executed, causing a memory leak. So, also in this case it is recommended to use smart pointers.

Never assign the result of a memory allocation to a dumb pointer. Whether you use new or malloc() or any other memory allocation method, always immediately give the resulting memory pointer to a smart pointer, shared_ptr or unique_ptr.

If you use old C-style arrays, you cannot use shared_ptr to manage their memory. Instead of using the old C-style arrays, you should switch to modern memory safe containers like the std::array, std::vector and so on, or give the C-style array to a unique_ptr which is allowed.

Never use a shared_ptr to manage a pointer to a C-style array. Use unique_ptr to manage the C-style array, or use STL containers instead of C-style arrays.

A shared_ptr can be created in two ways. With the first way, you have to mention the type you want to allocate twice: once as template parameter for shared_ptr and once as parameter to the new operator:

shared_ptr mySimpleSmartPtr(new Simple());

The second syntax is as follows:

auto mySimpleSmartPtr = make_shared();

This syntax uses the auto keyword and uses make_shared(), so you only have to specify the allocated type (Simple) once. If the Simple constructor requires parameters, you put them in between the parentheses of the make_shared() call. In fact, using make_shared() not only requires less typing, but the compiler can also generate more efficient code than using the longer syntax. For example, with Microsoft Visual C++ if you define your shared_ptr with the syntax shown in the following line, VC++ needs to allocate memory for the object and then allocate memory for a so called reference count control block that stores the reference count. The details of the reference count control block are specific to VC++ and are not important for this discussion.

shared_ptr mySimpleSmartPtr(new Simple());

However, by using make_shared() as follows, VC++ optimizes this memory allocation and allocates enough memory for both the object and the reference count control block with a single memory allocation call:

auto mySimpleSmartPtr = make_shared();

By default, shared_ptr will use the standard new and delete operators to allocate and deallocate memory. You can change this behavior as follows:

int* malloc_int(int value)

{

int* p = (int*)malloc(sizeof(int));

*p = value;

return p;

}

int main()

{

shared_ptr myIntSmartPtr(malloc_int(42), free);

return 0;

}

Code snippet from shared_ptr\shared_ptr_malloc_int.cpp

This will allocate the memory for the integer with malloc_int(), and the shared_ptr will deallocate the memory using the standard free() function. As mentioned earlier, in C++ you should never use malloc() but new instead. However, this feature of shared_ptr is available because it is very useful to manage other resources instead of just memory. For example, it can be used to automatically close a file or network socket or anything when the shared_ptr goes out of scope. The following example uses a shared_ptr to store a file pointer. When the shared_ptr goes out of scope, the file pointer is automatically closed with a call to the CloseFile() function. Remember that C++ has proper object oriented classes to work with files (see Chapter 15). Those classes will already automatically close their files when they go out of scope. This example using the old C fopen() and fclose() functions is just to give a demonstration for what shared_ptrs can be used for besides pure memory:

void CloseFile(FILE* filePtr)

{

if (filePtr == nullptr)

return;

fclose(filePtr);

cout << "File closed." << endl;

}

int main()

{

shared_ptr filePtr(fopen("data.txt", "w"), CloseFile);

if (filePtr == nullptr) {

cerr << "Error opening file." << endl;

} else {

cout << "File opened." << endl;

// Use filePtr

}

return 0;

}

Code snippet from shared_ptr\shared_ptr_file.cpp

Both shared_ptr

Return Main Page Previous Page Next Page

®Online Book Reader