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
adf393af
Verified
Commit
adf393af
authored
2 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
ajout fontions
parent
dba8a816
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/allocation_dynamique.md
+53
-1
53 additions, 1 deletion
slides/allocation_dynamique.md
with
53 additions
and
1 deletion
slides/allocation_dynamique.md
+
53
−
1
View file @
adf393af
...
...
@@ -116,6 +116,58 @@ $$
-
Ceci est une matrice (un tableau de tableau).
# Tableau dynamique en argument d'une fonction
## Implémenter la fonction ci-dessous
```
C
int32_t *p = malloc(50 * sizeof(*p));
initialize_to(p, 50, -1); // initialise un tableau à -1
free(p); // ne pas oublier
```
. . .
```
C
void initialize_to(int32_t *p, size_t size, int32_t val) {
for (size_t i = 0; i < size; ++i) {
p[i] = val;
}
}
```
# Tableau dynamique retourné d'une fonction
## Implémenter la fonction ci-dessous
```
C
// alloue un tableau de taille 50 et l'initilise à -1
int32_t *p = initialize_to(p, 50, -1);
free(p); // ne pas oublier
```
. . .
```
C
uint32_t initialize_to(int32_t *p, size_t size, int32_t val) {
int32_t *p = malloc(size * sizeof(*p));
for (size_t i = 0; i < size; ++i) {
p[i] = val;
}
return p;
}
```
## Pourquoi on peut retourner un tableau dynamique et pas un statique?
. . .
*
Le tableau est alloué sur le
**tas**
et non sur la
**pile**
.
*
La mémoire est gérée manuellement sur le tas, automatiquement sur la pile.
# Les *sanitizers*
Problèmes mémoire courants:
...
...
@@ -130,7 +182,7 @@ Outils pour leur détection:
*
Valgrind (outil externe).
*
Sanitizers (ajouts de marqueurs à la compilation).
Ici on utilise les sanitizers (modification de la ligne de compilation):
Ici on utilise les sanitizers (modification de la ligne de compilation
, modifiez les
*Makefile*
):
```
bash
gcc
-o
main main.c
-g
-fsanitize
=
address
-fsanitize
=
leak
...
...
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