Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
algoprog-etu-22-23
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
aliya.myaz
algoprog-etu-22-23
Commits
05f26c44
Commit
05f26c44
authored
2 years ago
by
Pierre Kunzli
Browse files
Options
Downloads
Patches
Plain Diff
ajout correction partielle serie 8
parent
052c1dfe
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Programmation/Exercices/serie_08_prog1_corr.c
+34
-0
34 additions, 0 deletions
Programmation/Exercices/serie_08_prog1_corr.c
Programmation/Exercices/serie_08_prog2_corr.c
+46
-0
46 additions, 0 deletions
Programmation/Exercices/serie_08_prog2_corr.c
with
80 additions
and
0 deletions
Programmation/Exercices/serie_08_prog1_corr.c
0 → 100644
+
34
−
0
View file @
05f26c44
#include
<stdio.h>
void
foo
(
int
a
)
{
a
+=
3
;
}
void
bar
(
int
*
a
)
{
*
a
+=
3
;
}
void
baz
(
int
*
a
)
{
a
+=
3
;
}
int
main
()
{
int
a
=
5
;
printf
(
"%i
\n
"
,
a
);
// la fonction foo ne peut pas modifier son parametre
// c'est donc normal que a ne soit pas modifié
foo
(
a
);
printf
(
"%i
\n
"
,
a
);
// la fonction bar prend un pointeur sur a
// ce qui lui permet de modifier a
bar
(
&
a
);
printf
(
"%i
\n
"
,
a
);
// la fonction baz pourrait modifier a
// mais son code modifie uniquement son pointeur
// en interne de la fonction
baz
(
&
a
);
printf
(
"%i
\n
"
,
a
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Programmation/Exercices/serie_08_prog2_corr.c
0 → 100644
+
46
−
0
View file @
05f26c44
#include
<stdio.h>
#include
<stdlib.h>
// le tableau statique tab[a] est désalloué à la fin de la fonction
// on le remplace par une allocation dynamique
int
*
foo
(
int
a
)
{
int
*
tab
=
malloc
(
a
*
sizeof
(
int
));
for
(
int
i
=
0
;
i
<
a
;
++
i
)
{
tab
[
i
]
=
2
;
}
return
tab
;
}
// l'allocation était de a octets au lieu de a*sizeof(int) octets
int
*
bar
(
int
a
)
{
int
*
tab
=
malloc
(
a
*
sizeof
(
int
));
for
(
int
i
=
0
;
i
<
a
;
++
i
)
{
tab
[
i
]
=
2
;
}
return
tab
;
}
// tab était réalloué à l'intérieu de baz sans que cela soit visible a l'exterieur
// cela a pour conséquence de ne pas initialiser l'espace mémoire vu a l'extérieur
// et de provoquer une fuite mémoire
void
baz
(
int
*
tab
,
int
b
)
{
for
(
int
i
=
0
;
i
<
b
;
++
i
)
{
tab
[
i
]
=
2
;
}
}
int
main
()
{
int
*
a
=
foo
(
4
);
printf
(
"%d
\n
"
,
a
[
3
]);
int
*
b
=
bar
(
4
);
printf
(
"%d
\n
"
,
b
[
3
]);
int
*
c
=
malloc
(
4
*
sizeof
(
int
));
baz
(
c
,
4
);
printf
(
"%d
\n
"
,
c
[
3
]);
free
(
a
);
free
(
b
);
free
(
c
);
}
\ No newline at end of file
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