Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying C++ System Programming Cookbook
  • Table Of Contents Toc
C++ System Programming Cookbook

C++ System Programming Cookbook

By : Onorato Vaticone
3.8 (4)
close
close
C++ System Programming Cookbook

C++ System Programming Cookbook

3.8 (4)
By: Onorato Vaticone

Overview of this book

C++ is the preferred language for system programming due to its efficient low-level computation, data abstraction, and object-oriented features. System programming is about designing and writing computer programs that interact closely with the underlying operating system and allow computer hardware to interface with the programmer and the user. The C++ System Programming Cookbook will serve as a reference for developers who want to have ready-to-use solutions for the essential aspects of system programming using the latest C++ standards wherever possible. This C++ book starts out by giving you an overview of system programming and refreshing your C++ knowledge. Moving ahead, you will learn how to deal with threads and processes, before going on to discover recipes for how to manage memory. The concluding chapters will then help you understand how processes communicate and how to interact with the console (console I/O). Finally, you will learn how to deal with time interfaces, signals, and CPU scheduling. By the end of the book, you will become adept at developing robust systems applications using C++.
Table of Contents (13 chapters)
close
close

Understanding C++ primitive types

This recipe will show all the primitive data types defined by the C++ standard, as well as their size.

How to do it...

In this section, we'll have a closer look at what primitives the C++ standard defines and what other information is important. We'll also learn that although the standard does not define a size for each, it defines another important parameter:

  1. First, open a new Terminal and type in the following program:
#include <iostream>
#include <limits>

int main ()
{
// integral types section
std::cout << "char " << int(std::numeric_limits<char>::min())
<< "-" << int(std::numeric_limits<char>::max())
<< " size (Byte) =" << sizeof (char) << std::endl;
std::cout << "wchar_t " << std::numeric_limits<wchar_t>::min()
<< "-" << std::numeric_limits<wchar_t>::max()
<< " size (Byte) ="
<< sizeof (wchar_t) << std::endl;
std::cout << "int " << std::numeric_limits<int>::min() << "-"
<< std::numeric_limits<int>::max() << " size
(Byte) ="
<< sizeof (int) << std::endl;
std::cout << "bool " << std::numeric_limits<bool>::min() << "-"
<< std::numeric_limits<bool>::max() << "
size (Byte) ="
<< sizeof (bool) << std::endl;

// floating point types
std::cout << "float " << std::numeric_limits<float>::min() <<
"-"
<< std::numeric_limits<float>::max() << " size
(Byte) ="
<< sizeof (float) << std::endl;
std::cout << "double " << std::numeric_limits<double>::min()
<< "-"
<< std::numeric_limits<double>::max() << " size
(Byte) ="
<< sizeof (double) << std::endl;
return 0;
}
  1. Next, build (compile and link) g++ primitives.cpp.
  2. This will produce an executable file with the (default) name of a.out.

How it works...

The output of the preceding program will be something like this:

This represents the minimum and maximum values that a type can represent and the size in bytes for the current platform.

The C++ standard does not define the size of each type, but it does define the minimum width:

  • char: Minimum width = 8
  • short int: Minimum width = 16
  • int: Minimum width = 16
  • long int: Minimum width = 32
  • long int int: Minimum width = 64

This point has huge implications as different platforms can have different sizes and a programmer should cope with this. To help us get some guidance regarding data types, there is the concept of a data model. A data model is a set of choices (a specific size for each type) made by each implementation (the psABI of the architecture that compilers and operating systems adhere to) to define all the primitive data types. The following table shows a subset of various types and data models that exist:

Data type LP32 ILP32 LLP64 LP64
char 8 8 8 8
short int 16 16 16 16
int 16 32 32 32
long 32 32 32 64
pointer 32 32 64 64

The Linux kernel uses the LP64 data model for 64-bit architectures (x86_64).

We briefly touched on the psABI topic (short for platform-specific Application Binary Interfaces (ABIs)). Each architecture (for example, x86_64) has a psABI specification that the OS adheres to. The GNU Compiler Collection (GCC) has to know these details as it has to know the sizes of the primitive types it compiles. The i386.h GCC header file contains the size of the primitive data types for that architecture:

root@453eb8a8d60a:~# uname -a
Linux 453eb8a8d60a 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

The program output shows that the current OS (actually, the Ubuntu image we're running) uses the LP64 data model as expected and that the machine's architecture is x86_64.

There's more...

As we've seen, the C++ standard defines the following primitive data types:

  • Integer: int
  • Character: char
  • Boolean: bool
  • Floating point: float
  • Double floating point: double
  • Void: void
  • Wide character: wchar_t
  • Null pointer: nullptr_­t

Data types can have other information so that their types can be defined:

  • Modifiers: signed, unsigned, long, and short
  • Qualifiers: const and restrict
  • Storage type: auto, static, extern, and mutable

Obviously, not all these additional attributes can be applied to all the types; for example, unsigned cannot be applied to the float and double types (their respective IEEE standards would not allow that).

See also

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
C++ System Programming Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon