Storage durations.

main
Joshua Potter 2024-12-01 09:45:06 -07:00
parent 8bee306505
commit 868ea3a343
7 changed files with 626 additions and 11 deletions

View File

@ -747,7 +747,7 @@
"_journal/2024-08/2024-08-01.md": "2e3da352cfbaf29b6b49e3c3a4f090df",
"_journal/2024-08-04.md": "f3675ba4cd33f16a5695732bbe10473e",
"_journal/2024-08/2024-08-03.md": "7c1d3dbaf47d1120bb879110e827d1b3",
"c17/pointers.md": "e7aa88f6062bc9502d10faf930bc6f38",
"c17/pointers.md": "0844c30d3617b0596bca31598de3e525",
"_journal/2024-08-07.md": "0a71a471b2eb6b3d45c663e1eaa553e9",
"_journal/2024-08/2024-08-06.md": "ecb72760daef7db26825c59add0dc040",
"_journal/2024-08/2024-08-05.md": "632d0ccd84817d8f2b22275df6d433c0",
@ -799,7 +799,7 @@
"c17/enums.md": "9414fb67aa256a0a11b7240534c67bf6",
"c17/derived-types.md": "6fb8f23a2423f05d5bdccb6672a32e38",
"c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875",
"c17/types/simple.md": "07bc1e1d1e6764353e99ea018a37f781",
"c17/types/simple.md": "7ee25535ade863ca90aad5d9bcadaa6c",
"c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d",
"c17/types/derived.md": "1a723c3208c2f926c6111d83a6c4c678",
"c17/types/basic.md": "5064e21e683c0218890058882e06b6f3",
@ -962,16 +962,18 @@
"_journal/2024-11-26.md": "29bc0b54d23034b9108e567a1d5fa8ac",
"_journal/2024-11/2024-11-25.md": "1ec17a8473fa9c4779090ecbd22d70ef",
"calculus/intervals.md": "3c2f6803bf7f89a626b76c464bd715e1",
"c17/storage.md": "2aeaf297a91322c6ee0423f051ed4a7f",
"c17/storage.md": "636666dc7b9ca12df125d276ccf51cba",
"c17/functions.md": "139184771c76ff16bcc23b1a2f87c329",
"_journal/2024-11-27.md": "5a58e3a0bd08c689d30600021937495d",
"_journal/2024-11/2024-11-26.md": "af7573eb695b61106e04c9f1aee51cc3",
"_journal/2024-11-28.md": "4321856aec890b179f1b569b908bba60",
"c17/linkage.md": "8410396eb99da8b3f350c277db351cc7",
"_journal/2024-11-30.md": "043897ee93912cebc76924f6a8721c35",
"c17/linkage.md": "52483d5177ef425cffd7963a580fd810",
"_journal/2024-11-30.md": "c6040ba2caf1c9ff55faed25d7879e9a",
"_journal/2024-11/2024-11-29.md": "4896d849b6345010669047323521e21a",
"_journal/2024-11/2024-11-28.md": "e9d252bc4bc3d6f70db56c36e1090e86",
"_journal/2024-11/2024-11-27.md": "7f44f4c91fea0c5c05d542aeeb9e723d"
"_journal/2024-11/2024-11-27.md": "7f44f4c91fea0c5c05d542aeeb9e723d",
"_journal/2024-12-01.md": "d34aff07b7fa8a9a05f84841496d13b5",
"_journal/2024-11/2024-11-30.md": "c6040ba2caf1c9ff55faed25d7879e9a"
},
"fields_dict": {
"Basic": [

View File

@ -3,7 +3,7 @@ title: "2024-11-30"
---
- [x] Anki Flashcards
- [ ] KoL
- [x] KoL
- [x] OGS
- [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story)

View File

@ -0,0 +1,12 @@
---
title: "2024-12-01"
---
- [x] Anki Flashcards
- [x] KoL
- [x] OGS
- [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story)
* More notes on C's [[storage]] duration classes.
* Finished day 1 of [Advent of Code](https://adventofcode.com).

View File

@ -19,6 +19,12 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
<!--ID: 1733010193273-->
END%%
%%ANKI
Cloze
Linkage is a property of {identifiers}, not {objects}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).<!--ID: 1733064425171-->
END%%
%%ANKI
Basic
What capability does linkage allow identifiers to perform?
@ -145,6 +151,18 @@ Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70
<!--ID: 1733010193289-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of `x`?
```c
int x;
static int x;
```
Back: N/A. Undefined behavior.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733022430503-->
END%%
%%ANKI
Basic
What is the linkage of a function identifier with file scope and no storage-class specifier?

View File

@ -492,7 +492,7 @@ Which of the following members of the list are ICEs?
```c
0U, '\0', 0UL, (void*)0, 5LL
```
Back: `0U`, `\0`, and `0UL`.
Back: `0U`, `\0`, `0UL`, and `5LL`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644440-->
END%%

View File

@ -6,6 +6,562 @@ tags:
- c17
---
## Overview
An object has a **storage duration** that determines its **lifetime**. There are four storage durations: **static**, **thread**, **automatic**, and **allocated**.
%%ANKI
Cloze
An {object} has a {storage duration} that determines its {lifetime}.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664862-->
END%%
%%ANKI
Basic
What are the four storage durations?
Back: Static, thread, automatic, and allocated.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664868-->
END%%
%%ANKI
Basic
What is an object's lifetime?
Back: The portion of a program execution during which storage is guaranteed to be reserved for the object.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664871-->
END%%
%%ANKI
Basic
What is the result of accessing an object within its lifetime?
Back: The value last stored within the object.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664874-->
END%%
%%ANKI
Basic
What is the result of accessing an object outside its lifetime?
Back: Undefined behavior.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664877-->
END%%
### Static
An object whose identifier is declared without storage-class specifier `_Thread_local`, and either with external or internal [[linkage]] or with the storage-class specifier `static`, has static storage duration.
Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.
%%ANKI
Basic
What is the storage duration of an object whose identifer has external linkage?
Back: Either static or thread.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664881-->
END%%
%%ANKI
Basic
When; does an object whose identifier has external linkage have a static storage duration?
Back: When the identifier is declared without storage-class specifier `_Thread_local`.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664884-->
END%%
%%ANKI
Basic
When; does an object whose identifier has external linkage have a thread storage duration?
Back: When the identifier is declared with storage-class specifier `_Thread_local`.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664888-->
END%%
%%ANKI
Basic
Which storage class specifier(s) can be used to declare an object with static storage duration?
Back: `extern` and `static`.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022801-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier `x`?
```c
int x;
```
Back: Static.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664892-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier `x`?
```c
extern int x;
```
Back: Static.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664896-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier `x`?
```c
static int x;
```
Back: Static.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664899-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier `x`?
```c
_Thread_local int x;
```
Back: Thread.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664903-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier `x`?
```c
void foo() { static int x; }
```
Back: Static.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664907-->
END%%
%%ANKI
Basic
What is the lifetime of an object with static storage duration?
Back: The entire program execution.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664911-->
END%%
%%ANKI
Basic
When is an object with static storage duration initialized?
Back: Prior to program startup (before `main` is called).
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664914-->
END%%
%%ANKI
Basic
How many times can object with static storage duration be initialized?
Back: Only once.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664922-->
END%%
%%ANKI
Basic
An object with static storage duration must have what linkage?
Back: Internal or external.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022808-->
END%%
### Thread Local
An object whose identifier is declared with the storage-class specified `_Thread_local` has thread storage duration.
Its lifetime is the entire execution of the thread for which it is created, and its stored value is initialized when the thread is started.
%%ANKI
Basic
Which keyword declares an object with thread local storage duration?
Back: `_Thread_local`
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664926-->
END%%
%%ANKI
Cloze
{1:`_Thread_local`} is a {2:keyword} whereas {2:`thread_local`} is a {1:macro}.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664930-->
END%%
%%ANKI
Basic
Which library is `thread_local` defined in?
Back: `<threads.h>`
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664934-->
END%%
%%ANKI
Basic
Which storage class specifier(s) can be used to declare an object with thread storage duration?
Back: `_Thread_local`
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022811-->
END%%
%%ANKI
Basic
What is the lifetime of an object with thread storage duration?
Back: The entire execution fo the thread for which it is created.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664939-->
END%%
%%ANKI
Basic
When is an object with thread storage duration initialized?
Back: When its corresponding thread is started.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733060664943-->
END%%
%%ANKI
Basic
An object with thread storage duration must have what linkage?
Back: Internal or external.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022814-->
END%%
### Automatic
An object whose identifier is declared with no linkage and without the storage-class specifier `static` has automatic storage duration, as do some compound literals.
%%ANKI
Basic
Which keyword is used to declare automatic storage duration?
Back: `auto`
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022818-->
END%%
%%ANKI
Basic
Which storage class specifier is essentially never used in modern C?
Back: `auto`
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022821-->
END%%
%%ANKI
Basic
Which storage class specifier(s) can be used to declare an object with automatic storage duration?
Back: `auto`
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064332819-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier `x`?
```c
void foo() { int x; }
```
Back: Automatic.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022825-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier `x`?
```c
void foo(int x) { }
```
Back: Automatic.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022829-->
END%%
%%ANKI
Basic
An object with automatic storage duration must have what linkage?
Back: None.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022833-->
END%%
%%ANKI
Basic
What are the three possible lifetimes of an automatic object?
Back: Enclosing block, declaration scope, temporary.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022838-->
END%%
For these objects that do not have a [[derived#Variable-Length|VLA]] type, its lifetime extends from entry into its enclosing block until execution of that block ends. Its initial value is indeterminate. If initialization is specified, it is performed each time the declaration or compound literal is reached in the execution of the block.
For these objects that do have a VLA type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration.
%%ANKI
Basic
Consider non-temporary object with automatic storage duration. What property determines its lifetime?
Back: Whether or not the object has a VLA type.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022843-->
END%%
%%ANKI
Basic
When is the lifetime of an object with automatic storage duration that of its enclosing block?
Back: When it is a non-temporary object with non-VLA type.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022848-->
END%%
%%ANKI
Basic
Consider non-temporary, non-VLA object with automatic storage duration. What is its lifetime?
Back: From entry into its enclosing block until execution of that block ends.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022853-->
END%%
%%ANKI
Basic
When does the lifetime of an object with automatic storage duration match its declaration's scope?
Back: When it is a non-temporary object with VLA type.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022857-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the lifetime of the object with identifier `x`?
```c
void foo(int x) { }
```
Back: From start of execution to end of execution of `foo`.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022862-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the lifetime of the object with identifier `x`?
```c
void foo() {
int z;
int y;
int x;
}
```
Back: Between the start and end of `foo`'s execution.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022867-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the value of `x` at line 1?
```c
void foo() {
int z = 0; // Line 1
int y = 1; // Line 2
int x = 2; // Line 3
}
```
Back: Indeterminate.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022873-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the value of `x` at line 3?
```c
void foo() {
int z = 0; // Line 1
int y = 1; // Line 2
int x = 2; // Line 3
}
```
Back: `2`
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022877-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the lifetime of the object referred to by identifier `x`?
```c
void foo() {
int z;
{ int y; int x; }
int a;
}
```
Back: From the start to the end of execution of its surrounding block.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022882-->
END%%
%%ANKI
Basic
Consider the following translation unit. Does `x`'s lifetime extend to line 1?
```c
void foo() {
int z = 0; // Line 1
int y = 1; // Line 2
int x = 2; // Line 3
}
```
Back: Yes.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022887-->
END%%
%%ANKI
Basic
Consider the following translation unit. Does `x`'s lifetime extend to line 1?
```c
void foo() {
int z = 0; // Line 1
int y = 1; // Line 2
{ int x = 2; } // Line 3
}
```
Back: No.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022892-->
END%%
%%ANKI
Basic
Consider the following translation unit. Does `x`'s lifetime extend to line 1?
```c
void foo() {
int z = 0; // Line 1
{ int y = 1; } // Line 2
int x = 2; // Line 3
}
```
Back: Yes.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022897-->
END%%
%%ANKI
Basic
Consider non-temporary, VLA object with automatic storage duration. What is its lifetime?
Back: From declaration of the VLA to when it leaves its scope.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022901-->
END%%
%%ANKI
Basic
Consider the following translation unit. Does `x`'s lifetime extend to line 1?
```c
void foo(int n) {
int z = 0; // Line 1
int y = 1; // Line 2
int x[n]; // Line 3
}
```
Back: No.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022906-->
END%%
%%ANKI
Basic
Consider the following translation unit. Does `x`'s lifetime extend to line 3?
```c
void foo(int n) {
int x[n]; // Line 1
int z = 0; // Line 2
int y = 1; // Line 3
}
```
Back: Yes.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022912-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the lifetime of the object with identifier `x`?
```c
void foo(int n) {
int z;
int y;
int x[n];
}
```
Back: Between its declaration and when `foo`'s execution ends.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022917-->
END%%
%%ANKI
Basic
Consider the following translation unit. Does `x`'s lifetime extend to line 3?
```c
void foo(int n) {
{ int x[n]; } // Line 1
int z = 0; // Line 2
int y = 1; // Line 3
}
```
Back: No.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022923-->
END%%
%%ANKI
Basic
Consider the following translation unit. Does `x`'s lifetime extend to line 3?
```c
void foo(int n) {
int x[n]; // Line 1
int z = 0; // Line 2
{ int y = 1; } // Line 3
}
```
Back: Yes.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022928-->
END%%
A non-lvalue expression with structure or union type, where the structure or union contains a member with array type refers to an object with automatic storage duration and **temporary lifetime**. Its lifetime lasts for the duration of the expression evaluation it is found within.
%%ANKI
Basic
When is the lifetime of an object with automatic storage duration temporary?
Back: When specifying a non-lvalue expression with `struct`/`union` type containing an array member.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022933-->
END%%
%%ANKI
Basic
Consider an object with temporary lifetime and automatic storage duration. What *must* the object's type be?
Back: Either a `struct` or `union`.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022938-->
END%%
%%ANKI
Basic
Consider an object with temporary lifetime and automatic storage duration. What *must* the `struct` or `union` contain?
Back: A member with array type.
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1733064022942-->
END%%
## Dynamic Allocation
The `<stdlib.h>` header provides the two most prominent functions used for managing dynamic memory: `malloc` and `free`. The former is used to allocate new memory whereas the latter is used to annihilate it.
@ -15,6 +571,8 @@ void* malloc(size_t size);
void free(void*);
```
Dynamically allocated objects have allocated storage duration. Their lifetime spans allocation to explicit destruction.
%%ANKI
Basic
Which standard library header exposes `malloc`?
@ -41,7 +599,7 @@ END%%
%%ANKI
Basic
What two functions are most prominently used for dynamic allocation?
Which two functions are most prominently used for managing dynamically allocated memory?
Back: `malloc` and `free`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874718-->
@ -65,7 +623,7 @@ END%%
%%ANKI
Basic
On success, what does a call to `malloc` return?
Back: A `void*` pointer.
Back: A valid `void*` pointer.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874721-->
END%%
@ -227,6 +785,31 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
<!--ID: 1732755874736-->
END%%
%%ANKI
Basic
What storage duration is associated with `malloc` and `free`?
Back: Allocated.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1733064332825-->
END%%
%%ANKI
Basic
What is the lifetime of an allocated object?
Back: From its allocation to when it is explicitly deallocated.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1733064332828-->
END%%
%%ANKI
Basic
What two functions are typically used to deallocate an allocated object?
Back: `free` and `realloc`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1733064332831-->
END%%
## Bibliography
* “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).

View File

@ -290,7 +290,7 @@ END%%
%%ANKI
Basic
Which header defines `CHAR_MIN`?
Back: `limits.h`
Back: `<limits.h>`
Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
<!--ID: 1732622657247-->
END%%