Notes on pointers.

main
Joshua Potter 2024-11-23 15:21:18 -07:00
parent 3b87d57d89
commit a01123d579
7 changed files with 201 additions and 8 deletions

View File

@ -333,7 +333,7 @@
"_journal/2024-02/2024-02-21.md": "f423137ae550eb958378750d1f5e98c7",
"_journal/2024-02-23.md": "219ce9ad15a8733edd476c97628b71fd",
"_journal/2024-02/2024-02-22.md": "312e55d57868026f6e80f7989a889c2b",
"c17/strings.md": "c876fc99f9980b4cb2bd9abb6b821322",
"c17/strings.md": "02f329f678efe939dbb5501399130296",
"c17/index.md": "611dd21439d56575172ef004c7635382",
"c17/escape-sequences.md": "a8b99070336878b4e8c11e9e4525a500",
"c17/declarations.md": "c853b9584365ba69ac5845082061a8dd",
@ -800,7 +800,7 @@
"c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875",
"c17/types/simple.md": "44d56a2998f3bba578f14672c8b0ee3c",
"c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d",
"c17/types/derived.md": "dcac68790016190ce5c4196bd9c1d53d",
"c17/types/derived.md": "3776627765547e91086735ab834c2de1",
"c17/types/basic.md": "5064e21e683c0218890058882e06b6f3",
"c17/types/index.md": "14b651bcfc8b2d62ffd200a74a9a2a6b",
"_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2",
@ -911,7 +911,7 @@
"_journal/2024-10/2024-10-24.md": "7a6b0b950b0492b4bf178a52a1a86de5",
"_journal/2024-10-29.md": "19444fb3b9bef82b200c77a6acc33637",
"_journal/2024-10/2024-10-28.md": "95bcec30eda031bf6410ff51a61b0ee3",
"c17/typedefs.md": "1fcb044f12c4f16de1ef8cb4c293dd10",
"c17/typedefs.md": "e2c1f02c6c8286375b150bca83ccbd27",
"_journal/2024-11-04.md": "69ac4ebe34ab5609a444bc9c46b55b84",
"_journal/2024-11/2024-11-03.md": "8c5e70f566953974f252da9472e527f0",
"_journal/2024-11/2024-11-02.md": "8c5e70f566953974f252da9472e527f0",
@ -931,7 +931,7 @@
"_journal/2024-11/2024-11-07.md": "434ec3f15d7065ea740127aa8477dd17",
"x86-64/directives.md": "019c1c1d04efb26c3e8758aac4543cc7",
"geometry/cartesian.md": "52eed93bf2d456ffab76e73d1031187c",
"geometry/index.md": "130185982889f115c9415f14c4424848",
"geometry/index.md": "cac68c1b624dbb0552e56cce47bcc21d",
"_journal/2024-11-10.md": "5478337fd2017b99d0b359713a511e66",
"_journal/2024-11/2024-11-09.md": "46f3a640223ef533f4523837b67b57c3",
"_journal/2024-11-18.md": "5567592053951cee80450cf582df270a",
@ -952,7 +952,7 @@
"_journal/2024-11/2024-11-19.md": "d879f57154cb27cb168eb1f1f430e312",
"set/cardinality.md": "499e758bc0929af06736fa2974aade60",
"geometry/area.md": "3c6e53a64ad3150d8f81f6e4a63da61a",
"_journal/2024-11-23.md": "cd8db62b2aa0d67d50c0aa8257956732",
"_journal/2024-11-23.md": "911f82ab8aede5ecdb96493aef64b0b9",
"_journal/2024-11/2024-11-22.md": "51117030e2364dbce3a8d507dead86ae"
},
"fields_dict": {

View File

@ -2,8 +2,11 @@
title: "2024-11-23"
---
- [ ] Anki Flashcards
- [x] Anki Flashcards
- [x] KoL
- [x] OGS
- [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story)
- [ ] Korean (Read 1 Story)
* Notes on Apostol's axiomatic definition of [[area]].
* More notes on [[derived#Pointers|pointers]].

View File

@ -440,7 +440,7 @@ END%%
%%ANKI
Basic
What is the transpose symmetric property of $\Theta$-notation?
Back: N/A
Back: N/A.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1709752223322-->
END%%

View File

@ -136,6 +136,8 @@ Tags: printf
<!--ID: 1708425941269-->
END%%
### Flags
| Flag | Description |
| ---- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `-` | Left-aligns the output |
@ -400,6 +402,8 @@ Tags: printf
<!--ID: 1710673807990-->
END%%
### Length
Length | Description
--------- | -----------
`hh` | `int` sized integer argument promoted from a `char`
@ -457,6 +461,8 @@ Tags: printf
<!--ID: 1708426673725-->
END%%
### Specifiers
Specifier | Description
--------- | -----------
`d`, `i` | a decimal `signed int`
@ -960,6 +966,15 @@ Tags: printf
<!--ID: 1707852083126-->
END%%
%%ANKI
Basic
What should be done to a pointer outputted by the `%p` format specifier?
Back: It should be cast to a `void*` pointer.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
Tags: printf
<!--ID: 1732397726966-->
END%%
%%ANKI
Basic
Given `int64_t x`, why is `printf("%d", x)` a problem?

View File

@ -103,6 +103,40 @@ Reference: “ISO: Programming Languages - C,” April 12, 2011, [https://port70
<!--ID: 1730740461662-->
END%%
%%ANKI
Basic
When can two pointers be subtracted?
Back: Only if both refer to elements of the same array object.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732397726963-->
END%%
%%ANKI
Basic
What is the result of `p - q` in the following?
```c
double A[4] = { 0.0, 1.0, 2.0, -3.0 };
double* p = &A[1];
double* q = &A[3];
```
Back: `-2`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732397726964-->
END%%
%%ANKI
Basic
What is the result of `p - q` in the following?
```c
double A[4] = { 0.0, 1.0, 2.0, -3.0 };
double* p = &A[3];
double* q = &A[1];
```
Back: `2`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732397726965-->
END%%
%%ANKI
Basic
Is `size_t` signed or unsigned?

View File

@ -1133,6 +1133,143 @@ Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Program
<!--ID: 1731942192229-->
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).
<!--ID: 1732397726948-->
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).
<!--ID: 1732397726951-->
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).
<!--ID: 1732397726952-->
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).
<!--ID: 1732397726953-->
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).
<!--ID: 1732397726954-->
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).
<!--ID: 1732397726955-->
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).
<!--ID: 1732397726956-->
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).
<!--ID: 1732397726957-->
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).
<!--ID: 1732397726958-->
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).
<!--ID: 1732397726959-->
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).
<!--ID: 1732397726960-->
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).
<!--ID: 1732397726961-->
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).
<!--ID: 1732397726962-->
END%%
## Bibliography
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.

View File

@ -1,5 +1,9 @@
---
title: Geometry
TARGET DECK: Obsidian::STEM
FILE TAGS: geometry
tags:
- geometry
---
## Overview