Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Boost C++ Application Development  Cookbook
Boost C++ Application Development  Cookbook

Boost C++ Application Development Cookbook: Recipes to simplify your application development , Second Edition

Arrow left icon
Profile Icon Anton Polukhin Alekseevic
Arrow right icon
S$53.98 S$59.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2 (5 Ratings)
eBook Aug 2017 438 pages 2nd Edition
eBook
S$53.98 S$59.99
Paperback
S$74.99
Subscription
Free Trial
Arrow left icon
Profile Icon Anton Polukhin Alekseevic
Arrow right icon
S$53.98 S$59.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2 (5 Ratings)
eBook Aug 2017 438 pages 2nd Edition
eBook
S$53.98 S$59.99
Paperback
S$74.99
Subscription
Free Trial
eBook
S$53.98 S$59.99
Paperback
S$74.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Boost C++ Application Development Cookbook

Managing Resources

In this chapter, we will cover the following topics:

  • Managing local pointers to classes that do not leave scope
  • Reference counting of pointers to classes used across functions
  • Managing local pointers to arrays that do not leave scope
  • Reference counting of pointers to arrays used across functions
  • Storing any functional objects in a variable
  • Passing function pointer in a variable
  • Passing C++11 lambda functions in a variable
  • Containers of pointers
  • Do it at scope exit!
  • Initializing the base class by the member of the derived class

Introduction

In this chapter, we'll continue to deal with datatypes, introduced by the Boost libraries, mostly focusing on working with the pointers. We'll see how to easily manage resources, how to use a datatype capable of storing any functional objects, functions, and lambda expressions. After reading this chapter, your code will become more reliable and memory leaks will become history.

Managing local pointers to classes that do not leave scope

Sometimes, we are required to dynamically allocate memory and construct a class in that memory. That's where the troubles start. Take a look at the following code:

bool foo1() { 
foo_class* p = new foo_class("Some data");

const bool something_else_happened = some_function1(*p);
if (something_else_happened) {
delete p;
return false;
}

some_function2(p);

delete p;
return true;
}

This code looks correct at first glance. But, what if some_function1() or some_function2() throws an exception? In that case, p won't be deleted. Let's fix it in the following way:

