Henrik Samuelsson > Recent Status Updates

Showing 1-30 of 753
Henrik Samuelsson
Henrik Samuelsson is on page 58 of 560 of The Rust Programming Language, 2nd Edition
Rust have the keyword match, that is used in kind of the same way as we use a switch in C or Java.
Oct 01, 2023 09:20PM Add a comment
The Rust Programming Language, 2nd Edition

Henrik Samuelsson
Henrik Samuelsson is on page 180 of 792 of C++ Crash Course: A Fast-Paced Introduction
We can get an copy of objects from:

1. Copy constructor i.e. Object(const Object& otherObject)
2. Copy assignment i.e. the = operator

There will be a default versions of the above but they might not behave as desired since there will be no deep copying. we will then instead define our own versions.
Sep 07, 2023 10:32PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 173 of 792 of C++ Crash Course: A Fast-Paced Introduction
Structured binding is a C++ feature that allows you to bind multiple variables to the elements of a structured object, such as a struct, in a single declaration. This can be for example be used to return multiple different values from a function.

// Inside function called create_car
return {car, production_year}

// Catch the two return values
auto [my_car, year] = create_car();
Sep 06, 2023 10:10PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 169 of 792 of C++ Crash Course: A Fast-Paced Introduction
Allocation and deallocation of memory should be handled within the constructor and destructor of a class to prevent leaking of memory used for storage.

This pattern is called resource acquisition is initialization (RAII) or constructor acquires, destructor releases (CADRe). Where the second name is in my opinion better because it describes the intent.
Sep 05, 2023 11:44PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 165 of 792 of C++ Crash Course: A Fast-Paced Introduction
If the code misbehaves for whatever reason so can this be handled by the use of the exception system that comes with C++.

Say for example that we receive unexpected input that our code can not handle. We can then move to special sections of our code base to try to handle this.

The C++ keywords used for exception implementation is try and catch, first we try to do something and if it fails we catch the exception.
Sep 04, 2023 10:30PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 156 of 792 of C++ Crash Course: A Fast-Paced Introduction
The keywords new and delete can be used to dynamically allocate memory for objects in C++.

Delete must be called sometimes after new to release the memory. Otherwise there will be a memory leak meaning that more and more memory will be used until we possibly run out of memory.

Think of this as we borrow memory and it is our responsibility to return it.
Sep 03, 2023 10:19PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 153 of 792 of C++ Crash Course: A Fast-Paced Introduction
Static members are members of a class that aren’t associated with a particular instance of the class.

Example:

struct RatThing {
static int rat_things_power; // A static member.
};

int RatThing::rat_things_power = 200; // Initialize the static member

int main() {
// snip
}
Aug 31, 2023 10:44PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 138 of 792 of C++ Crash Course: A Fast-Paced Introduction
C++ references can not be reseated. You can reassign a reference but it will not move the reference. What happens is instead that the value at the address that is referenced by the reference will be updated.

In other words pointers and references behaves different when employing the assignment operator =. Reassigning a pointer will set it to another address.
Aug 28, 2023 10:51PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 125 of 792 of C++ Crash Course: A Fast-Paced Introduction
There are unfortunately multiple ways to initialize objects in C++. If unsure what way to initialize an object go for braced initializers:

char name[] = {"Bjarne"};
int year_of_birth{1950};
Aug 26, 2023 09:40AM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 121 of 792 of C++ Crash Course: A Fast-Paced Introduction
Most Vexing Parse

The most vexing parse is a ambiguity resolution in the C++ programming language. In certain situations, the C++ grammar cannot distinguish between the creation of an object parameter and specification of a function's type. In those situations, the compiler is required to interpret the line as a function type specification. (Wikipedia)

Use braced initialization syntax to avoid most vexing parse.
Aug 25, 2023 11:07PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 112 of 792 of C++ Crash Course: A Fast-Paced Introduction
There are two types of enums in C++, scoped and unscoped.

Scoped enums are safer, need to use the type followed by :: when using the values. We use two keywords; enum class to create these enums in the code.

Unscoped enums are supported for legacy reasons, these are created by just using the keyword enum.
Aug 25, 2023 05:56AM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 107 of 792 of C++ Crash Course: A Fast-Paced Introduction
The C style way of getting the number of elements in an array (of int) is:

size_t num_elements = sizeof(array) / sizeof(int);

In C++ we can instead use std::size function from the header:

num_elements = std::size(array);
Aug 24, 2023 09:50PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 101 of 792 of C++ Crash Course: A Fast-Paced Introduction
std::byte is a C++ type that is dedicated for access to raw memory and bit manipulation, but pretty much nothing else. It is for example not possible to perform arithmetic operations on this type.

