diff --git a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json index a88ecc0..c91b51d 100644 --- a/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json +++ b/notes/.obsidian/plugins/obsidian-to-anki-plugin/data.json @@ -221,7 +221,8 @@ "arg-build-area.png", "stack-frame.png", "buffer-overflow.png", - "infinite-cartesian-product.png" + "infinite-cartesian-product.png", + "postage-function.png" ], "File Hashes": { "algorithms/index.md": "3ac071354e55242919cc574eb43de6f8", @@ -802,7 +803,7 @@ "c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d", "c17/types/derived.md": "74c608d72b25794bc3887f58f001bab2", "c17/types/basic.md": "5064e21e683c0218890058882e06b6f3", - "c17/types/index.md": "14b651bcfc8b2d62ffd200a74a9a2a6b", + "c17/types/index.md": "ae2ebd695b4eeadbabf28c814ce3a052", "_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2", "_journal/2024-08/2024-08-24.md": "563fad24740e44734a87d7c3ec46cec4", "algebra/abs-val.md": "a47bc08db62304eb526d15ede3e300cf", @@ -959,7 +960,8 @@ "_journal/2024-11-25.md": "1ec17a8473fa9c4779090ecbd22d70ef", "_journal/2024-11/2024-11-24.md": "894b021e6335d4e6947448c8d362c083", "_journal/2024-11-26.md": "29bc0b54d23034b9108e567a1d5fa8ac", - "_journal/2024-11/2024-11-25.md": "1ec17a8473fa9c4779090ecbd22d70ef" + "_journal/2024-11/2024-11-25.md": "1ec17a8473fa9c4779090ecbd22d70ef", + "calculus/intervals.md": "3c2f6803bf7f89a626b76c464bd715e1" }, "fields_dict": { "Basic": [ diff --git a/notes/_journal/2024-11-26.md b/notes/_journal/2024-11-27.md similarity index 62% rename from notes/_journal/2024-11-26.md rename to notes/_journal/2024-11-27.md index 4893ed0..90d406b 100644 --- a/notes/_journal/2024-11-26.md +++ b/notes/_journal/2024-11-27.md @@ -1,5 +1,5 @@ --- -title: "2024-11-26" +title: "2024-11-27" --- - [ ] Anki Flashcards diff --git a/notes/_journal/2024-11/2024-11-26.md b/notes/_journal/2024-11/2024-11-26.md new file mode 100644 index 0000000..338355c --- /dev/null +++ b/notes/_journal/2024-11/2024-11-26.md @@ -0,0 +1,11 @@ +--- +title: "2024-11-26" +--- + +- [x] Anki Flashcards +- [x] KoL +- [x] OGS +- [ ] Sheet Music (10 min.) +- [ ] Korean (Read 1 Story) + +* Notes on [[intervals]]. \ No newline at end of file diff --git a/notes/c17/alignment.md b/notes/c17/alignment.md index a3534a4..bcf670f 100644 --- a/notes/c17/alignment.md +++ b/notes/c17/alignment.md @@ -9,7 +9,7 @@ tags: ## 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. +For a large class of modern ISAs, storage for basic C datatypes respect **self-alignment**. 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. Wasted space introduced solely for alignment purposes is referred to as **slop**. diff --git a/notes/c17/declarations.md b/notes/c17/declarations.md index 527bac8..b44006e 100644 --- a/notes/c17/declarations.md +++ b/notes/c17/declarations.md @@ -275,497 +275,6 @@ Reference: Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Progra END%% -## Prototypes - -A function declaration/definition has two ways of using declarators: **parameter type lists** and **identifier type lists**. To make the distinction clear, consider the following two ways of defining an `add` function: - -```c -int f(int x, int y) { return x + y; } // Paramter type list -int f(x, y) int x; int y; { return x + y } // Identifier type list -``` - -A function **prototype** is a kind of function declaration that specifies the function signature. There are three important points to make note of: - -* Empty identifier lists are interpreted as "the compiler has not been told what this function's arguments are." -* The standard prohibits declaring functions with a non-empty identifier list. -* Empty parameter lists are not allowed. - -Therefore: - -```c -// Uses an empty identifer list. This declares a function `foo` -// that takes an unknown specification of arguments. -void foo(); -// Uses a non-empty identifier list. Compiler error. -void foo(x, y); -// Uses a non-empty identifier list. Compiler error. -void foo(x, y) int x; int y; -// Uses a non-empty identifier list. Definitions allow this. -void foo(x, y) int x; int y; { } -// Uses a non-empty parameter list. This prototypes a function -// `foo` that takes no arguments. -void foo(void); -// Uses a non-empty parameter list. This prototypes and defines -// a function `foo` that takes no arguments. -void foo(void) {} -``` - -Together these points imply a function prototype *must* use a parameter type list. - -%%ANKI -Basic -Which of prototypes or declarations are more general? -Back: Declarations. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -What two ways are parameters declared in function declarations and definitions? -Back: Identifier type lists and parameter type lists. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -Which of identifier type lists and/or parameter type lists considered obsolete? -Back: Identifier type lists. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -Define an addition function using identifier type lists. -Back: -```c -int add(x, y) int x; int y; { return x + y; } -``` -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -Define an addition function using parameter type lists. -Back: -```c -int add(int x, int y) { return x + y; } -``` -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -Is the following a prototype or a declaration? -```c -void foo(); -``` -Back: A declaration. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(); -``` -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). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(x, y); -``` -Back: A function declaration cannot have a non-empty identifier list. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(x, y) int x; int y; -``` -Back: A function declaration cannot have a non-empty identifier list. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(x, y) int x; int y; {} -``` -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). - -END%% - -%%ANKI -Basic -What function prototype is declared in the following? -```c -void foo(x, y) int x; int y; {} -``` -Back: N/A. No prototype has been declared. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(); - -int main(void) { foo(1); } -``` -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). - -END%% - -%%ANKI -Basic -Is the following a prototype or a declaration? -```c -void f(void); -``` -Back: A prototype. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(void); -``` -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). - -END%% - -%%ANKI -Basic -What compilation error does the following raise? -```c -void foo(void); - -int main(void) { foo(1); } -``` -Back: Too many arguments to function call `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). - -END%% - -%%ANKI -Basic -What function prototype is declared in the following? -```c -void foo(int x, int y); -``` -Back: `void foo(int, int)` -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -What function prototype is declared in the following? -```c -void foo(int x, int y) {} -``` -Back: `void foo(int, int)` -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -How many arguments does the following declaration specify? -```c -void foo(); -``` -Back: Some number unknown to the compiler. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -How many arguments does the following declaration specify? -```c -void foo(void); -``` -Back: Zero. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -Why might the following snippet raise a compilation error? -```c -int foo(); -int foo(int a); -``` -Back: N/A. It likely wouldn't. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -*Why* might the following snippet raise a compilation error? -```c -int foo(); -int foo(float a); -``` -Back: Conflicting types. The first `foo` declares any `float` passed to it default promotes to a `double`. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -*Why* might the following snippet raise a compilation error? -```c -int foo(); -int foo(char a); -``` -Back: Conflicting types. The first `foo` declares any `char` passed to it default promotes to an `int`. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -%%ANKI -Basic -*Why* might the following snippet raise a compilation error? -```c -int foo(); -int foo(double a); -``` -Back: N/A. It likely wouldn't. -Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). - -END%% - -### main - -`main` is a special function serving as the entrypoint to C programs. It can have several different prototypes, but the following two are always possible: - -```c -int main(void); -int main(int argc, char* argv[argc+1]); -``` - -The only two return values guaranteed to work on all platform is `EXIT_SUCCESS` and `EXIT_FAILURE`. Reaching the end of `main` is equivalent to a `reutrn` with value `EXIT_SUCCESS`. - -%%ANKI -Basic -Which function serves as the entrypoint of C programs? -Back: `main` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How many valid prototypes of `main` are available? -Back: Indeterminate. Depends on the system. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -How many "official" prototypes of `main` are available? -Back: Two. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What are the "official" prototypes of `main`? -Back: -```c -int main(void); -int main(int argc, char* argv[argc + 1]); -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What are the "official" prototypes of `main`? -Back: -```c -int main(void); -int main(int argc, char* argv[argc + 1]); -``` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). -END%% - -%%ANKI -Basic -What are the only portable values that `main` can return? -Back: `EXIT_SUCCESS` and `EXIT_FAILURE`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which library defines `EXIT_SUCCESS`? -Back: `stdlib.h` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which library defines `EXIT_FAILURE`? -Back: `stdlib.h` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What happens when `main` does not explicitly return a value? -Back: `EXIT_SUCCESS` is implicitly returned. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -Returning {1:`s`} in {1:`main`} is equivalent to invoking function {2:`exit`} with argument {2:`s`}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which library declares the following prototype? -```c -noreturn void exit(int) -``` -Back: `stdlib.h` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What does keyword `_Noreturn` indicate? -Back: The associated callee will never return control back to the caller. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What kind of syntactical construct is `_Noreturn`? -Back: A special keyword. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What kind of syntactical construct is `noreturn`? -Back: A macro. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -{1:`_Noreturn`} is a {2:keyword} whereas {2:`noreturn`} is a {1:macro}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which library is `noreturn` defined in? -Back: `stdnoreturn.h` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the value of `argv[0]`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: The name of the program invocation. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the value of `argv[argc]`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: `0` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the value of `argv[1]`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: `0` if `argc == 1` else the first argument to the program. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the minimum value of `argc`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: `1` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following prototype. What is the minimum length of `argv`? -```c -int main(int argc, char* argv[argc + 1]); -``` -Back: `2` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - ## Initializers An **initializer** is an expression that gives an object a value at time of declaration. Only variable-length arrays (VLAs) do not allow for an initializer. The default initializer looks like `{0}`. diff --git a/notes/c17/functions.md b/notes/c17/functions.md new file mode 100644 index 0000000..0d6a005 --- /dev/null +++ b/notes/c17/functions.md @@ -0,0 +1,608 @@ +--- +title: Functions +TARGET DECK: Obsidian::STEM +FILE TAGS: c17::function +tags: + - c17 +--- + +## Overview + +A function `f` without a following opening `(` is converted to a pointer to its start. This is called **function decay**. + +%%ANKI +Basic +What is the effect of function decay? +Back: Evaluation of a function `f` without a following opening `(` is converted to a pointer to its start. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What name is given to the implicit conversion of a function to a pointer? +Back: Function decay. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +According to Gustedt, what C feature explains why are there no "function values"? +Back: Function-to-pointer decay. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Why can't functions directly be made arguments to functions? +Back: Because function arguments decay to pointers. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +{1:Function pointers} are to {2:`(...)`} whereas {2:pointers} are to {1:`[...]`}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +f(3); +``` +Back: Decay, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +(&f)(3); +``` +Back: Address of, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +(*f)(3); +``` +Back: Decay, dereference, decay, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +(*&f)(3); +``` +Back: Address of, dereference, decay, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +In what order are decays, dereferences, address ofs, and calls performed in the following? +```c +(&*f)(3); +``` +Back: Decay, dereference, address of, call. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +{1:Pointers} refer to {2:arrays} whereas {2:function pointers} refer to {1:functions}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +## Prototypes + +A function declaration/definition has two ways of using declarators: **parameter type lists** and **identifier type lists**. To make the distinction clear, consider the following two ways of defining an `add` function: + +```c +int f(int x, int y) { return x + y; } // Paramter type list +int f(x, y) int x; int y; { return x + y } // Identifier type list +``` + +A function **prototype** is a kind of function [[c17/declarations|declaration]] that specifies the function signature. There are three important points to make note of: + +* Empty identifier lists are interpreted as "the compiler has not been told what this function's arguments are." +* The standard prohibits declaring functions with a non-empty identifier list. +* Empty parameter lists are not allowed. + +Therefore: + +```c +// Uses an empty identifer list. This declares a function `foo` +// that takes an unknown specification of arguments. +void foo(); +// Uses a non-empty identifier list. Compiler error. +void foo(x, y); +// Uses a non-empty identifier list. Compiler error. +void foo(x, y) int x; int y; +// Uses a non-empty identifier list. Definitions allow this. +void foo(x, y) int x; int y; { } +// Uses a non-empty parameter list. This prototypes a function +// `foo` that takes no arguments. +void foo(void); +// Uses a non-empty parameter list. This prototypes and defines +// a function `foo` that takes no arguments. +void foo(void) {} +``` + +Together these points imply a function prototype *must* use a parameter type list. + +%%ANKI +Basic +Which of prototypes or declarations are more general? +Back: Declarations. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +What two ways are parameters declared in function declarations and definitions? +Back: Identifier type lists and parameter type lists. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +Which of identifier type lists and/or parameter type lists considered obsolete? +Back: Identifier type lists. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +Define an addition function using identifier type lists. +Back: +```c +int add(x, y) int x; int y; { return x + y; } +``` +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +Define an addition function using parameter type lists. +Back: +```c +int add(int x, int y) { return x + y; } +``` +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +Is the following a prototype or a declaration? +```c +void foo(); +``` +Back: A declaration. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(); +``` +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). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(x, y); +``` +Back: A function declaration cannot have a non-empty identifier list. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(x, y) int x; int y; +``` +Back: A function declaration cannot have a non-empty identifier list. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(x, y) int x; int y; {} +``` +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). + +END%% + +%%ANKI +Basic +What function prototype is declared in the following? +```c +void foo(x, y) int x; int y; {} +``` +Back: N/A. No prototype has been declared. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(); + +int main(void) { foo(1); } +``` +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). + +END%% + +%%ANKI +Basic +Is the following a prototype or a declaration? +```c +void f(void); +``` +Back: A prototype. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(void); +``` +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). + +END%% + +%%ANKI +Basic +What compilation error does the following raise? +```c +void foo(void); + +int main(void) { foo(1); } +``` +Back: Too many arguments to function call `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). + +END%% + +%%ANKI +Basic +What function prototype is declared in the following? +```c +void foo(int x, int y); +``` +Back: `void foo(int, int)` +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +What function prototype is declared in the following? +```c +void foo(int x, int y) {} +``` +Back: `void foo(int, int)` +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +How many arguments does the following declaration specify? +```c +void foo(); +``` +Back: Some number unknown to the compiler. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +How many arguments does the following declaration specify? +```c +void foo(void); +``` +Back: Zero. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +Why might the following snippet raise a compilation error? +```c +int foo(); +int foo(int a); +``` +Back: N/A. It likely wouldn't. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +*Why* might the following snippet raise a compilation error? +```c +int foo(); +int foo(float a); +``` +Back: Conflicting types. The first `foo` declares any `float` passed to it default promotes to a `double`. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +*Why* might the following snippet raise a compilation error? +```c +int foo(); +int foo(char a); +``` +Back: Conflicting types. The first `foo` declares any `char` passed to it default promotes to an `int`. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +%%ANKI +Basic +*Why* might the following snippet raise a compilation error? +```c +int foo(); +int foo(double a); +``` +Back: N/A. It likely wouldn't. +Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf). + +END%% + +### main + +`main` is a special function serving as the entrypoint to C programs. It can have several different prototypes, but the following two are always possible: + +```c +int main(void); +int main(int argc, char* argv[argc+1]); +``` + +The only two return values guaranteed to work on all platform is `EXIT_SUCCESS` and `EXIT_FAILURE`. Reaching the end of `main` is equivalent to a `reutrn` with value `EXIT_SUCCESS`. + +%%ANKI +Basic +Which function serves as the entrypoint of C programs? +Back: `main` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How many valid prototypes of `main` are available? +Back: Indeterminate. Depends on the system. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +How many "official" prototypes of `main` are available? +Back: Two. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What are the "official" prototypes of `main`? +Back: +```c +int main(void); +int main(int argc, char* argv[argc + 1]); +``` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What are the "official" prototypes of `main`? +Back: +```c +int main(void); +int main(int argc, char* argv[argc + 1]); +``` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). +END%% + +%%ANKI +Basic +What are the only portable values that `main` can return? +Back: `EXIT_SUCCESS` and `EXIT_FAILURE`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which library defines `EXIT_SUCCESS`? +Back: `stdlib.h` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which library defines `EXIT_FAILURE`? +Back: `stdlib.h` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What happens when `main` does not explicitly return a value? +Back: `EXIT_SUCCESS` is implicitly returned. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +Returning {1:`s`} in {1:`main`} is equivalent to invoking function {2:`exit`} with argument {2:`s`}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which library declares the following prototype? +```c +noreturn void exit(int) +``` +Back: `stdlib.h` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does keyword `_Noreturn` indicate? +Back: The associated callee will never return control back to the caller. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What kind of syntactical construct is `_Noreturn`? +Back: A special keyword. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What kind of syntactical construct is `noreturn`? +Back: A macro. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +{1:`_Noreturn`} is a {2:keyword} whereas {2:`noreturn`} is a {1:macro}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which library is `noreturn` defined in? +Back: `stdnoreturn.h` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the value of `argv[0]`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: The name of the program invocation. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the value of `argv[argc]`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: `0` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the value of `argv[1]`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: `0` if `argc == 1` else the first argument to the program. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the minimum value of `argc`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: `1` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following prototype. What is the minimum length of `argv`? +```c +int main(int argc, char* argv[argc + 1]); +``` +Back: `2` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +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). \ No newline at end of file diff --git a/notes/c17/pointers.md b/notes/c17/pointers.md new file mode 100644 index 0000000..f05fdd5 --- /dev/null +++ b/notes/c17/pointers.md @@ -0,0 +1,599 @@ +--- +title: Pointers +TARGET DECK: Obsidian::STEM +FILE TAGS: c17::pointer +tags: + - c17 +--- + +## Overview + +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 +*Why* does a pointer's size match the machine's word size? +Back: Because it should be able to refer to any virtual address. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +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). + +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). + +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). + +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). + +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). + +END%% + +%%ANKI +Basic +Suppose a pointer logically evaluates to `true`. Why might it still be unsafe to use? +Back: Logical evaluation can't distinguish valid pointers from indeterminate pointers. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +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). + +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). + +END%% + +%%ANKI +Basic +How is `r->field` equivalently written using `*`? +Back: `(*r).field` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How is `(*r).field` more compactly written? +Back: `r->field` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +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). + +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). + +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. + +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. + +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. + +END%% + +%%ANKI +Basic +How do we rewrite the return statement using pointer arithmetic? +```c +int func() { + int A[3][4]; + return A[1][2]; +} +``` +Back: `return *(A + 6);` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How do we rewrite the return statement using pointer arithmetic? +```c +int func() { + int A[3][4]; + return A[2][0]; +} +``` +Back: `return *(A + 8);` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How do we rewrite the return statement using pointer arithmetic? +```c +int func() { + int A[4][3]; + return A[1][2]; +} +``` +Back: `return *(A + 5);` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How do we rewrite the return statement using pointer arithmetic? +```c +int func() { + int A[4][3]; + return A[1][0]; +} +``` +Back: `return *(A + 3);` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How is the `return` statement likely translated to x86-64? +```c +int* func(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 + +END%% + +%%ANKI +Basic +How is the `return` statement likely translated to x86-64? +```c +int func(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 + +END%% + +%%ANKI +Basic +How is the `return` statement likely translated to x86-64? +```c +int func(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 + +END%% + +%%ANKI +Basic +How is the `return` statement likely translated to x86-64? +```c +int* func(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 + +END%% + +%%ANKI +Basic +Suppose `char *p` has address `S`. What is the result of the following? +```c +(int *) p + 7 +``` +Back: `S + 28` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Suppose `char *p` has address `S`. What is the result of the following? +```c +(int *) (p + 7) +``` +Back: `S + 7` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +How do we declare a function pointer `fp` to the following? +```c +int foo(int x, int *p); +``` +Back: +```c +int (*fp)(int, int *); +``` +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What do the following two lines declare instances of? +```c +int (*fp)(int, int *); +int *fp(int, int *); +``` +Back: The first is a function pointer. The second is a function. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +What is the value of a function pointer? +Back: The address of the first instruction in the function's machine-code representation. +Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. + +END%% + +%%ANKI +Basic +Trap representations are most relevant to what kind of derivied type? +Back: Pointers. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What does a trap representation refer to? +Back: An invalid interpretation of a bit pattern as a specific type. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What happens when accessing an object with a trap representation of its type? +Back: Undefined behavior. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +*Why* must a dereferenced object have the correct designated type? +Back: A trap representation of an object's type leads to undefined behavior. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Refer to the following. *Why* isn't the pointer addition considered correct? +```c +double A[2] = { 0.0, 1.0 }; +double* p = &A[0] + 2; +``` +Back: N/A. It is. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Refer to the following. *Why* isn't the pointer addition considered correct? +```c +double A[2] = { 0.0, 1.0 }; +double* p = &A[0] + 3; +``` +Back: Pointers cannot refer to addresses beyond that immediately following the array. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Refer to the following. What values of `N` yield a valid pointer assignment? +```c +double A[2] = { 0.0, 1.0 }; +double* p = &A[N]; +``` +Back: `0`, `1`, and `2`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Refer to the following. What is the first value of `N` that yields an invalid pointer assignment? +```c +double A[2] = { 0.0, 1.0 }; +double* p = &A[N]; +``` +Back: `3` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Refer to the following. Is the last line a valid dereference? +```c +double A[2] = { 0.0, 1.0 }; +double a = *(&A[1]); +``` +Back: Yes. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Refer to the following. Is the last line a valid dereference? +```c +double A[2] = { 0.0, 1.0 }; +double a = *(&A[2]); +``` +Back: No. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Cloze +A pointer must point to a {valid object}, or {one position beyond} a valid object, or be {null}. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following. At what point *could* the program crash? +```c +double A[] = { 0.0, 1.0 }; +double* p = &A[0] + 2; +double q = *p; +``` +Back: On the third line. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Consider the following. At what point *could* the program crash? +```c +double A[] = { 0.0, 1.0 }; +double* p = &A[0] + 3; +double q = *p; +``` +Back: On the second line. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +## NULL + +The `NULL` macro refers to a **null pointer constant**, an ICE with value `0` or such an expression cast to type `void*`. The following table lists some valid values `NULL` can take on: + +| Expansion | Type | +| -------------------------- | -------------------- | +| `0U` | `unsigned` | +| `0` | `signed` | +| `\0` | `signed` | +| Enum constant of value `0` | `signed` | +| `0UL` | `unsigned long` | +| `0L` | `signed long` | +| `0ULL` | `unsigned long long` | +| `0LL` | `signed long long` | +| `(void*)0` | `void*` | + +%%ANKI +Basic +How are null pointer constants defined in terms of ICEs? +Back: As any ICE with value `0` or such an expression cast to type `void*`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What *must* the `NULL` macro expand to? +Back: Any null pointer constant. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which of the following members of the list are ICEs? +```c +0U, '\0', 0UL, (void*)0, 5LL +``` +Back: `0U`, `\0`, and `0UL`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which of the following members of the list are null pointer constants? +```c +0U, '\0', 0UL, (void*)0, 5LL +``` +Back: `0U`, `\0`, `0UL`, and `(void*)0`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which of the following members of the list could `NULL` be identical to? +```c +0U, '\0', 0UL, (void*)0, 5LL +``` +Back: `0U`, `\0`, `0UL`, and `(void*)0`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Which of the following members of the list are pointer constants? +```c +0U, '\0', 0UL, (void*)0, 5LL +``` +Back: Just `(void*)0`. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Why does Gustedt discourage use of `NULL`? +Back: The type of value it expands to is implementation-specific. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What is wrong with the following invocation? +```c +printf("%d, %p", 1, NULL); +``` +Back: `NULL` may not refer to a pointer type. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +What value must `NULL` have for the following to be correct? +```c +printf("%d, %p", 1, NULL); +``` +Back: `(void*)0` +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +## Aliasing + +Accessing the same object through different pointers is called **aliasing**. With the exclusion of [[simple#Character Types|character types]], only pointers of the same base type may alias. + +%%ANKI +Basic +What does aliasing refer to? +Back: Accessing the same object through different pointers. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is aliasing possible in the following function? +```c +void foo(double const* a, double* b); +``` +Back: Yes. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +%%ANKI +Basic +Is aliasing possible in the following function? +```c +void foo(double const* a, float* b); +``` +Back: No. +Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). + +END%% + +## Bibliography + +* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). \ No newline at end of file diff --git a/notes/c17/types/derived.md b/notes/c17/types/derived.md index bee5b36..36404f9 100644 --- a/notes/c17/types/derived.md +++ b/notes/c17/types/derived.md @@ -865,696 +865,11 @@ END%% ## Pointers -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 -*Why* does a pointer's size match the machine's word size? -Back: Because it should be able to refer to any virtual address. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -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). - -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). - -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). - -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). - -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). - -END%% - -%%ANKI -Basic -Suppose a pointer logically evaluates to `true`. Why might it still be unsafe to use? -Back: Logical evaluation can't distinguish valid pointers from indeterminate pointers. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -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). - -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). - -END%% - -%%ANKI -Basic -How is `r->field` equivalently written using `*`? -Back: `(*r).field` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How is `(*r).field` more compactly written? -Back: `r->field` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -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). - -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). - -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. - -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. - -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. - -END%% - -%%ANKI -Basic -How do we rewrite the return statement using pointer arithmetic? -```c -int func() { - int A[3][4]; - return A[1][2]; -} -``` -Back: `return *(A + 6);` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How do we rewrite the return statement using pointer arithmetic? -```c -int func() { - int A[3][4]; - return A[2][0]; -} -``` -Back: `return *(A + 8);` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How do we rewrite the return statement using pointer arithmetic? -```c -int func() { - int A[4][3]; - return A[1][2]; -} -``` -Back: `return *(A + 5);` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How do we rewrite the return statement using pointer arithmetic? -```c -int func() { - int A[4][3]; - return A[1][0]; -} -``` -Back: `return *(A + 3);` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How is the `return` statement likely translated to x86-64? -```c -int* func(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 - -END%% - -%%ANKI -Basic -How is the `return` statement likely translated to x86-64? -```c -int func(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 - -END%% - -%%ANKI -Basic -How is the `return` statement likely translated to x86-64? -```c -int func(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 - -END%% - -%%ANKI -Basic -How is the `return` statement likely translated to x86-64? -```c -int* func(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 - -END%% - -%%ANKI -Basic -Suppose `char *p` has address `S`. What is the result of the following? -```c -(int *) p + 7 -``` -Back: `S + 28` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Suppose `char *p` has address `S`. What is the result of the following? -```c -(int *) (p + 7) -``` -Back: `S + 7` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -How do we declare a function pointer `fp` to the following? -```c -int foo(int x, int *p); -``` -Back: -```c -int (*fp)(int, int *); -``` -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What do the following two lines declare instances of? -```c -int (*fp)(int, int *); -int *fp(int, int *); -``` -Back: The first is a function pointer. The second is a function. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -What is the value of a function pointer? -Back: The address of the first instruction in the function's machine-code representation. -Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016. - -END%% - -%%ANKI -Basic -Trap representations are most relevant to what kind of derivied type? -Back: Pointers. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What does a trap representation refer to? -Back: An invalid interpretation of a bit pattern as a specific type. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What happens when accessing an object with a trap representation of its type? -Back: Undefined behavior. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -*Why* must a dereferenced object have the correct designated type? -Back: A trap representation of an object's type leads to undefined behavior. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Refer to the following. *Why* isn't the pointer addition considered correct? -```c -double A[2] = { 0.0, 1.0 }; -double* p = &A[0] + 2; -``` -Back: N/A. It is. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Refer to the following. *Why* isn't the pointer addition considered correct? -```c -double A[2] = { 0.0, 1.0 }; -double* p = &A[0] + 3; -``` -Back: Pointers cannot refer to addresses beyond that immediately following the array. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Refer to the following. What values of `N` yield a valid pointer assignment? -```c -double A[2] = { 0.0, 1.0 }; -double* p = &A[N]; -``` -Back: `0`, `1`, and `2`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Refer to the following. What is the first value of `N` that yields an invalid pointer assignment? -```c -double A[2] = { 0.0, 1.0 }; -double* p = &A[N]; -``` -Back: `3` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Refer to the following. Is the last line a valid dereference? -```c -double A[2] = { 0.0, 1.0 }; -double a = *(&A[1]); -``` -Back: Yes. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Refer to the following. Is the last line a valid dereference? -```c -double A[2] = { 0.0, 1.0 }; -double a = *(&A[2]); -``` -Back: No. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -A pointer must point to a {valid object}, or {one position beyond} a valid object, or be {null}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following. At what point *could* the program crash? -```c -double A[] = { 0.0, 1.0 }; -double* p = &A[0] + 2; -double q = *p; -``` -Back: On the third line. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Consider the following. At what point *could* the program crash? -```c -double A[] = { 0.0, 1.0 }; -double* p = &A[0] + 3; -double q = *p; -``` -Back: On the second line. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -### NULL - -The `NULL` macro refers to a **null pointer constant**, an ICE with value `0` or such an expression cast to type `void*`. The following table lists some valid values `NULL` can take on: - -| Expansion | Type | -| -------------------------- | -------------------- | -| `0U` | `unsigned` | -| `0` | `signed` | -| `\0` | `signed` | -| Enum constant of value `0` | `signed` | -| `0UL` | `unsigned long` | -| `0L` | `signed long` | -| `0ULL` | `unsigned long long` | -| `0LL` | `signed long long` | -| `(void*)0` | `void*` | - -%%ANKI -Basic -How are null pointer constants defined in terms of ICEs? -Back: As any ICE with value `0` or such an expression cast to type `void*`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What *must* the `NULL` macro expand to? -Back: Any null pointer constant. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which of the following members of the list are ICEs? -```c -0U, '\0', 0UL, (void*)0, 5LL -``` -Back: `0U`, `\0`, and `0UL`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which of the following members of the list are null pointer constants? -```c -0U, '\0', 0UL, (void*)0, 5LL -``` -Back: `0U`, `\0`, `0UL`, and `(void*)0`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which of the following members of the list could `NULL` be identical to? -```c -0U, '\0', 0UL, (void*)0, 5LL -``` -Back: `0U`, `\0`, `0UL`, and `(void*)0`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Which of the following members of the list are pointer constants? -```c -0U, '\0', 0UL, (void*)0, 5LL -``` -Back: Just `(void*)0`. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Why does Gustedt discourage use of `NULL`? -Back: The type of value it expands to is implementation-specific. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What is wrong with the following invocation? -```c -printf("%d, %p", 1, NULL); -``` -Back: `NULL` may not refer to a pointer type. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What value must `NULL` have for the following to be correct? -```c -printf("%d, %p", 1, NULL); -``` -Back: `(void*)0` -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -### Aliasing - -Accessing the same object through different pointers is called **aliasing**. With the exclusion of [[simple#Character Types|character types]], only pointers of the same base type may alias. - -%%ANKI -Basic -What does aliasing refer to? -Back: Accessing the same object through different pointers. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is aliasing possible in the following function? -```c -void foo(double const* a, double* b); -``` -Back: Yes. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Is aliasing possible in the following function? -```c -void foo(double const* a, float* b); -``` -Back: No. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% +Pointers are another type of derived type. They are listed in more detail [[pointers|here]]. ## Functions -A function `f` without a following opening `(` is converted to a pointer to its start. This is called **function decay**. - -%%ANKI -Basic -What is the effect of function decay? -Back: Evaluation of a function `f` without a following opening `(` is converted to a pointer to its start. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -What name is given to the implicit conversion of a function to a pointer? -Back: Function decay. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -According to Gustedt, what C feature explains why are there no "function values"? -Back: Function-to-pointer decay. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -Why can't functions directly be made arguments to functions? -Back: Because function arguments decay to pointers. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -{1:Function pointers} are to {2:`(...)`} whereas {2:pointers} are to {1:`[...]`}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -f(3); -``` -Back: Decay, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -(&f)(3); -``` -Back: Address of, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -(*f)(3); -``` -Back: Decay, dereference, decay, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -(*&f)(3); -``` -Back: Address of, dereference, decay, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Basic -In what order are decays, dereferences, address ofs, and calls performed in the following? -```c -(&*f)(3); -``` -Back: Decay, dereference, address of, call. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% - -%%ANKI -Cloze -{1:Pointers} refer to {2:arrays} whereas {2:function pointers} refer to {1:functions}. -Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). - -END%% +Functions are another type of derived type. They are listed in more detail [[c17/functions|here]]. ## Bibliography diff --git a/notes/calculus/images/postage-function.png b/notes/calculus/images/postage-function.png new file mode 100644 index 0000000..6ce82c0 Binary files /dev/null and b/notes/calculus/images/postage-function.png differ diff --git a/notes/calculus/intervals.md b/notes/calculus/intervals.md new file mode 100644 index 0000000..90dea6d --- /dev/null +++ b/notes/calculus/intervals.md @@ -0,0 +1,232 @@ +--- +title: Intervals +TARGET DECK: Obsidian::STEM +FILE TAGS: calculus::intervals +tags: + - calculus +--- + +## Overview + +An interval corresponds to a continuous segment of the real number line. There are a few different types. For all $a, b \in \mathbb{R}$ satisfying $a < b$: + +* $[a, b]$ denotes a **closed interval**, all $x$ satisfying $a \leq x \leq b$; +* $(a, b)$ denotes an **open interval**, all $x$ satisfying $a < x < b$; +* $(a, b]$ denotes a **half-open interval**, all $x$ satisfying $a < x \leq b$; +* $[a, b)$ denotes a half-open interval, all $x$ satisfying $a \leq x < b$. + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ and consider interval $[a, b]$. How is $a$ and $b$ assumed to relate? +Back: $a < b$ +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Cloze +Let $a, b \in \mathbb{R}$ s.t. $a < b$. Then {$[a, b]$} denotes a {closed} interval. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$. How is $[a, b]$ expressed as a chain of inequalities? +Back: As all $x$ satisfying $a \leq x \leq b$. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Cloze +Let $a, b \in \mathbb{R}$ s.t. $a < b$. Then {$(a, b)$} denotes an {open} interval. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$. How is $(a, b)$ expressed as a chain of inequalities? +Back: As all $x$ satisfying $a < x < b$. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Cloze +Let $a, b \in \mathbb{R}$ s.t. $a < b$. Then {$(a, b]$ and $[a, b)$]} denote {half-open} intervals. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$. How is $(a, b]$ expressed as a chain of inequalities? +Back: As all $x$ satisfying $a < x \leq b$. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$. How is $[a, b)$ expressed as a chain of inequalities? +Back: As all $x$ satisfying $a \leq x < b$. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Cloze +Interval {$(a, b)$} is called the {interior} of interval $[a, b]$. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +## Partitions + +Let $a, b \in \mathbb{R}$ such that $a < b$. A **partition** $P$ of interval $[a, b]$ is a set of points $x_0 = a, x_1, \ldots, x_{n-1}, x_n = b$ satisfying $$x_0 < x_1 < \cdots < x_{n-1} < x_n.$$ + +We use the symbol $P = \{x_0, x_1, \ldots, x_n\}$ to designate this partition. + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$. What is a partition of interval $[a, b]$? +Back: A set of points $x_0 = a, x_1, \ldots, x_{n-1}, x_n = b$ satisfying $$x_0 < x_1 < \cdots < x_{n-1} < x_n.$$ +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$. How is a partition of interval $[a, b]$ denoted? +Back: As $\{a, x_1, \ldots, x_{n-1}, b\}$ where $a < x_1 < \cdots < x_{n-1} < b$. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$ and $P = \{a, x_1, \ldots, x_{n-1}, b\}$ be a partition of $[a, b]$. What are $P$'s closed subintervals? +Back: $[a, x_1]$, $[x_1, x_2]$, $\ldots$, $[x_{n-1}, b]$ +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$ and $P = \{a, x_1, \ldots, x_{n-1}, b\}$ be a partition of $[a, b]$. What are $P$'s open subintervals? +Back: $(a, x_1)$, $(x_1, x_2)$, $\ldots$, $(x_{n-1}, b)$ +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $a, b \in \mathbb{R}$ s.t. $a < b$ and $P = \{a, x_1, \ldots, x_{n-1}, b\}$ be a partition of $[a, b]$. How are the members of $P$ assumed to relate? +Back: $a < x_1 < \cdots < x_{n-1} < b$ +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +A **refinement** $P'$ of some partition $P$ is created by adjoining more subdivision points to those of $P$. $P'$, also a partition, is said to be **finer** than $P$. + +%%ANKI +Basic +Let $P$ be a partition of $[a, b]$. What is a refinement of $P$? +Back: A partition created by adjoining one or more subdivision points to those of $P$. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $P'$ be a refinement on partition $P$ of $[a, b]$. How do $P$ and $P'$ relate? +Back: $P \subseteq P'$ +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $P'$ be a refinement on partition $P$ of $[a, b]$. Which of $P$ or $P'$ have more subdivision points? +Back: $P'$ +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Cloze +Let $P'$ be a refinement on partition $P$ of $[a, b]$. Then $P'$ is said to be {finer} than $P$. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +## Step Functions + +A function $s$, whose domain is a closed interval $[a, b]$, is called a **step function** if and only if there exists a [[#Partitions|partition]] $P = \{a, x_1, \ldots, x_{n-1}, b\}$ of $[a, b]$ such that $s$ is constant on each open subinterval of $P$. + +> At each of the endpoints $x_{k-1}$ and $x_k$, the function must have some well-defined value. + +Step functions are also called **piecewise constant functions**. + +%%ANKI +Basic +With maximum specificity, what is the domain of a step function? +Back: A closed interval. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Suppose $s$ is a step function with domain $[a, b]$. This implies existence of what? +Back: A partition of $[a, b]$ such that $s$ is constant on each open subinterval. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $s$ be a step function on $[a, b]$. What of its corresponding partition must be constant? +Back: The value along each open subinterval. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +Let $s$ be a step function on $[a, b]$ with partition $P$. What criteria must the endpoints of $P$'s open subintervals satisfy? +Back: They must be well-defined. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Cloze +{Step} functions are also called {piecewise constant} functions. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +The following is an example of what kind of function? +![[postage-function.png]] +Back: A step function. +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +%%ANKI +Basic +What minimally refined partition is associated with the following step function? +![[postage-function.png]] +Back: $\{0, 1, 2, 3, 4\}$ +Reference: Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). + +END%% + +## Bibliography + +* Tom M. Apostol, _Calculus, Vol. 1: One-Variable Calculus, with an Introduction to Linear Algebra_, 2nd ed. (New York: Wiley, 1980). \ No newline at end of file diff --git a/notes/set/cardinality.md b/notes/set/cardinality.md index 8c28ab1..1171cf4 100644 --- a/notes/set/cardinality.md +++ b/notes/set/cardinality.md @@ -8,7 +8,7 @@ tags: ## Equinumerosity -We say set $A$ is **equinumerous** to set $B$, written ($A \approx B$) if and only if there exists a [[functions#Injections|one-to-one]] function from $A$ [[functions#Surjections|onto]] $B$. +We say set $A$ is **equinumerous** to set $B$, written ($A \approx B$) if and only if there exists a [[set/functions#Injections|one-to-one]] function from $A$ [[set/functions#Surjections|onto]] $B$. %%ANKI Basic