Move Semantic: Two nice Properties

I will talk about two nice properties of the move semantic in this post that are not so often mentioned. Containers of the standard template library (STL) can have non-copyable elements. The copy semantic is the fallback for the move semantic. Irritated? I hope so!

Read more
Tags: memory, move
Comments 3Views: 11926

Copy versus Move Semantic: A few Numbers

A lot was written about the advantages of the move semantic to the copy semantic. Instead of an expensive copy operation you can use a cheap move operation. But, what does that mean? I will compare in this post the performance of the copy and move semantic for the containers the Standard Template Library (STL). 

Read more
Tags: memory, move
Comments 2Views: 22853

std::array - Dynamic Memory, no Thanks

std::array combines the best from two worlds. At one hand, std::array has the size and efficiency of a C array; at the other hand, std::array has the interface of a std::vector. 

Read more
Tags: memory
Comments 11Views: 63652

Automatic Memory Management of the STL Containers

One of the big advantages of C++ string to a C string and of a std::vector to a C arrays is it that both C++ containers automatically manage their memory. Of course, that holds true for all further containers of the Standard Template Library. In this post, I will have a closer look at the automatic memory management of std::vector and std::string.

Read more
Tags: memory
Comments 6Views: 18290

std::weak_ptr

std::unique_ptr models the concept of exclusive ownership, std::shared_ptr the concept of shared ownership. If I stick to this picture then std::weak_ptr models the concept of temporary ownership because it borrows the resource from a std::shared_ptr. There is one dominant reason for having a std::weak_ptr in C++: breaking of cyclic references of std::shared_ptr's.

Read more
Comments 5Views: 26625

Specialities of std::shared_ptr

After I draw the big picture of a std::shared_ptr's in the last post, I want to present two special aspects of this smart pointer in this post. First, I show with std::shared_from_this how to create a std::shared_ptr from an object; second, I'm interested in the question to the answer: Should a function take a std::shared_ptr by copy or by reference? The numbers are quite interesting.

Read more
Comments 1Views: 11181

std::shared_ptr

std::shared_ptr's share the resource. The shared reference counter counts the number of owners. Copying a std::shared_ptr increases the reference count by one. Destroying a std::shared_ptr decreases the reference count by one. If the reference count becomes zero, the resource will automatically be released. 

Read more
Comments 48Views: 17274

std::unique_ptr

A std::unique_ptr manages automatically and exclusively the lifetime of its resource according to the RAII idiom. std::unique_ptr should be your first choice because it does its work without memory or performance overhead.

Read more
Add CommentViews: 19376

Memory and Performance Overhead of Smart Pointers

C++11 offers four different smart pointers. On two of them I will have a closer look in this post regarding memory and performance overhead. My first candidate std::unique_ptr takes exclusively care of the lifetime of one resource; std::shared_ptr shares the ownership of a resource with other std::shared_ptr's. I will state the result of my tests before I show you the raw numbers: There are only few reasons in modern C++ justifying the memory management with new and delete.

 

Read more
Comments 8Views: 74375

Careful Handling of Resources

The careful handling of resources - may it be for example memory, files or sockets - is a key concern of programming in C++. This holds in particular true for the embedded programming that is often characterized by limited resources. Therefore, I will write a few posts about this challenging and versatile topic.

Read more
Add CommentViews: 6019

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 4153

All 2925824

Currently are 206 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments