Memory management in the d programming language

Memory management in the d programming language, tutoriel & guide de travaux pratiques en pdf.

D Programming Language
Overview
The D programming language, designed and implemented by Walter Bright, appeared in 1999 when W. Bright decided that too many mistakes have been made in the design of C++, and took upon himself to create a new programming language to address C++’s shortcomings while, at the same time, adding features from modern managed programming languages. Walter Bright was the main developer of the first native C++ compiler, Zortech C++ (later to become Symantec C++, now Digital Mars C++), and thus already had experience with writing compilers. Since the first version released in 2001, the language was in continuous development, and is continuously attracting the attention of more developers.
D is a “systems” programming language, meaning it can be used for low-level task such as writing device drivers and operating systems (indeed, there is a project for writing an operating system entirely in D). However, this does not imply the relative inconvenience of use usually associated with compiled languages such as C (when comparing to C#/Java or interpreted languages). On the contrary, D attempts to combine the power of high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. D also concentrates on quality assurance, documentation, management, portability and reliability.
The D programming language is statically typed and compiles directly to machine code. It is not concentrated on a single paradigm, and supports many programming styles: imperative (procedural), object oriented, and meta-programming. Recent additions to the language also allow programs to be written in a functional style. Since it is not associated with a company or commercial organization, it is not governed by a corporate agenda and is not being designed by a committee – most of the design is influenced by the success of other languages and is “community-driven” (the community’s feedback greatly affects the language’s design).
D has received coverage in press on several occasions. The book “Learn to Tango with D” was written by Kris Bell, Lars Ivar Igesund, Sean Kelly and Michael Parker in 2008, and published by firstPress. A German book “Programming in D: Introduction to the new Programming Language” is available, as well as a Japanese book “D Language Perfect Guide”. Andrei Alexandrescu, a renowned expert on advanced C++ programming, author of the book “Modern C++ Design”, is expected to release a book titled “The D Programming Language” in October 2009.
There are currently several D compiler implementations. The reference implementation, Digital Mars D, is available for Windows, Linux, FreeBSD and OS X. The reference implementation is open-source, with the back-end being under a restrictive open-source license and the front-end and standard library being free software which can be freely reused.
Third-party implementations include GDC (GNU D compiler) and LDC (LLVM D compiler). The GDC compiler adapts the official front-end and standard library with the GNU C compiler (GCC), thus allowing D to be used on virtually any platform to which GCC has been ported. The LLVM D compiler is based on LLVM (“Low Level Virtual Machine”), a new compiler infrastructure designed for compile-time, link-time, run-time and “idle-time” optimization of programs written in arbitrary languages. LDC is considered production-ready for Linux 32-bit and 64-bit platforms, with more platform support on the way as LLVM development continues.

Notable features

The D specification lists numerous new and improved features compared to C++.
This section describes several features relevant to the scope of this paper.
Dynamic arrays
Aside from support of C-like static arrays, D introduces built-in dynamic arrays.
These arrays are different from the general notion of dynamic arrays in other languages.
A dynamic array is declared as follows:
int[] a;
A D dynamic array is internally represented as a structure with a pointer and length. The pointer indicates the start of the array data; the length indicates the number of elements in the array. These fields can be accessed using the respective properties – a.ptr and a.length .
D allows the following array operations: allocation, indexing, resizing, slicing, concatenation, appending, duplication, deallocation.
Allocation can be done using the new keyword, for example:
a = new int[1000];
This will allocate an array of 1000 integers.
Indexing is done like with regular arrays:
int i = a[5];
Resizing is done by setting the length property:
a.length = 500;
Note that this will not result in reallocation of the array. When the length is set to a value smaller than the previous one, no reallocation occurs – only the length value is updated in the array structure. Reallocation does happen when the length is set to a value larger than the previous one.
Slicing arrays is done using the slicing operator:
int[] b = a[100..200];
Slicing will create a new array construct, which points to the 100th element of a and has a length of 200, but will not reallocate memory. Thus, writing to an element of the b array will also modify the a array. This may sound counter-intuitive, however it opens up the path to new programming strategies, allowing to write more efficient code, because it allows programmers to avoid memory duplication. Omitting the contents of the square brackets is valid, and indicates a slice of the entire array (which is usually used to create a dynamic array slice over the data in a static array).
Unlike many other languages, D has a dedicated operator for concatenation, ~. Two arrays are concatenated as follows:
int[] c = a ~ b;
Unlike slicing or resizing, concatenation will always result in memory allocation. Since D is a garbage-collected language, programmers are free to use slicing and concatenation at will without having to worry about deallocating unused array contents. Similarly, an object can maintain a reference to an array or a slice of it for an indefinite time, without having to worry that the component which originally allocated the array would deallocate it, leaving a dangling pointer.
Array appending is similar to concatenation:
c ~= b;
Although intuitively similar to c = c ~ b, it is different in that it will not always allocate memory. When appending to an array, the garbage collector can check if there is enough free memory after the end of the array, and extend the array in-place. Similarly, the GC may decide to extend the array by a size larger than the item or array being appended, to optimize further appending operations.
Duplication simply creates a copy of an array in memory, and is done using the .dup property:
int[] d = c.dup;

Explicit deallocation of arrays is not necessary
A class of array operations unrelated to memory management but still worth noting is vector operations. A vector operation is indicated by the slice operator appearing on the left of the assignment operator (or a combined assignment/operation operator, such as +=). Vector operations allow the compiler to apply platform-specific optimizations, such as generating MMX/SIMD processor operands. For example:
21
d[] = c[] + 5;
This will copy in d all elements in the c array, added with 5.
The D language does not have a distinct “string” data type. Instead, it uses character arrays to represent strings. String literals, such as those in C/C++, are represented internally as static arrays of characters. The “string” alias This makes redundant several string functions present in other languages used for operations such as concatenation or substring slicing.

Associative arrays
Associative arrays, sometimes also called “hashtables” due to the use of hash functions in most implementations, represent a mapping from objects of one type to another. It is used to associate certain information with values that do not form a contiguous range – for example, given a list of students, one can associate their group name with the student name.
The advantage of associated arrays is that both insertion and lookup are done very quickly, and do not require iterating over every element to find the needed one. D has built-in support for associative arrays. The syntax is as follows:
int[string] aa;
The type before the brackets is the type of the value; the type inside the brackets is the type of the key.

1 Garbage Collection
1.1 History
1.2 Advantages
1.3 Performance
1.4 Downsides
1.5 Classification
2 D Programming Language
2.1 Overview
2.2 Notable features
2.2.1 Dynamic arrays
2.2.2 Associative arrays
2.3 D Garbage Collector
2.3.1 Overview
2.3.2 Memory layout
2.3.3 Memory operations
2.3.4 Garbage collection
3 Problems and solutions
3.1 Performance
3.2 Memory leaks
3.2.1 Description
3.2.2 Resolution
3.3 Memory corruption
3.3.1 Dangling pointers
3.3.2 Double free bugs
4 Diamond Memory Debugger
4.1 Overview
4.2 Module
4.2.1 Usage
4.2.2 Implementation
4.2.3 Logging
4.3 Analyzer
4.3.1 Overview
4.3.2 Commands
4.3.3 Memory map
4.4 Solving memory leaks
4.4 Practical applications
4.4.1 WebSafety Scanner
4.4.2 Internet data proxy
5. Economic aspects of the project
5.1 Description of the project
5.2 SWOT Analysis
5.3 Diamond advantages
5.4 Time management of the project
5.5 Project cost estimation
5.5.1 Material expenditures (consumables, raw)
5.5.2 Wage expenditures
5.5.3 Indirect expenditures
5.5.4 Calculation of the obsolescence of material assets
5.6 Conclusion
6 Labor and environment protection
7 Future Plans
7.1 Garbage collection
7.2 Memory debugging
Conclusions
Bibliography

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *