diff --git a/slides/genericite.md b/slides/genericite.md index 6e7d5f44c710ce0ccd8f624c92bb77d278a180f2..29d9c3e0bd06bc30d519c546660d373a5cba0fcd 100644 --- a/slides/genericite.md +++ b/slides/genericite.md @@ -47,6 +47,41 @@ date: "2022-02-22" ``` * À la programmeuse de faire attention à ce qu'elle fait. +## Exemple + +```C +struct tab { + int *t; +} +struct tab *tmp = malloc(); +tmp->t = malloc(); +free(tmp); // memory leak of tmp->t... +``` + +# Exemple simple + +* On souhaite échanger deux pointeurs + + ```C + int *a = malloc(); + int *b = malloc(); + swap(&a, &b); + ``` +* Comment écrire `swap()` pour que le code ci-dessus marche pour n'importe quel + type? + +. . . + +```C +void swap(void **a, void **b) { + void *tmp = *a; + *a = *b; + *b = tmp; +} +``` + + + # Cas d'utilisation (1/4) \footnotesize