notebook/notes/c17/index.md

12 KiB

title TARGET DECK FILE TAGS tags
C17 Obsidian::STEM c17
c17

Overview

A C program can be seen as a sort of machine that manipulates values: the particular values that variables of the program have at a given time, and also intermediate values that are the result of computed expressions.

This quote describes C's abstract state machine. Whatever instructions a C program compiles down to is "unimportant" provided that all observable states are correctly reproduced. This is the essence of optimization.

%%ANKI Basic What feature of C's abstract state machine makes C performant? Back: The ability to optimize. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic C can compile into any sequence of instructions provided what holds? Back: All observable states are correctly reproduced. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic Why is C's abstract state machine called the way it is? Back: Compilers are free to realize the state machine however they see fit. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What three components make up C's abstract state machine? Back: Values, types, and binary representations. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Cloze In C's abstract state machine, {binary representations} describe {types} which describe {values}. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

Values

An object is a region of data storage in the execution environment, the contents of which can represent values. An lvalue is an expression (with non-void object type) that potentially designates an object. An rvalue is the "value of the expression."

The notion of a value in C is an abstract entity. It exists beyond the program or the representation of the value in the program. For example, the value 0 (no matter how its represented) added to variable x should always yield result x regardless of platform.

%%ANKI Basic What does an object refer to? Back: A region of data storage in the execution environment. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic What does a value refer to? Back: The contents of an object when interpreted as having a specific type. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Why does Gustedt refer to values as abstract entities? Back: A value exists beyond a program or any particular representation. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic How does Gustedt distinguish the data of a program execution from values? Back: The data is the set of values of all objects at a given moment. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Cloze A {value} refers to the contents of an {object} when interpreted as having a specific type. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic What is an lvalue? Back: An expression (with non-void object type) that potentially designates an object. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Why are lvalues named the way they are? Back: The name is an acronym for locator value (or left value). Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic What is an rvalue? Back: The value of an expression. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Why are rvalues named the way they are? Back: The name is an acronym for right value. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Can an lvalue designate an object? Back: Yes. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Can an lvalue designate a function? Back: No. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is x an lvalue or rvalue in the following snippet?

int x = 10;

Back: An lvalue. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is int an lvalue or rvalue in the following snippet?

int x = 10;

Back: Neither. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is 10 an lvalue or rvalue in the following snippet?

int x = 10;

Back: An rvalue. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Why is x an lvalue in the following?

int x = 10;

Back: Because x refers to a memory location. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is x an lvalue or rvalue in the following snippet?

void x;

Back: Neither. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is x an lvalue or rvalue in the following snippet?

void *x;

Back: An lvalue. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is y an lvalue or rvalue in the following snippet?

int y = x + 10;

Back: An lvalue. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is x an lvalue or rvalue in the following snippet?

int y = x + 10;

Back: An rvalue. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic In the second line, is ptr an lvalue or rvalue?

int *ptr = &x;
*ptr = 10;

Back: An lvalue. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic In the second line, is *ptr an lvalue or rvalue?

int *ptr = &x;
*ptr = 10;

Back: An lvalue. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is getValue an lvalue or rvalue in the following snippet?

int getValue () {
  return 42;
}

Back: Neither. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Is getValue() an lvalue or rvalue in the following snippet?

int d = getValue();

Back: An rvalue. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Why is getValue in the following snippet neither an lvalue nor an rvalue?

int getValue() {
  return 42;
}

Back: The function name is just syntax. That is, it isn't an expression. Reference: “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Are variables typically lvalues or rvalues? Back: lvalues. Reference: ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Are constants typically lvalues or rvalues? Back: rvalues. Reference: ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Are array elements typically lvalues or rvalues? Back: lvalues. Reference: ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Are dereferenced pointers typically lvalues or rvalues? Back: lvalues. Reference: ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

%%ANKI Basic Are temporary values typically lvalues or rvalues? Back: rvalues. Reference: ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.

END%%

Types

Types are additional properties that C associates with values. All values have a type that is statically determined and all possible operations on a value are determined by its type.

%%ANKI Cloze Possible operations on a {value} are determined by its {type}. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

Representation

The binary representation of a type is the model used to represent values of said type on a given platform. The object representation of a type determines how values are stored in memory, disk, etc.

%%ANKI Basic What is the binary representation of a type? Back: The model used to represent values of the type on a given platform. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What is the binary representation of a value? Back: N/A. Binary representations describe types not values. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic What is the object representation of a type? Back: How a value of a given type is actually stored in memory, disk, etc. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Cloze A {type}'s {binary representation} determines the results of all operations. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Basic Why does Gustedt refer to binary representations as abstract entities? Back: Binary representations don't completely determine how values are stored in memory. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

%%ANKI Cloze A {binary} representation is abstract whereas an {object} representation is concrete. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).

END%%

Bibliography