std::byte b{42}
b *= 2; // Will not compile
b <<= 1; // Right shift will compile, and is equivalent to multiplication by 2
Aug 23, 2023 10:07PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 89 of 792 of C++ Crash Course: A Fast-Paced Introduction
The printf function can be used to print text and data if including cstdio.

Printf shall be given a first argument known as the format string, the basis for what will be printed.

The format string can hold format specifiers, indicated by a %, these inform about how to interpret additional arguments provided to printf such as variables or literals to be printed.

Ex:

printf("%d toes", 10); // Output 10 toes
Aug 22, 2023 09:49PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 59 of 792 of C++ Crash Course: A Fast-Paced Introduction
Benefits of C++ over C according to the book:

- Function Overloading - Only need to remember one function name
- References - A bit safer than raw pointers because cannot be null
- auto Initialization - Improves readability and refactoring somewhat
- range for loop - Less cluttered code
- C++ Standard Library - Many useful features here
- Lambdas - Can be easier to write a lambda than a full one-time function
Aug 21, 2023 04:03AM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 48 of 792 of C++ Crash Course: A Fast-Paced Introduction
Book starts with an section on differences between C and C++.

Example of difference:

In C we assign names to types we create, such as structs or enums, using the typedef keyword. In C++ this is done implicitly for us, the type tag will automatically become the name of our new type.
Aug 20, 2023 10:29PM Add a comment
C++ Crash Course: A Fast-Paced Introduction

Henrik Samuelsson
Henrik Samuelsson is on page 597 of 656 of Introduction to Programming with C++ for Engineers (IEEE Press)
Last chapter looks briefly into parallel execution of code in C++.

- Measure performance before refactoring, don't guess, use a profiler
- Must protect shared variables (mutex or similar)
- Many C++ standard algorithms comes with parallel implementation (std::execution::par)
- A good choice for parallel programming in C++ is to use OpenMP
Aug 20, 2023 12:47AM Add a comment
Introduction to Programming with C++ for Engineers (IEEE Press)

Henrik Samuelsson
Henrik Samuelsson is on page 570 of 656 of Introduction to Programming with C++ for Engineers (IEEE Press)
Real numbers are in computers often represented by floating point numbers. It is important to understand that this is only an approximation and that this introduces limitations:

- There will be rounding errors
- The spacing is not uniform, larger numbers are less exact
- Subtraction of close numbers can lead to severe cancelation errors

Floating point values are rounded to the nearest, ints are truncated towards 0.
Aug 19, 2023 04:36AM Add a comment
Introduction to Programming with C++ for Engineers (IEEE Press)

Henrik Samuelsson
Henrik Samuelsson is on page 521 of 656 of Introduction to Programming with C++ for Engineers (IEEE Press)
Integers values can be stored exactly in computers. For example 10 can be represented by the bit combination 0000 1010, meaning 8 + 2 = 10

Fractions might not be able to be stored exactly in computers. Fractions that involves multiples of 2 can be stored exactly if using sufficient numbers of bits. But other fractions will never be exact, regardless of bits used for storage there will still be an rounding error.
Aug 19, 2023 01:03AM Add a comment
Introduction to Programming with C++ for Engineers (IEEE Press)

Henrik Samuelsson
Henrik Samuelsson is on page 516 of 656 of Introduction to Programming with C++ for Engineers (IEEE Press)
Integer addition in C++ requires storage for the result of adequate size.

Adding unsigned integers that do not fit in the result variables will cause overflow and the result wraps around. The result will be the same as using the modulo operation, likely not the desired result.

Overflow behaviour when adding two signed integers is undefined so this is even worse.

In general arithmetic overflow needs to be avoided.
Aug 19, 2023 12:39AM Add a comment
Introduction to Programming with C++ for Engineers (IEEE Press)

Henrik Samuelsson
Henrik Samuelsson is on page 516 of 656 of Introduction to Programming with C++ for Engineers (IEEE Press)
Arithmetic values can have different bases. Humans use base 10 because they come equipped with 10 fingers.

Computers will generally use base 2 due that the fundamental unit in a computer is the bit and this can only have 2 different states, 1 or 0.

A computer combines several bits to handle larger values than 0 and 1. A byte holding 8 bits can store values 0 to 255, or -128 to 127 depending on bit interpretation.
Aug 18, 2023 11:26PM Add a comment
Introduction to Programming with C++ for Engineers (IEEE Press)

Henrik Samuelsson
Henrik Samuelsson is on page 509 of 656 of Introduction to Programming with C++ for Engineers (IEEE Press)
Objects are not limited to storing data they are also used for actions, these are referred to as functional objects. Functional objects will implement the overloaded functional operator ().
Aug 18, 2023 10:58PM Add a comment
Introduction to Programming with C++ for Engineers (IEEE Press)

« previous 1 3 4 5 6 7 8 9 25 26
Follow Henrik's updates via RSS