Skip to content
Snippets Groups Projects
Verified Commit cc85192b authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added dyn alloc

parent 41c38426
No related branches found
No related tags found
No related merge requests found
---
title: "Allocation dynamique de mémoire"
date: "2021-11-09"
patat:
wrap: true
margins:
left: 10
right: 10
---
# Allocation dynamique de mémoire (1/8)
- La fonction `malloc`{.C} permet d'allouer dynamiquement (pendant l'exécution du programme) une zone de mémoire contiguë.
```C
#include <stdlib.h>
void *malloc(size_t size);
```
- `size`{.C} est la taille de la zone mémoire **en octets**.
- Retourne un pointeur sur la zone mémoire ou `NULL`{.C} en cas d'échec: **toujours vérifier** que la valeur retournée est `!= NULL`{.C}.
# Allocation dynamique de mémoire (2/8)
- On peut allouer et initialiser une `fraction_t`{.C}:
```C
fraction_t *num = malloc(sizeof(fraction_t));
num->num = 1;
num->denom = -1;
```
- La zone mémoire **n'est pas** initialisée.
- Désallouer la mémoire explicitement $\Rightarrow$ **fuites mémoires**.
<!-- - Toujours garder un pointeur sur la mémoire allouée sinon **pointeur pendouillant**. -->
![La représentation mémoire de `fraction_t` et fuites.](figs/pointer_struct_ok.svg){width=100%}
# Allocation dynamique de mémoire (3/8)
- La fonction `free()`{.C} permet de libérer une zone préalablement allouée avec `malloc()`{.C}.
```C
#include <stdlib.h>
void free(void *ptr);
```
- Pour chaque `malloc()`{.C} doit correspondre exactement un `free()`{.C}.
- Si la mémoire n'est pas libérée: **fuite mémoire** (l'ordinateur plante quand il y a plus de mémoire).
- Si la mémoire est **libérée deux fois**: *seg. fault*.
- Pour éviter les mauvaises surprises mettre `ptr`{.C} à `NULL`{.C} après
libération.
# Allocation dynamique de mémoire (4/8)
## Tableaux dynamiques
- Pour allouer un espace mémoire de 50 entiers: un tableau
```C
int *p = malloc(50 * sizeof(int));
for (int i = 0; i < 50; ++i) {
p[i] = 0;
}
```
## Arithmétique de pointeurs
- Parcourir la mémoire différemment qu'avec l'indexation
```C
int *p = malloc(50 * sizeof(int));
// initialize somehow
double a = p[7];
double b = *(p + 7); // on avance de 7 "double"
p[0] == *p; // le pointeur est le premier élément
```
# Allocation dynamique de mémoire (5/8)
## Arithmétique de pointeurs
![L'arithmétique des pointeurs.](figs/pointer_arithmetics.svg){width=100%}
## Quelle est la complexité de l'accès à une case d'un tableau?
. . .
$$
\mathcal{O}(1).
$$
# Allocation dynamique de mémoire (6/8)
## Pointeur de pointeur
- Tout comme une valeur a une adresse, un pointeur a lui-même une adresse:
```C
int a = 2;
int *b = &a;
int **c = &b;
```
- Chaque `*`{.C} ou `&`{.C} rajoute une indirection.
# Allocation dynamique de mémoire (7/8)
## Pointeur de pointeur
![Les références de pointeurs.](figs/double_pointeur.svg){height=100%}
# Allocation dynamique de mémoire (8/8)
- Avec `malloc()`, on peut allouer dynamiquement des tableaux de pointeurs:
```C
int **p = malloc(50 * sizeof(int*));
for (int i = 0; i < 50; ++i) {
p[i] = malloc(70 * sizeof(int));
}
int a = p[5][8]; // on indexe dans chaque dimension
```
- Ceci est une matrice (un tableau de tableau).
# Les *sanitizers*
Problèmes mémoire courants:
* Dépassement de capacité de tableaux.
* Utilisation de mémoire non allouée.
* Fuites mémoire.
* Double libération.
Outils pour leur détection:
* Valgrind (outil externe).
* Sanitizers (ajouts de marqueurs à la compilation).
Ici on utilise les sanitizers (modification de la ligne de compilation):
```bash
gcc -o main main.c -g -fsanitize=address -fsanitize=leak
```
**Attention:** Il faut également faire l'édition des liens avec les sanitizers.
This diff is collapsed.
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="245.24887mm"
height="86.080841mm"
viewBox="0 0 245.24887 86.080843"
version="1.1"
id="svg8"
inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)"
sodipodi:docname="pointer_struct_ok.svg">
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.98994949"
inkscape:cx="483.38263"
inkscape:cy="105.54084"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="1884"
inkscape:window-height="1052"
inkscape:window-x="36"
inkscape:window-y="0"
inkscape:window-maximized="1"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0">
<inkscape:grid
originy="-104.07588"
originx="12.489411"
type="xygrid"
id="grid819" />
</sodipodi:namedview>
<defs
id="defs2">
<marker
inkscape:isstock="true"
style="overflow:visible"
id="marker3314"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend">
<path
inkscape:connector-curvature="0"
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path3312" />
</marker>
<marker
inkscape:isstock="true"
style="overflow:visible"
id="marker3284"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend">
<path
inkscape:connector-curvature="0"
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path3028" />
</marker>
<marker
inkscape:isstock="true"
style="overflow:visible"
id="Arrow2Lstart"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lstart">
<path
inkscape:connector-curvature="0"
transform="matrix(1.1,0,0,1.1,1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path3025" />
</marker>
<marker
inkscape:isstock="true"
style="overflow:visible"
id="Arrow2Lend"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend">
<path
inkscape:connector-curvature="0"
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path8951" />
</marker>
<marker
inkscape:isstock="true"
style="overflow:visible"
id="Arrow2Lend-7"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path8951-5"
inkscape:connector-curvature="0" />
</marker>
<marker
inkscape:isstock="true"
style="overflow:visible"
id="Arrow2Lend-9"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path8951-8"
inkscape:connector-curvature="0" />
</marker>
<marker
inkscape:isstock="true"
style="overflow:visible"
id="Arrow2Lend-7-2"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path8951-5-8"
inkscape:connector-curvature="0" />
</marker>
<marker
inkscape:isstock="true"
style="overflow:visible"
id="Arrow2Lend-1"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path8951-3"
inkscape:connector-curvature="0" />
</marker>
<marker
inkscape:isstock="true"
style="overflow:visible"
id="marker3284-4"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend">
<path
inkscape:connector-curvature="0"
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
id="path3028-7" />
</marker>
</defs>
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
transform="translate(12.489412,-106.8433)"
id="layer1"
inkscape:groupmode="layer"
inkscape:label="Layer 1">
<g
style="fill:none;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="g846-7-2"
transform="matrix(0.48784069,0,0,1,-44.968279,-20.032731)">
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-3-5-2"
width="29.104166"
height="9.260417"
x="99.218758"
y="156.77083" />
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-6-3-8"
width="29.104166"
height="9.260417"
x="128.32292"
y="156.77083" />
</g>
<g
style="fill:none;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="g846-5-1"
transform="matrix(0.48784069,0,0,1,-16.571887,-20.032726)">
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-3-6-9"
width="29.104166"
height="9.260417"
x="99.218758"
y="156.77083" />
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-6-2-4"
width="29.104166"
height="9.260417"
x="128.32292"
y="156.77083" />
</g>
<g
id="g1489"
transform="translate(-82.051746)">
<g
transform="matrix(0.4878407,0,0,1,93.949527,-20.032732)"
id="g846-7-2-6"
style="fill:none;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<rect
y="156.77083"
x="99.218758"
height="9.260417"
width="29.104166"
id="rect821-3-5-2-7"
style="opacity:1;fill:none;fill-opacity:1;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
<rect
y="156.77083"
x="128.32292"
height="9.260417"
width="29.104166"
id="rect821-6-3-8-5"
style="opacity:1;fill:none;fill-opacity:1;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
</g>
<g
transform="matrix(0.4878407,0,0,1,122.34592,-20.032727)"
id="g846-5-1-9"
style="fill:none;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<rect
y="156.77083"
x="99.218758"
height="9.260417"
width="29.104166"
id="rect821-3-6-9-1"
style="opacity:1;fill:none;fill-opacity:1;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
<rect
y="156.77083"
x="128.32292"
height="9.260417"
width="29.104166"
id="rect821-6-2-4-2"
style="opacity:1;fill:none;fill-opacity:1;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
</g>
</g>
<path
sodipodi:nodetypes="cc"
inkscape:connector-curvature="0"
id="path2993"
d="M -1.3502974,178.91256 C -10.434806,165.66351 -12.664615,164.59117 0.16160744,145.99852"
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#marker3284)" />
<path
sodipodi:nodetypes="cc"
inkscape:connector-curvature="0"
id="path2993-6"
d="M -1.6432048,189.1699 C -12.064062,177.52447 -18.226203,141.46875 -3.5290926,116.46165"
style="fill:none;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#marker3284-4)" />
<g
id="g1518"
transform="translate(3.2072343)">
<g
transform="matrix(0.48784068,0,0,1,-23.535649,-49.569597)"
id="g846-7-2-5"
style="fill:none;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.7158648, 1.43172961;stroke-dashoffset:0;stroke-opacity:1">
<rect
y="156.77083"
x="99.218758"
height="9.260417"
width="29.104166"
id="rect821-3-5-2-6"
style="opacity:1;fill:none;fill-opacity:1;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.7158648, 1.43172961;stroke-dashoffset:0;stroke-opacity:1" />
<rect
y="156.77083"
x="128.32292"
height="9.260417"
width="29.104166"
id="rect821-6-3-8-9"
style="opacity:1;fill:none;fill-opacity:1;stroke:#0000ff;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.7158648, 1.43172961;stroke-dashoffset:0;stroke-opacity:1" />
</g>
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#0000ff;stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:0.50000001, 1.00000003;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-3-6-7-37"
width="14.198196"
height="9.260417"
x="-3.5290926"
y="107.20123" />
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#0000ff;stroke-width:0.5;stroke-miterlimit:4;stroke-dasharray:0.50000001, 1.00000003;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-6-2-3-4"
width="14.198196"
height="9.260417"
x="10.669104"
y="107.20123" />
<g
transform="translate(-85.25898)"
id="g1481">
<g
style="fill:none;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.71586478, 1.43172956;stroke-dashoffset:0;stroke-opacity:1"
id="g846-7-2-6-4"
transform="matrix(0.48784071,0,0,1,90.258825,-49.569599)">
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.71586478, 1.43172956;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-3-5-2-7-3"
width="29.104166"
height="9.260417"
x="99.218758"
y="156.77083" />
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.71586478, 1.43172956;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-6-3-8-5-0"
width="29.104166"
height="9.260417"
x="128.32292"
y="156.77083" />
</g>
<g
style="fill:none;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.71586478, 1.43172956;stroke-dashoffset:0;stroke-opacity:1"
id="g846-5-1-9-8"
transform="matrix(0.48784071,0,0,1,118.65522,-49.569594)">
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.71586478, 1.43172956;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-3-6-9-1-4"
width="29.104166"
height="9.260417"
x="99.218758"
y="156.77083" />
<rect
style="opacity:1;fill:none;fill-opacity:1;stroke:#ff0007;stroke-width:0.71586478;stroke-miterlimit:4;stroke-dasharray:0.71586478, 1.43172956;stroke-dashoffset:0;stroke-opacity:1"
id="rect821-6-2-4-2-3"
width="29.104166"
height="9.260417"
x="128.32292"
y="156.77083" />
</g>
</g>
</g>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:9.61108494px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;stroke-width:0.24027713"
x="2.3449361"
y="132.47079"
id="text1455"><tspan
sodipodi:role="line"
id="tspan1453"
x="2.3449361"
y="132.47079"
style="stroke-width:0.24027713">fraction_t</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:10.58333302px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;stroke-width:0.26458332"
x="2.2523637"
y="155.76122"
id="text1459"><tspan
sodipodi:role="line"
id="tspan1457"
x="2.2523637"
y="155.76122"
style="stroke-width:0.26458332">num</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:10.58333302px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;stroke-width:0.26458332"
x="59.490494"
y="155.76122"
id="text1463"><tspan
sodipodi:role="line"
id="tspan1461"
x="59.490494"
y="155.76122"
style="stroke-width:0.26458332">denom</tspan></text>
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:8.47521019px;line-height:1.25;font-family:monospace;-inkscape-font-specification:monospace;letter-spacing:0px;word-spacing:0px;stroke-width:0.21188024"
x="0.21260698"
y="181.14244"
id="text1467"><tspan
sodipodi:role="line"
id="tspan1465"
x="0.21260698"
y="181.14244"
style="stroke-width:0.21188024">fraction_t *frac = malloc(sizeof(fraction_t));</tspan><tspan
sodipodi:role="line"
x="0.21260698"
y="191.73645"
style="stroke-width:0.21188024"
id="tspan1471">frac = NULL; // addresse mémoire inaccessible</tspan></text>
</g>
</svg>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment