Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
programmation_sequentielle
cours
Commits
63d80bac
Verified
Commit
63d80bac
authored
4 weeks ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
updated 2025
parent
f28633c8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#38071
passed
4 weeks ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/opaque.md
+153
-0
153 additions, 0 deletions
slides/opaque.md
with
153 additions
and
0 deletions
slides/opaque.md
0 → 100644
+
153
−
0
View file @
63d80bac
---
title
:
"
Types
opaques"
date
:
"
2025-02-28"
---
# Types composés
*
Jusqu'ici les
`struct`
sont dans les
`.h`
et sont
*transparents*
```
C
// table.h
typedef struct _table {
int *data;
int length;
} table;
// main.c
table tab; // membres de tab accessibles directement
tab.length = 10;
tab.data = malloc(tab.length * sizeof(int));
tab.data[9] = 10;
```
# Types opaques
*
Afin de cacher les détails de l'implémentation.
*
Afin d'éviter les modifications directs des données.
*
Afin de protéger le monde de la dévastation!
*
Définition de types
**opaques**
:
*
Variables dans les structures ne sont pas accessibles.
*
Variables dans les structures ne sont pas modifiables.
*
Les variables ne sont même pas connues.
*
Nécessité de passer par des fonctions pour initialiser/modifier les instances
de types opaques.
*
Très souvent utilisés pour les structures de données abstraites (table de
hachage, pile, file, ...).
# Utilisation d'un type opaque: problème?
*
Dans
`opaque.h`
```C
struct table;
```
*
Dans
`opaque.c`
```C
struct table {
int a;
}
```
*
Dans
`main.c`
```C
int main() {
struct table t;
}
// error: storage size of ‘t’ isn’t known
```
*
La taille de
`table`
n'est pas connue à la compilation!
*
Comment faire?
# Utilisation d'un type opaque: pointeur!
\f
ootnotesize
*
Dans
`opaque.h`
```C
struct table;
struct table *create();
void init(struct table **t);
```
*
Dans
`opaque.c`
```C
struct table {
int a;
}
struct table *create() {
struct table *t = malloc(sizeof(*t));
return t;
}
void init(struct table **t) {
*t = malloc(sizeof(**t));
}
```
*
Dans
`main.c`
```C
int main() {
struct table *t = create();
init(&t);
t->a = 2; // Interdit, set(2)
printf("%d\n", t->a); // Interdit, get()
}
```
# Un peu plus joli: typedef! (1/2)
*
Dans
`opaque.h`
```C
struct _table;
typedef struct _table * table;
void init(table *t);
void set_a(table t, int a);
int get_a(table t);
```
*
Dans
`opaque.c`
```C
struct _table {
int a;
}
void init(table *t) {
*t = malloc(sizeof(**t));
(*t)->a = 0;
}
void set_a(table t, int a) {
t->a = a;
}
int get_a(table t) {
return t->a;
}
```
# Un peu plus joli: typedef! (2/2)
*
Dans
`main.c`
```C
int main() {
table t;
init(&t);
set_a(t, 10);
printf("%d\n", get_a(t));
}
```
*
On a fait les fonctions
`get_a()`
et
`set_a()`
comme exemples, mais...
. . .
*
c'est pas forcément nécessaire d'implémenter (
`get/set`
).
. . .
*
Par exemple, pour la hashmap on
`get/set`
les variables des structs!
## Yaka
*
Utiliser les types opaques pour la hashmap!
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment