Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours_prog
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
Package Registry
Model registry
Operate
Environments
Terraform modules
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
yassin.elhakoun
cours_prog
Commits
ae80d510
Verified
Commit
ae80d510
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added opaque slides
parent
ceb34063
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/opaque.md
+99
-0
99 additions, 0 deletions
slides/opaque.md
with
99 additions
and
0 deletions
slides/opaque.md
0 → 100644
+
99
−
0
View file @
ae80d510
---
title
:
"
Types
opaques"
date
:
"
2022-01-18"
patat
:
wrap
:
true
margins
:
left
:
10
right
:
10
---
# 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!
*
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));
}
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()
}
```
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