Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Monday, January 2, 2023

Modern C++ features

Modern C++

In this article, I will provide a tutorial on features in modern C++ using very simple examples. The examples will be kept short for beginners to quickly grasp the features.

The complete list of features can be found in the following links: C++11, C++14, C++17, C++20. Please refer to these links for more information.

1 C++11

1.1 nullptr

Considering function overloading:

void fun(int);
void fun(char *);

Traditionally, NULL is often defined as 0. The issue arises with which function will foo(NULL) call?

Thus, C++11 introduces a new kerword, nullptr, as a distinghished null pointer consant.

#include <iostream>

void fun(char *p) {
        std::cout << "hi pointer" << std::endl;

}

void fun(int i) {
        std::cout << "hi integer" << std::endl;

}

int main()
{
        fun(0);       // Calls func(int)
        fun(nullptr); // Calls func(char*)
        //fun(NULL);  // This generates an error
        return 0;

}

1.2 Strongly typed enumeration

Enumerations in C++03 are effectively integers and do not have their own scope. It is possible to compare two enum values of different enumeration types.

#include <iostream>

int main()
{
        enum Animal {Cat, Dog, Elephant};
        enum Fish {Clam, Dolphin, Eel};

        Animal a = Dog;
        Fish f = Dolphin;

        if (a == f)
                std::cout << "Dog == Dolphin" << std::endl;
        else
                std::cout << "Dog != Dolphin" << std::endl;
        return 0;
}

Sunday, December 18, 2022

C11 (C standard revision) features

C11 revision

1 Overview

C11 is the informal name for ISO/IEC 9899:2011, the current standard for the C language that was ratified by ISO in December 2011.

It standardizes many features for safer programming, such as removal of gets() function.

Additionally, C11 adds concurrency support, type-generic expressions, alignment-related facilities, static assertion, unicode support, floating-point characteristic macros, no-return functions, anonymous structures and unions, and various bound-checking and reentrancy functions.

The following examples demonstrating C11 features can be compiled with options such as

clang -std=c11 ex.c

2 Multiple threading

C11 standardized multi-threading support.

It introduces new headers threads.h and stdatomic.h, supporting multiple threads of execution:

  • threads creation and management
  • mutexes
  • conditional variables
  • atomic objects
  • thread specific storage

Monday, December 12, 2022

STM32CubeIDE C++ the simplest example with cin and cout

Table of Contents

Refer to STM32CubeIDE Input/Output on STM32 Nucleo Board in advance for basics.

1 Create a new STM32 project

In STM32CubeIDE, create a new STM32 project.

In Setup STM32 project window, select C++ for the Targeted Language.


STM32CubeIDE Input/Output on STM32 Nucleo Board

Table of Contents

Step by step to use printf() and scanf() in STM32CubeIDE with Nucleo-L152RE board.

1 STM32CubeIDE installation

2 Create a new STM32 project

In STM32CubeIDE menu, click File | New | STM32 Project.



Wednesday, April 6, 2011

MIPS Exceptions Initialization and Handling on Linux

Exceptions and interrupts are un-expected events that disrupt the normal flow of execution. Interrupts are unexpected events from outside the CPU core. Exceptions are from within the CPU, including memory translation exceptions, cache misses, unusual program conditions such as unaligned loads, system calls and traps, and so on.

MIPS architecture adopts precise exception mechanisms, and make it easy for the software to deal with the exceptions. After any exception, CP0 EPC register points to the correct place to restart execution after the exception is dealt with.

Friday, December 4, 2009

MIPS calling convention with GCC

In this article, we are going to learn MIPS calling conventions by examples. And we will focus on the one used by GCC. All the examples will be compiled with GCC flag -mno-abicalls not generating SVR4-style position-independent code. Normally -mabicalls should be used, we disable it for the simplification of assembly codes.

Thursday, November 19, 2009

C99 Features

Here is a simple example demonstrating some of the C99 features. It is a test program I wrote for learning from C99 standard. Although it doesn't cover all the new features for C99, I think it is at least a start for people who are not familiar to C99 like me.

Click HERE to download c99.c.

Monday, November 16, 2009

Learning x86 Calling Conventions by Examples

The x86 architecture features many different calling conventions. There are three major calling conventions in use: __cdecl, __stdcall, and __fastcall. We are going to force the compiler to generate assembly with different calling conventions in order to see how they work.

Sunday, November 8, 2009

Understand Weak Symbols by Examples

Wikipedia defines the weak symbols: "In computing, a weak symbol is a symbol definition in an object file or dynamic library that may be overridden by other symbol definitions, its value will be zero if no definition found by loader." In other words, we can define a symbol that doesn't need to be resolved at link time. It is a very well-known feature and used a lot in Linux Kernel, Glibc, and so on.