# Variable-length arrays

:::::{activity} Array causes segmentation fault
Left code causes a segmentation error, but the right one runs fine.
1. What is different between two programs?
1. What could be the problem?

::::{grid} 2
:::{grid-item-card} 💻💥😵‍💫
```{literalinclude} ../code-error/stack_overflow.c
:language: c
```
Output:
```text
segmentation fault (core dumped)
```
:::
:::{grid-item-card} 😌
```{literalinclude} ../code/stack_overflow_fixed.c
:language: c
```
Output:
```{literalinclude} ../code/stack_overflow_fixed.txt
:language: text
```
:::
::::
:::::

(static-and-stack-based-memory)=
## Static- and stack-based memory
<!--, heap-based-->

<!-- TODO make this example much shorter, it is too complex, just show, here it contains garbage usw -->
:::{activity} 🤔 Question to ponder
`capitalize_and_print()`{l=c} capitalizes a string and prints it. The following implementation uses a separate memory for output and input. The two examples use static- and stack-based memory.

Look at the outputs of programs below.

1. Programs' outputs are not correct. What could be the reason?
1. Where could the difference between the static and stack version come from?
<!-- 1. after the first try, static version does not work, because memory is not filled with zero.
2. stack memory is unitialized-->
:::

::::{grid} 2
:::{grid-item-card} static
```{literalinclude} ../code-wi/array_static.c
:language: c
:emphasize-lines: 13
```
```{command-output} printf "l3v3rP05tej\nhello" | code-wi/array_static.exe
:shell:
```
:::
:::{grid-item-card} stack
```{literalinclude} ../code-wi/array_stack.c
:language: c
:emphasize-lines: 16
```
```{command-output} printf "l3v3rP05tej\nhello" | code-wi/array_stack.exe
:shell:
```
:::
::::
<!--
:::{card} heap
```{literalinclude} ../code-wi/array_heap.c
:language: c
:emphasize-lines: 16,25
```
```{command-output} printf "l3v3rP05tej\nhello" | code-wi/array_heap.exe
:shell:
```
:::
-->


<!--TODO find a representation where static variables, code, data etc are stored-->

:::{commons-figure} https://commons.wikimedia.org/wiki/File:ProgramCallStack2_en.svg
:name: stack-based-memory
:figwidth: 45%
:align: right
A representation of the stack memory. Whenever a function is called, then a new frame is created with arguments, data, and return address, which returns back to the place where the function was called – `FRAME N-1` for example.
:::

:::{wpd} static variable
a variable that lives for the entire time of a program.
:::
:::{wpd} automatic variable
a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope.

Automatic variables are typically allocated in the stack memory.
:::
:::{wpd} stack
:id: stack-based memory allocation
regions of memory where data is added or removed in last-in-first-out manner.
:::

Stack-based memory is automatically *allocated* and *released* as shown in {numref}`stack-based-memory`. To *allocate* simply means to *reserve*.

## Initializing variable-length arrays

```c
void f(size_t size) {
    int arr[size] = {};  // {} initializes to zero
    
    // Has the same effect:
    for (size_t i = 0; i < size; ++i)
      arr[i] = 0;
}
```

:::{card} 🤔 Question to ponder
Where can the initialization help in the stack-based code above?
:::

:::{card} ⚡ Live programming
We will write a function that marks the squares on a board where a knight can move and prints it
void print_knights_moves(size_t x, size_t y, size_t board_size);
:::
<!--
:::{card} ⚡ Live programming
Create a variable-length two dimensional array and fill it with incrementing numbers. Then print the array and return.
```c
void print_two_dimensional_array(size_t length, size_t width);
```
:::
-->