Alignment and typedefs.
parent
2d3f5e0879
commit
7a337fd4ba
|
@ -334,7 +334,7 @@
|
|||
"c17/strings.md": "e909daa71ad93bcb38edbe30bc0516ac",
|
||||
"c17/index.md": "611dd21439d56575172ef004c7635382",
|
||||
"c17/escape-sequences.md": "a8b99070336878b4e8c11e9e4525a500",
|
||||
"c17/declarations.md": "2d7877915bf8a2772d1e4de636ba52a5",
|
||||
"c17/declarations.md": "c520b5335cbcad3cf54590bf100fafbc",
|
||||
"algorithms/sorting/merge-sort.md": "6506483f7df6507cee0407bd205dbedd",
|
||||
"_journal/2024-02-24.md": "9bb319d5014caf962a9ce3141076cff4",
|
||||
"_journal/2024-02/2024-02-23.md": "0aad297148e8cc4058b48b7e45787ca7",
|
||||
|
@ -400,7 +400,7 @@
|
|||
"_journal/2024-03/2024-03-17.md": "23f9672f5c93a6de52099b1b86834e8b",
|
||||
"set/directed-graph.md": "b4b8ad1be634a0a808af125fe8577a53",
|
||||
"set/index.md": "29f5f295765101fe11d821faa908b027",
|
||||
"set/graphs.md": "a56f867f51e69cb7438bbdf6215fca36",
|
||||
"set/graphs.md": "de5817ecac06f006c7f985cc1b45f67a",
|
||||
"_journal/2024-03-19.md": "a0807691819725bf44c0262405e97cbb",
|
||||
"_journal/2024-03/2024-03-18.md": "2c711c50247a9880f7ed0d33b16e1101",
|
||||
"awk/variables.md": "e40a20545358228319f789243d8b9f77",
|
||||
|
@ -596,7 +596,7 @@
|
|||
"x86-64/instructions/logical.md": "49d40018f1fcb4ed1595d9175bbaab57",
|
||||
"x86-64/instructions/arithmetic.md": "df57fdc8d260d0812464b08565ef4c51",
|
||||
"x86-64/instructions/access.md": "4589d79f9915ffeeab7b75d2208b7917",
|
||||
"x86-64/instructions/index.md": "72c19067e938ab39ea51d25d6ac2bad9",
|
||||
"x86-64/instructions/index.md": "df78f6d1d56da054606d374ca3282bf8",
|
||||
"_journal/2024-06-09.md": "935b3ddf65c51e680ac5c000c7e380af",
|
||||
"_journal/2024-06/2024-06-08.md": "9e1ebc8882a395b96ca765ad5c982d68",
|
||||
"_journal/2024-06-10.md": "84d27300b97c8544ab4ec68b06edd824",
|
||||
|
@ -798,7 +798,7 @@
|
|||
"c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875",
|
||||
"c17/types/simple.md": "b53fd365671848f09e6a0cd46e17c1bf",
|
||||
"c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d",
|
||||
"c17/types/derived.md": "cd77112313bd0f9cf99b0f224a364179",
|
||||
"c17/types/derived.md": "89b86ecc2440bcb1aff13e1504efd1ba",
|
||||
"c17/types/basic.md": "5064e21e683c0218890058882e06b6f3",
|
||||
"c17/types/index.md": "14b651bcfc8b2d62ffd200a74a9a2a6b",
|
||||
"_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2",
|
||||
|
@ -879,7 +879,7 @@
|
|||
"_journal/2024-10/2024-10-06.md": "65a8e538144f59ee9e7296110cc9aa14",
|
||||
"_journal/2024-10-09.md": "66acb6c8abbf0c860966223f3299c2bc",
|
||||
"_journal/2024-10/2024-10-08.md": "2f737d8198b12bf635808964c4887ae1",
|
||||
"x86-64/procedures.md": "f3241c5d499467d9332d2bb950a97c3d",
|
||||
"x86-64/procedures.md": "3464bb6229f85973961c6693f47d6f20",
|
||||
"_journal/2024-10-10.md": "29e4be2c164c63f8b7aab56bddee3dd6",
|
||||
"_journal/2024-10/2024-10-09.md": "b718c6f4cfe3069fcc0faf07ec414f73",
|
||||
"_journal/2024-10-15.md": "6e696a8d16ba257ed89e8564b771d290",
|
||||
|
@ -908,7 +908,17 @@
|
|||
"_journal/2024-10/2024-10-25.md": "0597575213a2bb0e15052efa9404dd1b",
|
||||
"_journal/2024-10/2024-10-24.md": "4493c339209fdd910866d126b8ebe3c6",
|
||||
"_journal/2024-10-29.md": "19444fb3b9bef82b200c77a6acc33637",
|
||||
"_journal/2024-10/2024-10-28.md": "95bcec30eda031bf6410ff51a61b0ee3"
|
||||
"_journal/2024-10/2024-10-28.md": "95bcec30eda031bf6410ff51a61b0ee3",
|
||||
"c17/typedefs.md": "1fcb044f12c4f16de1ef8cb4c293dd10",
|
||||
"_journal/2024-11-04.md": "69ac4ebe34ab5609a444bc9c46b55b84",
|
||||
"_journal/2024-11/2024-11-03.md": "8c5e70f566953974f252da9472e527f0",
|
||||
"_journal/2024-11/2024-11-02.md": "8c5e70f566953974f252da9472e527f0",
|
||||
"_journal/2024-11/2024-11-01.md": "8c5e70f566953974f252da9472e527f0",
|
||||
"_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": "49ef4c266dbd9300211288ffa2152c7e",
|
||||
"_journal/2024-11-05.md": "6a599e6bc9dcd12a0940956285ae4d00"
|
||||
},
|
||||
"fields_dict": {
|
||||
"Basic": [
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
title: "2024-11-03"
|
||||
---
|
||||
|
||||
- [x] Anki Flashcards
|
||||
- [x] KoL
|
||||
- [ ] OGS
|
||||
- [ ] Sheet Music (10 min.)
|
||||
- [ ] Korean (Read 1 Story)
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
title: "2024-11-05"
|
||||
---
|
||||
|
||||
- [ ] Anki Flashcards
|
||||
- [ ] KoL
|
||||
- [ ] OGS
|
||||
- [ ] Sheet Music (10 min.)
|
||||
- [ ] Korean (Read 1 Story)
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
title: "2024-11-03"
|
||||
---
|
||||
|
||||
- [x] Anki Flashcards
|
||||
- [x] KoL
|
||||
- [ ] OGS
|
||||
- [ ] Sheet Music (10 min.)
|
||||
- [ ] Korean (Read 1 Story)
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
title: "2024-11-03"
|
||||
---
|
||||
|
||||
- [x] Anki Flashcards
|
||||
- [x] KoL
|
||||
- [ ] OGS
|
||||
- [ ] Sheet Music (10 min.)
|
||||
- [ ] Korean (Read 1 Story)
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
title: "2024-11-03"
|
||||
---
|
||||
|
||||
- [x] Anki Flashcards
|
||||
- [x] KoL
|
||||
- [ ] OGS
|
||||
- [ ] Sheet Music (10 min.)
|
||||
- [ ] Korean (Read 1 Story)
|
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
title: "2024-11-04"
|
||||
---
|
||||
|
||||
- [x] Anki Flashcards
|
||||
- [x] KoL
|
||||
- [ ] OGS
|
||||
- [ ] Sheet Music (10 min.)
|
||||
- [ ] Korean (Read 1 Story)
|
||||
|
||||
* Additional notes on pointers and arrays.
|
||||
* First pass on "The Lost Art of Structure Packing".
|
|
@ -0,0 +1,102 @@
|
|||
---
|
||||
title: Alignment
|
||||
TARGET DECK: Obsidian::STEM
|
||||
FILE TAGS: c17 x86-64
|
||||
tags:
|
||||
- c17
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
For a large class of modern ISAs, storage for basic C datatypes respect **self-alignmnet**. This means `char`s can start on any byte address, `short`s on any even address, 4-byte `int`s and `float`s must start on an address divisible by 4, and `double`s must start on an address divisible by 8. Likewise pointers are also self-aligned.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What does self-alignment refer to?
|
||||
Back: The placement of C datatypes on an address divisible by the size of the datatype.
|
||||
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: 1730831516929-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What addresses can a `char` be stored at?
|
||||
Back: Any address.
|
||||
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: 1730831516932-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What addresses can a `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-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What addresses can a `short` be stored at?
|
||||
Back: Any address divisible by 2.
|
||||
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: 1730831516936-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What addresses can a `double` be stored at?
|
||||
Back: Any address divisible by 8.
|
||||
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: 1730831516938-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What addresses can a pointer be stored at?
|
||||
Back: Any address divisible by 8.
|
||||
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: 1730831516941-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How does self-alignment make access faster?
|
||||
Back: It enables single instruction fetches and puts.
|
||||
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: 1730831516943-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
With respect to memory alignment, {slop} is {waste space for padding datatypes to their alignment}.
|
||||
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: 1730831516944-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What value is slop initialized to?
|
||||
Back: Undefined.
|
||||
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: 1730831516946-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
*Why* isn't equality for `struct`s well-defined?
|
||||
Back: The value of slop is undefined.
|
||||
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: 1730831516947-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
*Why* isn't inequality for `struct`s well-defined?
|
||||
Back: The value of slop is undefined.
|
||||
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: 1730831516948-->
|
||||
END%%
|
||||
|
||||
## Bibliography
|
||||
|
||||
* 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/).
|
|
@ -132,54 +132,6 @@ Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Progra
|
|||
<!--ID: 1722786892108-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type of `x` in the following?
|
||||
```c
|
||||
#define int_ptr int *
|
||||
int_ptr x, y;
|
||||
```
|
||||
Back: `int *`
|
||||
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
||||
<!--ID: 1722786892109-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type of `y` in the following?
|
||||
```c
|
||||
#define int_ptr int *
|
||||
int_ptr x, y;
|
||||
```
|
||||
Back: `int`
|
||||
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
||||
<!--ID: 1722786892110-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type of `x` in the following?
|
||||
```c
|
||||
typedef int_ptr int *
|
||||
int_ptr x, y;
|
||||
```
|
||||
Back: `int *`
|
||||
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
||||
<!--ID: 1722786892111-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type of `y` in the following?
|
||||
```c
|
||||
typedef int_ptr int *
|
||||
int_ptr x, y;
|
||||
```
|
||||
Back: `int *`
|
||||
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
||||
<!--ID: 1722786892112-->
|
||||
END%%
|
||||
|
||||
Declarations can be read by complying with the precedence rules outlined below:
|
||||
|
||||
1. Find the name of the declaration.
|
||||
|
|
|
@ -0,0 +1,126 @@
|
|||
---
|
||||
title: Typedefs
|
||||
TARGET DECK: Obsidian::STEM
|
||||
FILE TAGS: c17::type
|
||||
tags:
|
||||
- c17
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
The `<stddef.h>` header defines a few standard `typedef`s:
|
||||
|
||||
* `ptrdiff_t`: the signed integer type of the result of subtracting two pointers.
|
||||
* `size_t`: the unsigned integer type of the result of the `sizeof` operator.
|
||||
|
||||
The standard often uses `typedef`s ending with `_t`.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type of `x` in the following?
|
||||
```c
|
||||
#define int_ptr int *
|
||||
int_ptr x, y;
|
||||
```
|
||||
Back: `int *`
|
||||
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
||||
<!--ID: 1722786892109-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type of `y` in the following?
|
||||
```c
|
||||
#define int_ptr int *
|
||||
int_ptr x, y;
|
||||
```
|
||||
Back: `int`
|
||||
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
||||
<!--ID: 1722786892110-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type of `x` in the following?
|
||||
```c
|
||||
typedef int_ptr int *
|
||||
int_ptr x, y;
|
||||
```
|
||||
Back: `int *`
|
||||
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
||||
<!--ID: 1722786892111-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the type of `y` in the following?
|
||||
```c
|
||||
typedef int_ptr int *
|
||||
int_ptr x, y;
|
||||
```
|
||||
Back: `int *`
|
||||
Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
||||
<!--ID: 1722786892112-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What header defines `size_t`?
|
||||
Back: `<stddef.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: 1730740461658-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What header defines `ptrdiff_t`?
|
||||
Back: `<stddef.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: 1730740461659-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
`ptrdiff_t` is used as the type of what result?
|
||||
Back: Subtracting two pointers.
|
||||
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: 1730740461660-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
`size_t` is used as the type of what result?
|
||||
Back: The `sizeof` operation.
|
||||
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: 1730740461661-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is `ptrdiff_t` signed or unsigned?
|
||||
Back: Signed.
|
||||
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: 1730740461662-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Is `size_t` signed or unsigned?
|
||||
Back: Unsigned.
|
||||
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: 1730740461663-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
The C standard typically suffixes `typedef`s with what?
|
||||
Back: `_t`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755507-->
|
||||
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).
|
||||
* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
|
|
@ -508,6 +508,53 @@ Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70
|
|||
<!--ID: 1728244147664-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Add initializer with first element set to `1` using designated initialization.
|
||||
```c
|
||||
int example[3];
|
||||
```
|
||||
Back:
|
||||
```c
|
||||
int example[3] = { [0] = 1 };
|
||||
```
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730757470057-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Add initializer with first element set to `1` *without* using designated initialization.
|
||||
```c
|
||||
int example[3];
|
||||
```
|
||||
Back:
|
||||
```c
|
||||
int example[3] = { 1 };
|
||||
```
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730757470061-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the value of `example[1]` in the following?
|
||||
```c
|
||||
int example[2] = { [0] = 1 };
|
||||
```
|
||||
Back: `0`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730757470063-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Are arrays passed by reference or value?
|
||||
Back: Reference.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755493-->
|
||||
END%%
|
||||
|
||||
### Structures
|
||||
|
||||
A `struct` is a grouping of data together. It has the following general form:
|
||||
|
@ -560,6 +607,125 @@ Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Progra
|
|||
<!--ID: 1722786892128-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Define an object with `fieldA` set to `1` using designated initialization.
|
||||
```c
|
||||
struct example { int fieldA; };
|
||||
```
|
||||
Back:
|
||||
```c
|
||||
struct example test = { .fieldA = 1 };
|
||||
```
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730757470065-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Define an object with `fieldA` set to `1` *without* using designated initialization.
|
||||
```c
|
||||
struct example { int fieldA; };
|
||||
```
|
||||
Back:
|
||||
```c
|
||||
struct example test = { 1 };
|
||||
```
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730757470066-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the value of `test.fieldB` in the following?
|
||||
```c
|
||||
struct example { int fieldA; int fieldB; };
|
||||
struct example test = { .fieldA = 1 };
|
||||
```
|
||||
Back: `0`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730757470068-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Are `struct`s well-defined with respect to `=` (i.e. assignment)?
|
||||
Back: Yes.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755496-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Are `struct`s well-defined with respect to `==` (i.e. equality)?
|
||||
Back: No.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755498-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Are `struct`s well-defined with respect to `!=` (i.e. inequality)?
|
||||
Back: No.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755499-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Generally speaking, what are valid member types of a `struct`?
|
||||
Back: All object types except VLAs.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755500-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Generally speaking, what object type is not a valid `struct` member type?
|
||||
Back: VLAs.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755501-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Declare a variable `var` of the nested `struct` with member `c` set to `1`.
|
||||
```c
|
||||
struct A {
|
||||
struct B { int c; };
|
||||
};
|
||||
```
|
||||
Back: `struct B var = { 1 };`
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755502-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the visibility of `struct B` with respect to `struct A`?
|
||||
```c
|
||||
struct A {
|
||||
struct B { int c; };
|
||||
};
|
||||
```
|
||||
Back: They have the same visibility.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755503-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What does it mean for `struct A` and `struct B` to have the same visibility?
|
||||
```c
|
||||
struct A {
|
||||
struct B { int c; };
|
||||
};
|
||||
```
|
||||
Back: Nesting `struct`s does not introduce any notion of scope.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755504-->
|
||||
END%%
|
||||
|
||||
## Unions
|
||||
|
||||
A `union` is a grouping of data together but with overlaid storage. It has the following general form:
|
||||
|
@ -607,7 +773,7 @@ END%%
|
|||
|
||||
## Pointers
|
||||
|
||||
Pointers have the same size as the machine's word size since it should be able to refer to any virtual address.
|
||||
Pointers have the same size as the machine's word size since it should be able to refer to any virtual address. All pointers are either **valid**, **null**, or **indeterminate**.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
|
@ -617,6 +783,257 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
|||
<!--ID: 1707493017246-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is a pointer represented in binary?
|
||||
Back: N/A. This is implementation specific.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730755551586-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What three states can a pointer be in?
|
||||
Back: Valid, null, or indeterminate.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730755551590-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is a pointer made null?
|
||||
Back: By initializing or assigning the pointer to `0`.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730755551592-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
When does a pointer evaluate to `false`?
|
||||
Back: When it is a null pointer.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730755551594-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
When does a pointer evaluate to `true`?
|
||||
Back: When it is not a null pointer.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730755551596-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Suppose a pointer logically evaluates to `true`. Why can't we use it?
|
||||
Back: Logical evaluation can't distinguish valid pointers from indeterminate pointers.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730755551598-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the result of dereferencing an indeterminate pointer?
|
||||
Back: Undefined behavior.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730755551599-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
What is the result of dereferencing a null pointer?
|
||||
Back: Undefined behavior.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730755551601-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is `r->field` equivalently written using `*`?
|
||||
Back: `(*r).field`
|
||||
Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730757470069-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is `(*r).field` more compactly written?
|
||||
Back: `r->field`
|
||||
Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730757492874-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How many members *must* be defined in a `struct` initializer?
|
||||
Back: One.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755505-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Are `struct`s passed by reference or value?
|
||||
Back: Value.
|
||||
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
|
||||
<!--ID: 1730758755506-->
|
||||
END%%
|
||||
|
||||
C allows arithmetic on pointers, where the computed value is scaled according to the size of the data type referenced by the pointer.
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is the following (assumed valid) expression simplified?
|
||||
```c
|
||||
*&E
|
||||
```
|
||||
Back: As just `E`.
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730740461664-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is the following (assumed valid) expression simplified?
|
||||
```c
|
||||
&*E
|
||||
```
|
||||
Back: As just `E`.
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730740461665-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Cloze
|
||||
Pointer arithmetic {`*(A + i)`} is equivalent to array reference `A[i]`.
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730740461666-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Given the following, how can we equivalent write `A[1][2]` using pointer arithmetic?
|
||||
```c
|
||||
int main() {
|
||||
int A[3][4];
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
Back: `A + 6`
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730745799783-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Given the following, how can we equivalent write `A[2][0]` using pointer arithmetic?
|
||||
```c
|
||||
int main() {
|
||||
int A[3][4];
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
Back: `A + 8`
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730745799787-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Given the following, how can we equivalent write `A[1][2]` using pointer arithmetic?
|
||||
```c
|
||||
int main() {
|
||||
int A[4][3];
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
Back: `A + 5`
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730745799789-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Given the following, how can we equivalent write `A[1][0]` using pointer arithmetic?
|
||||
```c
|
||||
int main() {
|
||||
int A[4][3];
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
Back: `A + 3`
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730745799791-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is the `return` statement likely translated to x86-64?
|
||||
```c
|
||||
int* example(int *E) {
|
||||
return E;
|
||||
}
|
||||
```
|
||||
Back:
|
||||
```asm
|
||||
movq %rdi,%rax
|
||||
```
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
Tags: x86-64
|
||||
<!--ID: 1730740461667-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is the `return` statement likely translated to x86-64?
|
||||
```c
|
||||
int example(int *E) {
|
||||
return E[0];
|
||||
}
|
||||
```
|
||||
Back:
|
||||
```asm
|
||||
movl (%rdi),%eax
|
||||
```
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
Tags: x86-64
|
||||
<!--ID: 1730740461668-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is the `return` statement likely translated to x86-64?
|
||||
```c
|
||||
int example(int *E, int i) {
|
||||
return E[i];
|
||||
}
|
||||
```
|
||||
Back:
|
||||
```asm
|
||||
movl (%rdi, %rsi, 4),%eax
|
||||
```
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
Tags: x86-64
|
||||
<!--ID: 1730740461669-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
How is the `return` statement likely translated to x86-64?
|
||||
```c
|
||||
int example(int *E) {
|
||||
return &E[2];
|
||||
}
|
||||
```
|
||||
Back:
|
||||
```asm
|
||||
leaq 8(%rdi),%rax
|
||||
```
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
Tags: x86-64
|
||||
<!--ID: 1730740461670-->
|
||||
END%%
|
||||
|
||||
## Bibliography
|
||||
|
||||
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
|
|
|
@ -282,6 +282,30 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
|
|||
<!--ID: 1713213168894-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Consider scaled index operand form $Imm(r_b, r_i, s)$. What values can $r_b$ take on?
|
||||
Back: Any 64-bit register.
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730740461653-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Consider scaled index operand form $Imm(r_b, r_i, s)$. What values can $r_i$ take on?
|
||||
Back: Any 64-bit register.
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730740461655-->
|
||||
END%%
|
||||
|
||||
%%ANKI
|
||||
Basic
|
||||
Consider scaled index operand form $Imm(r_b, r_i, s)$. What values can $s$ take on?
|
||||
Back: $1$, $2$, $4$, or $8$.
|
||||
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
||||
<!--ID: 1730740461656-->
|
||||
END%%
|
||||
|
||||
## Bibliography
|
||||
|
||||
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
|
Loading…
Reference in New Issue