License : Creative Commons Attribution 4.0 International (CC BY-NC-SA 4.0)
Copyright : Hervé Frezza-Buet, CentraleSupelec
Last modified : April 19, 2024 10:22
Link to the source : execution.md

Table of contents

Execution

Data segments

The purpose of C++ code writing is tu build up programs that run… so understanding execution is crucial.

A process running on a computer handles 4 kinds of memory in the RAM, called data segments.

Text

First kind is a read-only part, called the text. This is the recipe gathered for execution. This is what our C++ code describes. The size of the text is fixed (when you are cooking a recipe, the size of the book where it is written do not change).

Stack

Second kind is the stack, described in the C++ self-study. It is where local variables, function argument are allocated during execution.

Heap

Third kind is the heap, discribed in the C++ self-study as well. It is where memory for on-demand allocations by new or by dynamical objects as std::vectors are taken.

Data

Last kind is the data. This is where global variables (if any) are taken. The room for storing global variables is reserved at start, and the size is fixed during the whole execution. We may not use global variables explicitly, but rather use static members of class, which are a object oriented way to have global variables in your code.

Threads

Last, let us mention that a multi-threaded execution consists of having several threads of execution, as if many cooker were working in parallel. In this case, all the thread share the text data segment (they all read the same recipe book), but each thread my be currently reading at a specific line. Thread also share data and heap data segments. It means that global variables are accessible to all threads, as well as values mentioned by pointers to the heap. Be carefull with concurrent access issue, there are tools for this (mutexes, etc.). Indeed, what threads only own for themselves is the stack. As each thread executes its own recipe, it has to keep in mind its context of exectution (where do I have to return when I am done with the current recipe, what are the arguments, what are the value of local variables, etc.). In other words, each thread as its own stack data segment.

Hervé Frezza-Buet,