bool foo2() { 
foo_class* p = new foo_class("Some data");
try {
const bool something_else_happened = some_function1(*p);
if (something_else_happened) {
delete...

Reference counting of pointers to classes used across functions

Imagine that you have some dynamically allocated structure containing data, and you want to process it in different threads of execution. The code to do this is as follows:

#include <boost/thread.hpp> 
#include <boost/bind.hpp>

void process1(const foo_class* p);
void process2(const foo_class* p);
void process3(const foo_class* p);

void foo1() {
while (foo_class* p = get_data()) // C way
{
// There will be too many threads soon, see
// recipe 'Parallel execution of different tasks'
// for a good way to avoid uncontrolled growth of threads
boost::thread(boost::bind(&process1, p))
.detach();
boost::thread(boost::bind(&process2, p))
.detach();
boost::thread(boost::bind(&process3, p))
.detach(...

Managing pointers to arrays that do not leave scope

We already saw how to manage pointers to a resource in the Managing pointers to classes that do not leave scope recipe. But, when we deal with arrays, we need to call delete[] instead of a simple delete. Otherwise, there will be a memory leak. Take a look at the following code:

void may_throw1(char ch); 
void may_throw2(const char* buffer);

void foo() {
// we cannot allocate 10MB of memory on stack,
// so we allocate it on heap
char* buffer = new char[1024 * 1024 * 10];

// Oops. Here comes some code, that may throw.
// It was a bad idea to use raw pointer as the memory may leak!!
may_throw1(buffer[0]);
may_throw2(buffer);

delete[] buffer;
}

Getting ready

...

Reference counting of pointers to arrays used across functions

We continue coping with pointers, and our next task is to reference count an array. Let's take a look at the program that gets some data from the stream and processes it in different threads. The code to do this is as follows:

#include <cstring> 
#include <boost/thread.hpp>
#include <boost/bind.hpp>

void do_process(const char* data, std::size_t size);

void do_process_in_background(const char* data, std::size_t size) {
// We need to copy data, because we do not know,
// when it will be deallocated by the caller.
char* data_cpy = new char[size];
std::memcpy(data_cpy, data, size);

// Starting thread of execution to process data.
boost::thread(boost::bind(&do_process, data_cpy, size))
.detach();
boost::thread(boost::bind(&do_process, data_cpy, size...

Storing any functional objects in a variable

Consider the situation when you are developing a library that has its API declared in the header files and implementation in the source files. This library shall have a function that accepts any functional objects. Take a look at the following code:

// making a typedef for function pointer accepting int 
// and returning nothing
typedef void (*func_t)(int);

// Function that accepts pointer to function and
// calls accepted function for each integer that it has.
// It cannot work with functional objects :(
void process_integers(func_t f);

// Functional object
class int_processor {
const int min_;
const int max_;
bool& triggered_;

public:
int_processor(int min, int max, bool& triggered)
: min_(min)
, max_(max)
, triggered_(triggered)
{}

void operator()(int i) const {
if (i...
Left arrow icon Right arrow icon

Key benefits

  • • Learn to use the Boost libraries to simplify your application development
  • • Learn to develop high quality, fast and portable applications
  • • Learn the relations between Boost and C++11/C++4/C++17

Description

If you want to take advantage of the real power of Boost and C++ and avoid the confusion about which library to use in which situation, then this book is for you. Beginning with the basics of Boost C++, you will move on to learn how the Boost libraries simplify application development. You will learn to convert data such as string to numbers, numbers to string, numbers to numbers and more. Managing resources will become a piece of cake. You’ll see what kind of work can be done at compile time and what Boost containers can do. You will learn everything for the development of high quality fast and portable applications. Write a program once and then you can use it on Linux, Windows, MacOS, Android operating systems. From manipulating images to graphs, directories, timers, files, networking – everyone will find an interesting topic. Be sure that knowledge from this book won’t get outdated, as more and more Boost libraries become part of the C++ Standard.

Who is this book for?

This book is for developers looking to improve their knowledge of Boost and who would like to simplify their application development processes. Prior C++ knowledge and basic knowledge of the standard library is assumed.

What you will learn

  • • Get familiar with new data types for everyday use
  • • Use smart pointers to manage resources
  • • Get to grips with compile-time computations and assertions
  • • Use Boost libraries for multithreading
  • • Learn about parallel execution of different task
  • • Perform common string-related tasks using Boost libraries
  • • Split all the processes, computations, and interactions to tasks and process them independently
  • • Learn the basics of working with graphs, stacktracing, testing and interprocess communications
  • • Explore different helper macros used to detect compiler, platform and Boost features

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 30, 2017
Length: 438 pages
Edition : 2nd
Language : English
ISBN-13 : 9781787284838
Vendor :
Microsoft
Languages :
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Aug 30, 2017
Length: 438 pages
Edition : 2nd
Language : English
ISBN-13 : 9781787284838
Vendor :
Microsoft
Languages :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just S$6 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just S$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total S$ 216.97
C++17 STL Cookbook
S$74.99
Boost C++ Application Development  Cookbook
S$74.99
Mastering the C++17 STL
S$66.99
Total S$ 216.97 Stars icon

Table of Contents

12 Chapters
Starting to Write Your Application Chevron down icon Chevron up icon
Managing Resources Chevron down icon Chevron up icon
Converting and Casting Chevron down icon Chevron up icon
Compile-Time Tricks Chevron down icon Chevron up icon
Multithreading Chevron down icon Chevron up icon
Manipulating Tasks Chevron down icon Chevron up icon
Manipulating Strings Chevron down icon Chevron up icon
Metaprogramming Chevron down icon Chevron up icon
Containers Chevron down icon Chevron up icon
Gathering Platform and Compiler Information Chevron down icon Chevron up icon
Working with the System Chevron down icon Chevron up icon
Scratching the Tip of the Iceberg Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2
(5 Ratings)
5 star 80%
4 star 0%
3 star 0%
2 star 0%
1 star 20%
Grigory Nov 03, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm an experienced C++ programmer. One of my friends recommended this book and I enjoyed it.I use Boost for my own projects and know it pretty well. So I read the first few recipes fully and then just started reading Intro+HowToDoIt+TheresMore sections, skipping the detailed recipe description ("How It works").That was great. A lot of good tricks and modern C++ knowledge even for an experienced programmer!I strictly recommend reading this book.
Amazon Verified review Amazon
Jerome Lanteri Aug 30, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
good book for have reference and examples of use for boost C++ lib
Amazon Verified review Amazon
Sergey Platonov Oct 27, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Liked the easter eggs! Now seriously, the book is well written with a lot of practical advice. It is a set of recipes for everyday use, actually. Every topic (or recipe) consists of five parts: a short introduction of what this topic is about and what you'll need to use it, the recipe itself, explanation, additional information on the subject and related links. I've really enjoyed "There's more..." section. since usually it contains some interesting facts.Strongly recommend this book to every C++ developer, especially, if you just discovered boost or about to use boost ln your project.Of course, some topics seem to be pretty obvious, but I guess one can't know everything, so someone might find this helpful.
Amazon Verified review Amazon
Robert Toby Chaloner Jan 07, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
As a starting point for C++ Boost its very good. Gives a good grounding to build on
Amazon Verified review Amazon
Masum Serazi Feb 06, 2018
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
This book is waste of money. Please don't buy it, rather go to boost official site and read their tutorial.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.