40 KiB
title | TARGET DECK | FILE TAGS | tags | |
---|---|---|---|---|
Storage | Obsidian::STEM | c17::storage |
|
Overview
An object has a storage duration that determines its lifetime. There are four storage durations: static, thread, automatic, and allocated.
%%ANKI Cloze An {object} has a {storage duration} that determines its {lifetime}. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What are the four storage durations? Back: Static, thread, automatic, and allocated. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is an object's lifetime? Back: The portion of a program execution during which storage is guaranteed to be reserved for the object. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is the result of accessing an object within its lifetime? Back: The value last stored within the object. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is the result of accessing an object outside its lifetime? Back: Undefined behavior. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic How many different storage durations are there? Back: Four. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
Static
An object whose identifier is declared without storage-class specifier _Thread_local
, and either with external or internal linkage or with the storage-class specifier static
, has static storage duration.
Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup. Initialization always happens.
%%ANKI Basic What is the storage duration of an object whose identifer has external linkage? Back: Either static or thread. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
When; does an object whose identifier has external linkage have a static storage duration?
Back: When the identifier is declared without storage-class specifier _Thread_local
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
When; does an object whose identifier has external linkage have a thread storage duration?
Back: When the identifier is declared with storage-class specifier _Thread_local
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which storage class specifier(s) can be used to declare an object with static storage duration?
Back: extern
and static
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier x
?
int x;
Back: Static. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier x
?
extern int x;
Back: Static. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier x
?
static int x;
Back: Static. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier x
?
_Thread_local int x;
Back: Thread. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier x
?
void foo() { static int x; }
Back: Static. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is the lifetime of an object with static storage duration? Back: The entire program execution. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
When is an object with static storage duration initialized?
Back: Prior to program startup (before main
is called).
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic How many times can object with static storage duration be initialized? Back: Only once. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic An object with static storage duration must have what linkage? Back: Internal or external. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Does C initialize an object with static storage duration and no initializer? Back: Yes. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
What value does C initialize an object with static storage duration and no initializer to?
Back: 0
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
Thread Local
An object whose identifier is declared with the storage-class specified _Thread_local
has thread storage duration.
Its lifetime is the entire execution of the thread for which it is created, and its stored value is initialized when the thread is started.
%%ANKI
Basic
Which keyword declares an object with thread local storage duration?
Back: _Thread_local
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Cloze
{1:_Thread_local
} is a {2:keyword} whereas {2:thread_local
} is a {1:macro}.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which library is thread_local
defined in?
Back: <threads.h>
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which storage class specifier(s) can be used to declare an object with thread storage duration?
Back: _Thread_local
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What is the lifetime of an object with thread storage duration? Back: The entire execution fo the thread for which it is created. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic When is an object with thread storage duration initialized? Back: When its corresponding thread is started. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic An object with thread storage duration must have what linkage? Back: Internal or external. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Does C initialize an object with thread storage duration and no initializer? Back: Yes. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
What value does C initialize an object with thread storage duration and no initializer to?
Back: 0
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
Automatic
An object whose identifier is declared with no linkage and without the storage-class specifier static
has automatic storage duration, as do some compound literals.
%%ANKI
Basic
Which keyword is used to declare automatic storage duration?
Back: auto
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which storage class specifier is essentially never used in modern C?
Back: auto
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which storage class specifier(s) can be used to declare an object with automatic storage duration?
Back: auto
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier x
?
void foo() { int x; }
Back: Automatic. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the storage duration of the object with identifier x
?
void foo(int x) { }
Back: Automatic. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic An object with automatic storage duration must have what linkage? Back: None. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What are the three possible lifetimes of an automatic object? Back: Enclosing block, declaration scope, temporary. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Does C initialize an object with automatic storage duration and no initializer? Back: No. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic What value does C initialize an object with automatic storage duration and no initializer to? Back: N/A. C does not initialize these objects. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
For these objects that do not have a derived#Variable-Length type, its lifetime extends from entry into its enclosing block until execution of that block ends. Its initial value is indeterminate. If initialization is specified, it is performed each time the declaration or compound literal is reached in the execution of the block.
For these objects that do have a VLA type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration.
%%ANKI Basic Consider non-temporary object with automatic storage duration. What property determines its lifetime? Back: Whether or not the object has a VLA type. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic When is the lifetime of an object with automatic storage duration that of its enclosing block? Back: When it is a non-temporary object with non-VLA type. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Consider non-temporary, non-VLA object with automatic storage duration. What is its lifetime? Back: From entry into its enclosing block until execution of that block ends. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic When does the lifetime of an object with automatic storage duration match its declaration's scope? Back: When it is a non-temporary object with VLA type. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the lifetime of the object with identifier x
?
void foo(int x) { }
Back: From start of execution to end of execution of foo
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the lifetime of the object with identifier x
?
void foo() {
int z;
int y;
int x;
}
Back: Between the start and end of foo
's execution.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the value of x
at line 1?
void foo() {
int z = 0; // Line 1
int y = 1; // Line 2
int x = 2; // Line 3
}
Back: Indeterminate. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the value of x
at line 3?
void foo() {
int z = 0; // Line 1
int y = 1; // Line 2
int x = 2; // Line 3
}
Back: 2
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the lifetime of the object referred to by identifier x
?
void foo() {
int z;
{ int y; int x; }
int a;
}
Back: From the start to the end of execution of its surrounding block. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. Does x
's lifetime extend to line 1?
void foo() {
int z = 0; // Line 1
int y = 1; // Line 2
int x = 2; // Line 3
}
Back: Yes. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. Does x
's lifetime extend to line 1?
void foo() {
int z = 0; // Line 1
int y = 1; // Line 2
{ int x = 2; } // Line 3
}
Back: No. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. Does x
's lifetime extend to line 1?
void foo() {
int z = 0; // Line 1
{ int y = 1; } // Line 2
int x = 2; // Line 3
}
Back: Yes. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic Consider non-temporary, VLA object with automatic storage duration. What is its lifetime? Back: From declaration of the VLA to when it leaves its scope. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. Does x
's lifetime extend to line 1?
void foo(int n) {
int z = 0; // Line 1
int y = 1; // Line 2
int x[n]; // Line 3
}
Back: No. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. Does x
's lifetime extend to line 3?
void foo(int n) {
int x[n]; // Line 1
int z = 0; // Line 2
int y = 1; // Line 3
}
Back: Yes. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. What is the lifetime of the object with identifier x
?
void foo(int n) {
int z;
int y;
int x[n];
}
Back: Between its declaration and when foo
's execution ends.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. Does x
's lifetime extend to line 3?
void foo(int n) {
{ int x[n]; } // Line 1
int z = 0; // Line 2
int y = 1; // Line 3
}
Back: No. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider the following translation unit. Does x
's lifetime extend to line 3?
void foo(int n) {
int x[n]; // Line 1
int z = 0; // Line 2
{ int y = 1; } // Line 3
}
Back: Yes. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
A non-lvalue expression with structure or union type, where the structure or union contains a member with array type refers to an object with automatic storage duration and temporary lifetime. Its lifetime lasts for the duration of the expression evaluation it is found within.
%%ANKI
Basic
When is the lifetime of an object with automatic storage duration temporary?
Back: When specifying a non-lvalue expression with struct
/union
type containing an array member.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider an object with temporary lifetime and automatic storage duration. What must the object's type be?
Back: Either a struct
or union
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Consider an object with temporary lifetime and automatic storage duration. What must the struct
or union
contain?
Back: A member with array type.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which operator remains well-defined because of temporary lifetimes?
Back: []
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
According to Gustedt, what is the only reason objects with temporary lifetimes exist?
Back: To access array members of struct
/union
function return values.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic When does a temporary lifetime of an object end? Back: At the end of the enclosing (full) expression. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic When does the lifetime of the object with temporary lifetime end in the following?
printf("mem().org[0] is %u\n", mem().ory[0]);
Back: Once the argument to printf
is constructed.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic What data likely exists in an object with automatic storage duration before initialization? Back: Whatever data existed beforehand at its address in the stack. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020). Tags: x86-64
END%%
%%ANKI Basic What assembly-level translation justifies why the lifetime of objects with automatic storage duration may precede their declaration? Back: Stack space for automatic variables is allocated at function start. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020). Tags: x86-64
END%%
Register
In contrast to auto
, there also exists a register
#Storage-Class Specifiers. The &
operator is not allowed for variables declared with register
meaning variables declared with register
can't pointers#Aliasing.
Because of derived#Arrays, arrays with storage-class register
are useless.
%%ANKI
Basic
What restriction is enforced on objects with storage-class register
?
Back: The &
operator is not allowed.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Cloze
The {auto
} and {register
} storage-class specifiers are used with automatic storage duration.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
Variables declared with storage-class specifier can't alias?
Back: register
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
According to Gustedt, the register
storage-class specifier is useless for what kind of objects?
Back: Arrays.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
According to Gustedt, why is the register
storage-class specifier useless for arrays?
Back: In most contexts, arrays decay to an address in which case register
is not applicable.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
Dynamic Allocation
The <stdlib.h>
header provides the two most prominent functions used for managing dynamic memory: malloc
and free
. The former is used to allocate new memory whereas the latter is used to annihilate it.
void* malloc(size_t size);
void free(void*);
Dynamically allocated objects have allocated storage duration. Their lifetime spans allocation to explicit destruction.
%%ANKI
Basic
Which standard library header exposes malloc
?
Back: <stdlib.h>
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
Which standard library header exposes free
?
Back: <stdlib.h>
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
Why is malloc
named the way it is?
Back: It stands for memory allocate.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
Which two functions are most prominently used for managing dynamically allocated memory?
Back: malloc
and free
.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Cloze
Generally speaking, memory created with {malloc
} should be deleted with {free
}.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
What three specialized variants of malloc
are exposed by <stdlib.h>
?
Back: calloc
, realloc
, and aligned_alloc
.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
On success, what does a call to malloc
return?
Back: A valid void*
pointer.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
On failure, what does a call to malloc
return?
Back: A null pointer value.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
What are the argument(s) to malloc
?
Back: The size (in bytes) of the block of memory to allocate.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic What is wrong with the following code snippet?
double *d = (double*)malloc(sizeof *d);
Back: The return type of malloc
should not be cast.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic What is wrong with the following code snippet?
double *d = malloc(sizeof *d);
Back: N/A. This is valid. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
What possible error can occur by explicitly casting the return type of malloc
?
Back: If <stdlib.h>
isn't imported, older C compilers assume malloc
returns an int
.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
Without <stdlib.h>
, an older C compiler might assume what malloc
declaration?
Back:
int malloc();
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
Within <stdlib.h>
, what function prototype does malloc
have?
Back:
void* malloc(size_t size);
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Cloze
Storage allocated through malloc
is {uninitialized} and has {no type}.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic Does the following invoke undefined behavior? Why or why not?
free(0);
Back: No. free
ignores null pointer values.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic Does the following invoke undefined behavior? Why or why not?
double d = 0.0;
free(&d);
Back: Yes. d
was not dynamically allocated.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic Does the following invoke undefined behavior? Why or why not?
double* d = malloc(sizeof *d);
free(&d);
Back: Yes. d
was not dynamically allocated.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic Does the following invoke undefined behavior? Why or why not?
double* d = malloc(sizeof *d);
free(d);
Back: No. The address d
pointed to was dynamically allocated.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic Does the following invoke undefined behavior? Why or why not?
double* d = malloc(sizeof *d);
free(d);
d = 0;
free(d);
Back: No. free
ignores null pointer values.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic Does the following invoke undefined behavior? Why or why not?
double* d = malloc(sizeof *d);
free(d);
free(d);
Back: Yes. free
should not be invoked on already free
'd data.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
malloc
and its variants all have the size
parameter in what position?
Back: The last position.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
What storage duration is associated with malloc
and free
?
Back: Allocated.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic What is the lifetime of an allocated object? Back: From its allocation to when it is explicitly deallocated. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI
Basic
What two functions are typically used to deallocate an allocated object?
Back: free
and realloc
.
Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic Does C initialize an object with allocated storage duration and no initializer? Back: No. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
%%ANKI Basic What value does C initialize an object with allocated storage duration and no initializer to? Back: N/A. C does not initialize these objects. Reference: Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).
END%%
Storage-Class Specifiers
Storage-class specifiers are used to control an object's storage duration and an identifier's linkage. There are six storage-class specifiers:
typedef
- Discussed in typedefs.
- Is called a "storage-class specifier" for syntactic convenience only.
extern
static
_Thread_local
- Discussed in #Thread Local.
auto
- Discussed in #Automatic.
register
- Discussed in #Automatic.
At most, one storage-class specifier may be given in the declaration specifiers in a declaration, except that _Thread_local
may appear with static
or extern
.
%%ANKI Basic How many different storage-class specifiers are there? Back: Six. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which storage-class specifier isn't really a storage-class specifier?
Back: typedef
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Syntactically, what makes typedef
a special storage-class specifier?
Back: It is labeled a "storage-class specifier" for syntactic convenience.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
A _Thread_local
storage-class specifier can appear next to what other specifier(s)?
Back: static
and extern
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
An extern
storage-class specifier can appear next to what other specifier(s)?
Back: Just _Thread_local
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
A static
storage-class specifier can appear next to what other specifier(s)?
Back: Just _Thread_local
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which storage-class specifiers are most related to linkage?
Back: extern
and static
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI
Basic
Which storage-class specifiers are most related to automatic storage duration?
Back: auto
and register
.
Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What about an object might a storage-class specifier control? Back: The storage duration. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
%%ANKI Basic What about an identifier might a storage-class specifier control? Back: The linkage. Reference: “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
END%%
Bibliography
- “ISO: Programming Languages - C17,” April 2017, https://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf.
- Jens Gustedt, Modern C (Shelter Island, NY: Manning Publications Co, 2020).