Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On System Programming with C++

You're reading from   Hands-On System Programming with C++ Build performant and concurrent Unix and Linux systems with C++17

Arrow left icon
Product type Paperback
Published in Dec 2018
Publisher Packt
ISBN-13 9781789137880
Length 552 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Dr. Rian Quinn Dr. Rian Quinn
Author Profile Icon Dr. Rian Quinn
Dr. Rian Quinn
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Getting Started with System Programming FREE CHAPTER 2. Learning the C, C++17, and POSIX Standards 3. System Types for C and C++ 4. C++, RAII, and the GSL Refresher 5. Programming Linux/Unix Systems 6. Learning to Program Console Input/Output 7. A Comprehensive Look at Memory Management 8. Learning to Program File Input/Output 9. A Hands-On Approach to Allocators 10. Programming POSIX Sockets Using C++ 11. Time Interfaces in Unix 12. Learning to Program POSIX and C++ Threads 13. Error – Handling with Exceptions 14. Assessments 15. Other Books You May Enjoy

Error handling POSIX-style

POSIX-style error handling provides the most basic form of error handling possible, capable of being leveraged on almost any system, in almost any program. Written with standard C in mind, POSIX-style error handling takes the following form:

if (foo() != 0) {
std::cout << errno << '\n';
}

Generally, each function called either returns 0 on success or -1 on failure, and stores the error code into a global (non-thread safe) implementation-defined macro, called errno. The reason 0 is used for success is that on most CPUs, comparing a variable to 0 is faster than comparing a variable to any other value, and the success case is the expected case. The following example demonstrates how this pattern is used:

#include <cstring>
#include <iostream>

int myfunc(int val)
{
if (val == 42) {
errno = EINVAL;
return...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image