commit
26f583ec5e
6 changed files with 88 additions and 0 deletions
@ -0,0 +1 @@ |
|||
_build |
@ -0,0 +1 @@ |
|||
# Garbage |
@ -0,0 +1,3 @@ |
|||
(lang dune 3.0) |
|||
|
|||
(using mdx 0.2) |
@ -0,0 +1,55 @@ |
|||
# Garbage |
|||
|
|||
## Allocation mémoire |
|||
|
|||
### Allocation statique |
|||
|
|||
On peut allouer de la mémoire statiquement à deux endroits : |
|||
|
|||
- dans le segment `.data` lors d'une initialisation explicite, |
|||
- dans le segment `.bss` lors d'une initialisation par zéro. |
|||
|
|||
<!-- $MDX file=memory_layout.c --> |
|||
```c |
|||
// memory_layout.c |
|||
|
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
|
|||
const int x = 3; |
|||
|
|||
int y = 5; |
|||
|
|||
static int i; |
|||
|
|||
char *my_str = "this should appear in .data"; |
|||
|
|||
int main(void) { |
|||
|
|||
printf("x = %d\n", x); |
|||
|
|||
printf("y = %d\n", y); |
|||
y = 10; |
|||
printf("y = %d\n", y); |
|||
|
|||
printf("i = %d\n", i); |
|||
i = 42; |
|||
printf("i = %d\n", i); |
|||
|
|||
exit(0); |
|||
} |
|||
``` |
|||
|
|||
```sh |
|||
$ gcc memory_layout.c -o memory_layout.exe |
|||
$ size ./memory_layout.exe |
|||
text data bss dec hex filename |
|||
1790 608 8 2406 966 ./memory_layout.exe |
|||
``` |
|||
|
|||
### MDX test |
|||
|
|||
```ocaml |
|||
# 1 + 2;; |
|||
- : int = 3 |
|||
``` |
@ -0,0 +1 @@ |
|||
(mdx) |
@ -0,0 +1,27 @@ |
|||
// memory_layout.c
|
|||
|
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
|
|||
const int x = 3; |
|||
|
|||
int y = 5; |
|||
|
|||
static int i; |
|||
|
|||
char *my_str = "this should appear in .data"; |
|||
|
|||
int main(void) { |
|||
|
|||
printf("x = %d\n", x); |
|||
|
|||
printf("y = %d\n", y); |
|||
y = 10; |
|||
printf("y = %d\n", y); |
|||
|
|||
printf("i = %d\n", i); |
|||
i = 42; |
|||
printf("i = %d\n", i); |
|||
|
|||
exit(0); |
|||
} |
Loading…
Reference in new issue