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
cccd3332
Verified
Commit
cccd3332
authored
2 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
typos
parent
a4876fb1
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/fonctions_dordre_superieur.md
+56
-18
56 additions, 18 deletions
slides/fonctions_dordre_superieur.md
with
56 additions
and
18 deletions
slides/fonctions_dordre_superieur.md
+
56
−
18
View file @
cccd3332
...
...
@@ -218,22 +218,60 @@ vec3 add(vec3 lhs, vec3 rhs){
# Le map
```
c
typedef
double
(
*
operator
)(
double
);
double
*
map
(
operator
op
,
double
*
tab
,
size_t
size
)
{
double
*
res
=
malloc
(
sizeof
(
*
res
)
*
size
);
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
res
[
i
]
=
op
(
tab
[
i
]);
*
Exemple d'application
```c
typedef double (*operator)(double);
double *map(operator op, double *tab, size_t size) {
double *res = malloc(sizeof(*res) * size);
for (int i = 0; i < size; ++i) {
res[i] = op(tab[i]);
}
return res;
}
return
res
;
}
double
add_one
(
double
val
)
{
return
val
+
1
;
}
double
sqr
(
double
lhs
){
return
apply_operator
(
add_dbl
,
lhs
,
rhs
);
}
double
tab
[]
=
{
1
.
0
,
2
.
0
,
3
.
0
};
double
*
square
=
map
(
sqr
,
tab
,
3
);
double
*
and_one
=
map
(
add_one
,
square
,
3
);
```
\ No newline at end of file
double add_one(double val) {
return val + 1;
}
double sqr(double val){
return val * val;
}
double tab[] = {1.0, 2.0, 3.0};
double *square = map(sqr, tab, 3);
double *and_one = map(add_one, square, 3);
```
# Le map
*
Permettrait le chaînage.
```C
double *sqr_and_one = map(add_one, map(sqr, tab, 3), 3);
```
. . .
*
Problème?
. . .
*
Allocation dynamique... fuite mémoire.
. . .
*
Solution?
. . .
```c
typedef double (*operator)(double);
double *map(operator op, double *tab, size_t size) {
double *res = malloc(sizeof(*res) * size);
for (int i = 0; i < size; ++i) {
res[i] = op(tab[i]);
}
free(tab);
return res;
}
```
*
Problème potentiel?
*
**Attention au double free!**
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