C storage and linkage.

main
Joshua Potter 2024-11-30 16:43:44 -07:00
parent c3ca1f107d
commit 8bee306505
14 changed files with 801 additions and 20 deletions

View File

@ -334,10 +334,10 @@
"_journal/2024-02/2024-02-21.md": "f423137ae550eb958378750d1f5e98c7",
"_journal/2024-02-23.md": "219ce9ad15a8733edd476c97628b71fd",
"_journal/2024-02/2024-02-22.md": "312e55d57868026f6e80f7989a889c2b",
"c17/strings.md": "02f329f678efe939dbb5501399130296",
"c17/strings.md": "545706fd2296528127a50c7e7dbd7c44",
"c17/index.md": "14c1dff9cb7e0272128ac1b89b3494b9",
"c17/escape-sequences.md": "a8b99070336878b4e8c11e9e4525a500",
"c17/declarations.md": "c853b9584365ba69ac5845082061a8dd",
"c17/declarations.md": "c520b5335cbcad3cf54590bf100fafbc",
"algorithms/sorting/merge-sort.md": "6506483f7df6507cee0407bd205dbedd",
"_journal/2024-02-24.md": "9bb319d5014caf962a9ce3141076cff4",
"_journal/2024-02/2024-02-23.md": "0aad297148e8cc4058b48b7e45787ca7",
@ -563,7 +563,7 @@
"_journal/2024-05/2024-05-25.md": "3e8a0061fa58a6e5c48d12800d1ab869",
"_journal/2024-05-27.md": "b36636d10eab34380f17f288868df3ae",
"_journal/2024-05/2024-05-26.md": "abe84b5beae74baa25501c818e64fc95",
"algebra/set.md": "e31026e860182bf000af60e4b5f39413",
"algebra/set.md": "6cf503bd1118d22a45f1571057bea96d",
"algebra/boolean.md": "c9bd5abb601be6d2bedd24d5f54848b0",
"git/merge-conflicts.md": "761ad6137ec51d3877f7d5b3615ca5cb",
"_journal/2024-05-28.md": "0f6aeb5ec126560acdc2d8c5c6570337",
@ -593,7 +593,7 @@
"_journal/2024-06/2024-06-06.md": "db3407dcc86fa759b061246ec9fbd381",
"_journal/2024-06-08.md": "b20d39dab30b4e12559a831ab8d2f9b8",
"_journal/2024-06/2024-06-07.md": "c6bfc4c1e5913d23ea7828a23340e7d3",
"lambda-calculus/alpha-conversion.md": "6df655e60976715e5c6fbbe72b628c6d",
"lambda-calculus/alpha-conversion.md": "a68f3cc1565fb26335218986808a1190",
"lambda-calculus/index.md": "0e47dcd2ac0c84ad4c1a5e7609dade5f",
"x86-64/instructions/condition-codes.md": "9c05ed99f5c96162e25f0ec4db55c656",
"x86-64/instructions/logical.md": "a15c7da43cb97badef8ba4f8aadf9cbb",
@ -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": "c80957f5cab7403b85bcabcc9ce983ef",
"c17/pointers.md": "e7aa88f6062bc9502d10faf930bc6f38",
"_journal/2024-08-07.md": "0a71a471b2eb6b3d45c663e1eaa553e9",
"_journal/2024-08/2024-08-06.md": "ecb72760daef7db26825c59add0dc040",
"_journal/2024-08/2024-08-05.md": "632d0ccd84817d8f2b22275df6d433c0",
@ -801,7 +801,7 @@
"c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875",
"c17/types/simple.md": "07bc1e1d1e6764353e99ea018a37f781",
"c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d",
"c17/types/derived.md": "74c608d72b25794bc3887f58f001bab2",
"c17/types/derived.md": "1a723c3208c2f926c6111d83a6c4c678",
"c17/types/basic.md": "5064e21e683c0218890058882e06b6f3",
"c17/types/index.md": "ae2ebd695b4eeadbabf28c814ce3a052",
"_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2",
@ -920,7 +920,7 @@
"_journal/2024-10/2024-10-31.md": "8c5e70f566953974f252da9472e527f0",
"_journal/2024-10/2024-10-30.md": "054bdbf52843fa2445f6b9f91d5ca46e",
"_journal/2024-10/2024-10-29.md": "432b3b073dafd54421ff6f7182ab9a58",
"c17/alignment.md": "7f10089d8e71e0c61c20f84ff95d78a8",
"c17/alignment.md": "8d52fdf4834e9f13da3be0719da45b9a",
"_journal/2024-11-05.md": "6a599e6bc9dcd12a0940956285ae4d00",
"_journal/2024-11-06.md": "c91de8a099cfee2514e1054400996e76",
"_journal/2024-11/2024-11-05.md": "79a1304037e18fefa135d576db040784",
@ -951,8 +951,8 @@
"_journal/2024-11/2024-11-21.md": "951b6034d60a40dbd8201c50abf0dbb9",
"_journal/2024-11/2024-11-20.md": "951b6034d60a40dbd8201c50abf0dbb9",
"_journal/2024-11/2024-11-19.md": "d879f57154cb27cb168eb1f1f430e312",
"set/cardinality.md": "c334d969afe9248c691cb6d3623c958a",
"geometry/area.md": "552fda3efca5db84b27f551884d93e51",
"set/cardinality.md": "ee5c8a4c6db54a18710609f312e582f2",
"geometry/area.md": "dc362cb5be314b2b9e4ecc53f2289af0",
"_journal/2024-11-23.md": "911f82ab8aede5ecdb96493aef64b0b9",
"_journal/2024-11/2024-11-22.md": "51117030e2364dbce3a8d507dead86ae",
"_journal/2024-11-24.md": "225661114ad1b605132873e83acdf777",
@ -961,7 +961,17 @@
"_journal/2024-11/2024-11-24.md": "894b021e6335d4e6947448c8d362c083",
"_journal/2024-11-26.md": "29bc0b54d23034b9108e567a1d5fa8ac",
"_journal/2024-11/2024-11-25.md": "1ec17a8473fa9c4779090ecbd22d70ef",
"calculus/intervals.md": "3c2f6803bf7f89a626b76c464bd715e1"
"calculus/intervals.md": "3c2f6803bf7f89a626b76c464bd715e1",
"c17/storage.md": "2aeaf297a91322c6ee0423f051ed4a7f",
"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",
"_journal/2024-11/2024-11-29.md": "4896d849b6345010669047323521e21a",
"_journal/2024-11/2024-11-28.md": "e9d252bc4bc3d6f70db56c36e1090e86",
"_journal/2024-11/2024-11-27.md": "7f44f4c91fea0c5c05d542aeeb9e723d"
},
"fields_dict": {
"Basic": [

View File

@ -0,0 +1,11 @@
---
title: "2024-11-30"
---
- [x] Anki Flashcards
- [ ] KoL
- [x] OGS
- [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story)
* Preliminary notes on [[linkage]].

View File

@ -2,7 +2,7 @@
title: "2024-11-27"
---
- [ ] Anki Flashcards
- [x] Anki Flashcards
- [x] KoL
- [x] OGS
- [ ] Sheet Music (10 min.)

View File

@ -0,0 +1,11 @@
---
title: "2024-11-28"
---
- [x] Anki Flashcards
- [x] KoL
- [x] OGS
- [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story)
* Notes on [[strings#Copying Functions|memcpy and memmove]].

View File

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

View File

@ -500,7 +500,7 @@ END%%
For any sets $A$, $B$, and $C$, $$\begin{align*} A \times (B \cap C) & = (A \times B) \cap (A \times C) \\ A \times (B \cup C) & = (A \times B) \cup (A \times C) \\ A \times (B - C) & = (A \times B) - (A \times C) \end{align*}$$
%%ANKI
Basic
Which of the algebra of sets operators does the Cartesian product distributive over?
Which of the algebra of sets operators does the Cartesian product distribute over?
Back: $\cap$, $\cup$, and $-$
Reference: “Cartesian Product,” in _Wikipedia_, April 17, 2024, [https://en.wikipedia.org/w/index.php?title=Cartesian_product&oldid=1219343305](https://en.wikipedia.org/w/index.php?title=Cartesian_product&oldid=1219343305).
<!--ID: 1718069881718-->

View File

@ -42,7 +42,7 @@ END%%
%%ANKI
Basic
What addresses can a `int` be stored at?
What addresses can an `int` be stored at?
Back: Any address divisible by 4.
Reference: Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. [http://www.catb.org/esr/structure-packing/](http://www.catb.org/esr/structure-packing/).
<!--ID: 1730831516934-->

View File

@ -469,7 +469,7 @@ END%%
%%ANKI
Basic
Which library defines `EXIT_SUCCESS`?
Back: `stdlib.h`
Back: `<stdlib.h>`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732293415810-->
END%%
@ -477,7 +477,7 @@ END%%
%%ANKI
Basic
Which library defines `EXIT_FAILURE`?
Back: `stdlib.h`
Back: `<stdlib.h>`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732293415813-->
END%%
@ -503,7 +503,7 @@ Which library declares the following prototype?
```c
noreturn void exit(int)
```
Back: `stdlib.h`
Back: `<stdlib.h>`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732293415823-->
END%%
@ -542,7 +542,7 @@ END%%
%%ANKI
Basic
Which library is `noreturn` defined in?
Back: `stdnoreturn.h`
Back: `<stdnoreturn.h>`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732293415846-->
END%%

340
notes/c17/linkage.md Normal file
View File

@ -0,0 +1,340 @@
---
title: Linkage
TARGET DECK: Obsidian::STEM
FILE TAGS: c17::linkage
tags:
- c17
- linkage
---
## Overview
An identifier declared in different scopes or in the same scope multiple times can be made to refer to the same object or function by a process called **linkage**. There are three kinds of linkage: **external**, **internal**, and **none**.
%%ANKI
Basic
Linkage is a property of what C construct?
Back: Identifiers.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1733010193273-->
END%%
%%ANKI
Basic
What capability does linkage allow identifiers to perform?
Back: Reference to the same object or function.
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: 1733010193275-->
END%%
%%ANKI
Basic
What are the three kinds of linkage?
Back: External, internal, and 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: 1733010193276-->
END%%
%%ANKI
Basic
External linkage denotes the same object across what?
Back: The entire program.
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: 1733010193277-->
END%%
%%ANKI
Basic
Internal linkage denotes the same object across what?
Back: A single translation unit.
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: 1733010193278-->
END%%
%%ANKI
Basic
What is the linkage between two different identifiers?
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: 1733010193279-->
END%%
%%ANKI
Basic
Which keyword is usually associated with external linkage?
Back: `extern`
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: 1733010193280-->
END%%
%%ANKI
Basic
What linkage does a keyword annotated with `extern` have?
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: 1733010193281-->
END%%
%%ANKI
Basic
What linkage does a keyword associated with `static` have?
Back: Internal.
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: 1733010193282-->
END%%
%%ANKI
Basic
Which keyword is usually associated with internal linkage?
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: 1733010193283-->
END%%
%%ANKI
Basic
When can an identifier have both external and internal linkage?
Back: When specified in two different translation units.
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: 1733010193284-->
END%%
%%ANKI
Basic
Within a translation unit, what happens if an identifer has both external and internal linkage?
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: 1733010193285-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of `x`?
```c
int x;
```
Back: 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: 1733010193286-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of `foo`?
```c
void foo(void) { }
```
Back: 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: 1733010193287-->
END%%
%%ANKI
Basic
What is the linkage of an object identifier with file scope and no storage-class specifier?
Back: 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: 1733010193288-->
END%%
%%ANKI
Basic
*How* do you identify the linkage of an object identifier with file scope and no storage-class specifier?
Back: Its always 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: 1733010193289-->
END%%
%%ANKI
Basic
What is the linkage of a function identifier with file scope and no storage-class specifier?
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: 1733010193290-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of `foo`?
```c
static void foo(void);
extern void foo(void);
```
Back: Internal.
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: 1733010193291-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of `foo`?
```c
extern void foo(void);
static void foo(void);
```
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: 1733010193292-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of `foo`?
```c
void foo(void);
static void foo(void);
```
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: 1733010193293-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of `foo`?
```c
void foo(void);
void foo(void);
```
Back: 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: 1733010193294-->
END%%
%%ANKI
Basic
*How* do you identify the linkage of a function identifier with file scope and no storage-class specifier?
Back: Its the same as if the function identifier were declared `extern`.
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: 1733010193295-->
END%%
%%ANKI
Basic
What is the linkage of a function parameter?
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: 1733010193296-->
END%%
%%ANKI
Basic
What is the linkage of a local variable without a storage specified?
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: 1733010193297-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
int x;
int main(void) { extern int x; }
```
Back: 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: 1733010193298-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
int x;
int main(void) { static int x; }
```
Back: Internal.
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: 1733010193299-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
extern int x;
int main(void) { static int x; }
```
Back: Internal.
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: 1733010193300-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
extern int x;
int main(void) { extern int x; }
```
Back: 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: 1733010193301-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
static int x;
extern int x;
int main(void) { extern int x; }
```
Back: Internal.
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: 1733010193302-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
extern int x;
static int x;
int main(void) { extern 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: 1733010193303-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
extern int x;
int main(void) { int x; }
```
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: 1733010193304-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
static int x;
int main(void) { int x; }
```
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: 1733010193305-->
END%%
%%ANKI
Basic
Consider the following translation unit. What is the linkage of the last `x`?
```c
void foo(int x) { }
```
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: 1733010193306-->
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

@ -319,7 +319,7 @@ END%%
%%ANKI
Basic
Trap representations are most relevant to what kind of derivied type?
Trap representations are most relevant to what kind of derived type?
Back: Pointers.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732397726948-->

232
notes/c17/storage.md Normal file
View File

@ -0,0 +1,232 @@
---
title: Storage
TARGET DECK: Obsidian::STEM
FILE TAGS: c17::storage
tags:
- c17
---
## 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.
```c
void* malloc(size_t size);
void free(void*);
```
%%ANKI
Basic
Which standard library header exposes `malloc`?
Back: `<stdlib.h>`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874715-->
END%%
%%ANKI
Basic
Which standard library header exposes `free`?
Back: `<stdlib.h>`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874716-->
END%%
%%ANKI
Basic
Why is `malloc` named the way it is?
Back: It stands for **m**emory **alloc**ate.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874717-->
END%%
%%ANKI
Basic
What two functions are most prominently used for dynamic allocation?
Back: `malloc` and `free`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874718-->
END%%
%%ANKI
Cloze
Generally speaking, memory created with {`malloc`} should be deleted with {`free`}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874719-->
END%%
%%ANKI
Basic
What three specialized variants of `malloc` are exposed by `<stdlib.h>`?
Back: `calloc`, `realloc`, and `aligned_alloc`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874720-->
END%%
%%ANKI
Basic
On success, what does a call to `malloc` return?
Back: A `void*` pointer.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874721-->
END%%
%%ANKI
Basic
On failure, what does a call to `malloc` return?
Back: A null pointer value.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874722-->
END%%
%%ANKI
Basic
What are the argument(s) to `malloc`?
Back: The size (in bytes) of the block of memory to allocate.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874723-->
END%%
%%ANKI
Basic
What is wrong with the following code snippet?
```c
double *d = (double*)malloc(sizeof *d);
```
Back: The return type of `malloc` should not be cast.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874724-->
END%%
%%ANKI
Basic
What is wrong with the following code snippet?
```c
double *d = malloc(sizeof *d);
```
Back: N/A. This is valid.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874725-->
END%%
%%ANKI
Basic
What possible error can occur by explicitly casting the return type of `malloc`?
Back: If `<stdlib.h>` isn't imported, older C compilers assume `malloc` returns an `int`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874726-->
END%%
%%ANKI
Basic
Without `<stdlib.h>`, an older C compiler might assume what `malloc` declaration?
Back:
```c
int malloc();
```
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874727-->
END%%
%%ANKI
Basic
Within `<stdlib.h>`, what function prototype does `malloc` have?
Back:
```c
void* malloc(size_t size);
```
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874728-->
END%%
%%ANKI
Cloze
Storage allocated through `malloc` is {uninitialized} and has {no type}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874729-->
END%%
%%ANKI
Basic
Does the following invoke undefined behavior? Why or why not?
```c
free(0);
```
Back: No. `free` ignores null pointer values.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874730-->
END%%
%%ANKI
Basic
Does the following invoke undefined behavior? Why or why not?
```c
double d = 0.0;
free(&d);
```
Back: Yes. `d` was not dynamically allocated.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874731-->
END%%
%%ANKI
Basic
Does the following invoke undefined behavior? Why or why not?
```c
double* d = malloc(sizeof *d);
free(&d);
```
Back: Yes. `d` was not dynamically allocated.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874732-->
END%%
%%ANKI
Basic
Does the following invoke undefined behavior? Why or why not?
```c
double* d = malloc(sizeof *d);
free(d);
```
Back: No. The address `d` pointed to was dynamically allocated.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874733-->
END%%
%%ANKI
Basic
Does the following invoke undefined behavior? Why or why not?
```c
double* d = malloc(sizeof *d);
free(d);
d = 0;
free(d);
```
Back: No. `free` ignores null pointer values.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874734-->
END%%
%%ANKI
Basic
Does the following invoke undefined behavior? Why or why not?
```c
double* d = malloc(sizeof *d);
free(d);
free(d);
```
Back: Yes. `free` should not be invoked on already `free`'d data.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874735-->
END%%
%%ANKI
Basic
`malloc` and its variants all have the `size` parameter in what position?
Back: The last position.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732755874736-->
END%%
## Bibliography
* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).

View File

@ -1146,11 +1146,179 @@ Tags: encoding::unicode
<!--ID: 1706976705750-->
END%%
## Copying Functions
The two primary functions used for copying memory are `memcpy` and `memmove`:
```c
void* memcpy(void* restrict s1, const void* restrict s2, size_t n);
void* memmove(void* s1, const void* s2, size_t n);
```
%%ANKI
Basic
What are the two primary functions provided by the standard library for copying memory?
Back: `memcpy` and `memmove`.
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: 1732843837986-->
END%%
%%ANKI
Basic
What is the function prototype of `memcpy`?
Back:
```c
void* memcpy(void* restrict s1, const void* restrict s2, size_t n);
```
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: 1732843837987-->
END%%
%%ANKI
Basic
What does the `memcpy` function do?
Back: Copies a specified number of bytes from one object to another.
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: 1732843837989-->
END%%
%%ANKI
Basic
Disregarding out of bounds errors, when does `memcpy` invoke undefined behavior?
Back: When copying occurs between objects that overlap in memory.
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: 1732843837990-->
END%%
%%ANKI
Basic
What does the first argument of `memcpy` refer to?
Back: The destination address bytes are copied to.
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: 1732843837991-->
END%%
%%ANKI
Basic
What does the second argument of `memcpy` refer to?
Back: The source address bytes are copied from.
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: 1732843837993-->
END%%
%%ANKI
Basic
What does the third argument of `memcpy` refer to?
Back: The number of bytes to copy from the source address.
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: 1732843837994-->
END%%
%%ANKI
Basic
Which of `memcpy` or `memmove` is faster?
Back: `memcpy`
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: 1732843837995-->
END%%
%%ANKI
Basic
*Why* is `memmove` slower than `memcpy`?
Back: It's implemented as if copying first into a temporary array that does not overlap the arguments in memory.
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: 1732843837996-->
END%%
%%ANKI
Basic
When should you prefer using `memcpy` instead of `memmove`?
Back: When the copying occurs between objects that do not overlap in memory.
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: 1732843837997-->
END%%
%%ANKI
Basic
Which standard header library defines `memcpy`?
Back: `<string.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: 1732843837998-->
END%%
%%ANKI
Basic
What is the function prototype of `memmove`?
Back:
```c
void* memmove(void* s1, const void* s2, size_t n);
```
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: 1732843837999-->
END%%
%%ANKI
Basic
What does the first argument of `memmove` refer to?
Back: The destination address bytes are copied to.
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: 1732843838000-->
END%%
%%ANKI
Basic
What does the second argument of `memmove` refer to?
Back: The source address bytes are copied from.
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: 1732843838001-->
END%%
%%ANKI
Basic
What does the third argument of `memmove` refer to?
Back: The number of bytes to copy from the source address.
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: 1732843838002-->
END%%
%%ANKI
Basic
Disregarding out of bounds errors, when does `memmove` invoke undefined behavior?
Back: N/A.
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: 1732843838003-->
END%%
%%ANKI
Basic
When should you prefer using `memmove` instead of `memcopy`?
Back: When the copying occurs between objects that overlap in memory.
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: 1732843838004-->
END%%
%%ANKI
Basic
Which standard header library defines `memmove`?
Back: `<string.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: 1732843838005-->
END%%
%%ANKI
Basic
What does the `memmove` function do?
Back: Copies a specified number of bytes from one object to another.
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: 1732843838006-->
END%%
## Bibliography
* Arnold D. Robbins, “GAWK: Effective AWK Programming,” October 2023, [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf).
* Brian W. Kernighan and Dennis M. Ritchie, *The C Programming Language*, 2nd ed (Englewood Cliffs, N.J: Prentice Hall, 1988).
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
* “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).
* Mendel Cooper, “Advanced Bash-Scripting Guide,” n.d., 916.
* “Printf,” in *Wikipedia*, January 18, 2024, [https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962](https://en.wikipedia.org/w/index.php?title=Printf&oldid=1196716962).

View File

@ -309,7 +309,7 @@ Let $Q$ be a set. If there exists exactly one $c$ such that $a(S) \leq c \leq a(
Cloze
Let $Q$ be a set. The {exhaustion} property of area states:
if there exists {exactly one} $c \in \mathbb{R}$ such that {$a(S) \leq c \leq a(T)$} for all {step regions} $S$ and $T$ satisfying {$S \subseteq Q \subseteq T$}, then {$Q \in \mathscr{M}$} and {$a(Q) = c$}.
If there exists {exactly one} $c \in \mathbb{R}$ such that {$a(S) \leq c \leq a(T)$} for all {step regions} $S$ and $T$ satisfying {$S \subseteq Q \subseteq T$}, then {$Q \in \mathscr{M}$} and {$a(Q) = c$}.
Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980).
<!--ID: 1732381333427-->
END%%

View File

@ -139,7 +139,7 @@ END%%
%%ANKI
Basic
Is the following identity true? $$\lambda x y. x(xy) \equiv \lambda u v. u(uv))$$
Is the following identity true? $$\lambda x y. x(xy) \equiv \lambda u v. u(uv)$$
Back: No.
Reference: Hindley, J Roger, and Jonathan P Seldin. “Lambda-Calculus and Combinators, an Introduction,” n.d. [https://www.cin.ufpe.br/~djo/files/Lambda-Calculus%20and%20Combinators.pdf](https://www.cin.ufpe.br/~djo/files/Lambda-Calculus%20and%20Combinators.pdf).
<!--ID: 1717687744169-->