CS354 Q2 — Memory & Addressing (Chunks 1–7)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/117

flashcard set

Earn XP

Description and Tags

Quiz-style practice matching the complete study reading. Terms = questions; definitions = answers with reasoning. Search [Chunk 1] through [Chunk 7] to study each chunk. Chunk 1 is starred. Chunk 7 allocator cards are labeled backup review. Original practice, not actual quiz questions.

Last updated 6:41 PM on 10/8/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

118 Terms

1
New cards

[Chunk 1] Given int x = 12; int *p = &x;, what are x, p, and *p?

x is 12; p stores the address of x; *p is 12. The pointer value is an address, while dereferencing retrieves the pointed-to value.

2
New cards

[Chunk 1] If p stores the address of x, what does &p mean?

The address of the pointer variable p itself, not the address stored inside p.

3
New cards

[Chunk 1] Given int x = 4; int *p = &x; *p = 9;, what is x?

  1. 9. Assigning through *p writes into x.


4
New cards

[Chunk 1] How do p = &y and *p = 7 differ?

p = &y changes which object p points to. *p = 7 changes the value of the object p currently points to.

5
New cards

[Chunk 1] Given int x = 8; int *p = &x; int **pp = &p;, what is **pp?

  1. 8. First follow pp to p, then follow p to x.


6
New cards

[Chunk 1] Given int x; int *p = &x; int **pp = &p;, what are the types of pp, *pp, and **pp?

pp: int**; *pp: int*; **pp: int. Each dereference removes one pointer level.

7
New cards

[Chunk 1] In int *p, q;, which variables are pointers?

Only p is a pointer. q is an int. To declare both as pointers, write int *p, *q;.

8
New cards

[Chunk 1] A short* p points to 0x3000. Where is p + 4 if sizeof(short) is 2?

0x3008. Four elements × 2 bytes = 8 bytes.

9
New cards

[Chunk 1] An int* p points to 0x1000. Where is p + 3 if sizeof(int) is 4?

0x100C. Three elements × 4 bytes = 12 bytes.

10
New cards

[Chunk 1] For int a[10]; int *p = &a[2]; int *q = &a[7];, what is q - p?

5 elements, not 20 bytes. Pointer subtraction within the same array counts elements.

11
New cards

[Chunk 1] What does p + n use as its scale factor?

sizeof(*p), the size of the pointed-to type. Byte displacement = n × sizeof(*p).

12
New cards

[Chunk 1] Is dereferencing int *p = NULL valid? Explain.

No. NULL does not point to a valid object. Comparing p with NULL is valid; dereferencing it is not.

13
New cards

[Chunk 1] Why is dereferencing an uninitialized pointer unsafe?

Its stored address has not been established as the address of a live valid object.

14
New cards

[Chunk 1] C passes arguments by value. How can a pointer parameter change the caller’s variable?

The function receives a copy of the address. Dereferencing that copied address reaches the caller’s original object.

15
New cards

[Chunk 1] Trace: void f(int x, int *p){x=50; *p=60;} int a=1,b=2; f(a,&b); What are a and b?

a = 1; b = 60. x was a copied integer; *p accesses b.

16
New cards

[Chunk 1] A function assigns its pointer parameter p = &other. Does this reassign the caller’s pointer variable?

No. The pointer parameter is a copy. Reassigning it changes only that local copy.

17
New cards

[Chunk 1] Write an expression that reads x through int **pp when pp points to a pointer to x.

**pp

18
New cards

[Chunk 1] Explain what to draw for int x=3; int *p=&x; int **pp=&p;.

Three boxes: x holds 3; p holds x’s address and points to x; pp holds p’s address and points to p.

19
New cards

[Chunk 2] Rewrite a[3] using pointer arithmetic and dereferencing.

*(a + 3)

20
New cards

[Chunk 2] What does an array name usually decay to in an expression?

A pointer to its first element. The array itself is still an array object, not a pointer variable.

21
New cards

[Chunk 2] Given int a[6]; int *p=a;, what are sizeof(a) and sizeof(p) on course Linux (int 4, pointer 8)?

sizeof(a) = 24; sizeof(p) = 8. One measures the entire array; the other measures a pointer.

22
New cards

[Chunk 2] Why cannot sizeof(a) recover the original length inside void f(int a[])?

The array parameter is adjusted to a pointer, so sizeof(a) measures the pointer. Pass the length separately.

23
New cards

[Chunk 2] For int a[4][6] with 4-byte ints, find sizeof(a), sizeof(a[0]), and sizeof(a[0][0]).

96 bytes, 24 bytes, and 4 bytes respectively.

24
New cards

[Chunk 2] In row-major storage, what is the flat element index of row r, column c in C columns?

r * C + c. Multiply by sizeof(element) for the byte offset.

25
New cards

[Chunk 2] For int a[4][6], what is the byte offset of a[2][3] if ints are 4 bytes?

60 bytes: (2*6 + 3)*4.

26
New cards

[Chunk 2] For int a[3][4], how far do a + 1 and a[0] + 1 advance (int 4 bytes)?

a + 1 advances one 16-byte row. a[0] + 1 advances one 4-byte integer.

27
New cards

[Chunk 2] What type does int a[3][4] decay to?

A pointer to a row of 4 ints: int (*)[4]. It is not int**.

28
New cards

[Chunk 2] Why does a function indexing int a[][4] need the column dimension?

The column count determines the row stride, so the compiler can calculate a[r][c].

29
New cards

[Chunk 2] Can int a[7][5] be passed to a parameter declared int x[6][5]? Explain the dimensions.

Yes, in this ordinary array parameter form the first dimension adjusts away. The 5-column row type matches. Actual loops must still stay within the real array bounds.

30
New cards

[Chunk 2] Compare a rectangular int a[R][C] with an int** whose rows are separately allocated.

The rectangle stores all integers contiguously. int** leads to a table of row pointers; each row is a separate allocation and rows need not be adjacent.

31
New cards

[Chunk 2] How does board[r][c] access a separately allocated-row int**?

Read row pointer board[r], follow it, then index its c-th integer.

32
New cards

[Chunk 2] Why is an int** not interchangeable with a pointer to a contiguous rectangular row?

int** expects pointer entries in a table; a rectangular row pointer expects inline integer rows. Their storage interpretation differs.

33
New cards

[Chunk 2] A pointer table and 4 rows are separately malloc’d. How many frees, and in what order?

5 frees: free each row first, then free the pointer table.

34
New cards

[Chunk 2] A 4×6 integer board uses one flat malloc allocation. How many frees release it?

One free for that one allocation. Free counts depend on allocation structure, not the number of elements.

35
New cards

[Chunk 2] For ./tool 8 9 10, what is argc and what string is argv[2]?

argc = 4; argv[2] is "9". argv[0] is the program-name string.

36
New cards

[Chunk 2] For ./tool red blue green, what does *(argv + argc - 2) refer to?

argv[2], the string "blue". argc=4, so argc-2=2.

37
New cards

[Chunk 3] A local int *p points to malloc storage. Identify where p and *p’s object live.

p is typically on the stack; its target allocation is on the heap. Pointer storage and target storage are separate.

38
New cards

[Chunk 3] Classify int total=5; declared globally in the course memory model.

.data: nonzero-initialized global/static storage.

39
New cards

[Chunk 3] Classify an uninitialized global int counter; and state its initial value.

.bss; it starts at zero.

40
New cards

[Chunk 3] Classify a local static int saved=9; and explain its lifetime.

.data; the object persists for the program’s lifetime even though the name has local scope.

41
New cards

[Chunk 3] Where do machine instructions and string literals typically live?

Instructions: .text. String literals: .rodata.

42
New cards

[Chunk 3] Why is returning &x from a function with ordinary local int x invalid to use afterward?

x’s lifetime ends when the function returns. Returning its address does not extend its lifetime.

43
New cards

[Chunk 3] Distinguish scope from lifetime.

Scope is where a name can be used. Lifetime is how long the object exists. A local static has local scope but program-long lifetime.

44
New cards

[Chunk 3] Distinguish a memory leak from a dangling pointer.

Leak: allocated memory is not released and becomes unreachable. Dangling pointer: refers to an object whose lifetime ended, such as a freed allocation.

45
New cards

[Chunk 3] After free(p); p=NULL;, are other pointers to the old allocation safe?

No. Setting p to NULL changes only p; other aliases still dangle.

46
New cards

[Chunk 3] State course 64-bit Linux sizes of char, short, int, long, float, double, and pointer.

1, 2, 4, 8, 4, 8, and 8 bytes respectively, unless a problem specifies otherwise.

47
New cards

[Chunk 3] Do int* and char* have different sizes or different arithmetic scale factors?

Usually the same pointer size (8 bytes here), but different arithmetic scales: 4 bytes for int versus 1 for char.

48
New cards

[Chunk 3] Store 0xA1B2C3D4 at 0x6000 in little endian. Give bytes at increasing addresses.

0x6000:D4; 0x6001:C3; 0x6002:B2; 0x6003:A1. Least-significant byte is at the lowest address.

49
New cards

[Chunk 3] Store 0xA1B2C3D4 at 0x6000 in big endian. Give bytes at increasing addresses.

0x6000:A1; 0x6001:B2; 0x6002:C3; 0x6003:D4. Most-significant byte is at the lowest address.

50
New cards

[Chunk 3] Which byte address does a pointer to a multi-byte object identify?

The object’s lowest byte address, regardless of endian order.

51
New cards

[Chunk 3] Compare local char *p="cat"; with local char a[]="cat";. Which characters can be modified?

p points to a read-only string literal; modifying it is invalid. a is a writable local array containing c,a,t, and the zero terminator.

52
New cards

[Chunk 3] A global char a[80]="cat"; is used as a pointer. Which region does it address?

.data: the initialized global array itself. It is not the read-only literal target arrangement of char *p="cat";.

53
New cards

[Chunk 4] Rewrite p->score using dereference and member access.

(*p).score. The parentheses are needed because member access binds more tightly than unary *.

54
New cards

[Chunk 4] When do you use object.member versus pointer->member?

Use . for a struct/union object; use -> for a pointer to one.

55
New cards

[Chunk 4] Does typedef allocate storage or change a type’s memory layout?

No. It introduces a type alias. Declaring objects of that type allocates storage.

56
New cards

[Chunk 4] Layout char c; int i; short s; with sizes/alignments 1,4,2. Give offsets and total size.

c:0; padding:1-3; i:4-7; s:8-9; trailing padding:10-11. Total size 12, aligned to 4.

57
New cards

[Chunk 4] Layout short s; char c; int i; with sizes/alignments 2,1,4. Give offsets and size.

s:0-1; c:2; padding:3; i:4-7. Total size 8.

58
New cards

[Chunk 4] Explain the two alignment checks in a struct layout problem.

Align each member’s start to its required boundary; then round the entire struct size to its overall alignment so array elements remain aligned.

59
New cards

[Chunk 4] Why can reordering struct members change sizeof(struct)?

Different orders require different internal/trailing padding while preserving member alignment.

60
New cards

[Chunk 4] A struct has size 12. How many bytes does a pointer to it advance with p + 2?

24 bytes, including each struct’s padding.

61
New cards

[Chunk 4] Compare struct and union storage for int i and char c.

Struct members occupy distinct storage plus possible padding. Union members overlap at the same start; space is enough for the largest member with alignment rounding.

62
New cards

[Chunk 4] On the course model, why can reading a character view of a stored integer expose endianness?

It examines a byte of the integer’s memory representation. The first byte differs in little versus big endian.

63
New cards

[Chunk 4] Given int x=42; void *p=&x;, write an expression to read x via p.

*(int*)p. A void pointer needs an appropriate object-pointer type before dereferencing.

64
New cards

[Chunk 4] Does casting an address to another pointer type convert the underlying object?

No. A cast changes how the address is interpreted; it does not transform the stored object.

65
New cards

[Chunk 5] In P2, how do you locate row r, column c in the flat Cell array?

Index r*number_of_columns+c. Values are stored in row-major order.

66
New cards

[Chunk 5] What memory must be freed after transferring the int** P2 board into the flat Cell array?

Every separately allocated integer row, then the row-pointer table. The flat Cell allocation remains in use.

67
New cards

[Chunk 5] Which masks encode black, circle, and number in a P2 Cell byte?

Black:0x80; circle:0x40; number:0x1F. Number uses the low five bits.

68
New cards

[Chunk 5] Write an expression to extract a cell’s puzzle number without flags.

cell & 0x1F

69
New cards

[Chunk 5] Write a condition that tests whether cell is black.

(cell & 0x80) != 0

70
New cards

[Chunk 5] Write an update that sets the black flag while preserving the number.

cell |= 0x80; OR sets the bit without clearing the other bits.

71
New cards

[Chunk 5] Why is XOR with BLACK not a reliable operation for “mark black”?

XOR toggles the flag. An already-black cell would become unmarked instead of staying black.

72
New cards

[Chunk 5] Decode Cell 0x4D: puzzle number and flags.

Number13; circle set; black unset. 0x4D & 0x1F = 13.

73
New cards

[Chunk 5] Decode Cell 0x89: puzzle number and flags.

Number9; black set; circle unset. The full byte value is not the puzzle number.

74
New cards

[Chunk 5] State the three Hitori solution constraints.

No repeated surviving number in a row/column; no orthogonally adjacent black cells; all surviving cells remain orthogonally connected.

75
New cards

[Chunk 5] A cell is newly marked black. Which deduction applies to its neighbors, and why?

Circle deduction: orthogonal neighbors must survive because adjacent black cells are forbidden.

76
New cards

[Chunk 5] A cell is circled. What happens to other copies of its number in its row or column?

Blackout deduction: they must be blacked out to preserve uniqueness.

77
New cards

[Chunk 5] For 4,7,4 in adjacent row positions, what sandwich deduction follows? Explain.

Circle the middle 7. At least one 4 must be black; a black center would be adjacent to that black 4.

78
New cards

[Chunk 5] A row has an adjacent pair of 6s and another 6 elsewhere. What doublet deduction follows?

Black out the other 6. Exactly one adjacent 6 must survive: both black violates adjacency and both surviving violates uniqueness.

79
New cards

[Chunk 5] What is the reasoning behind a gate deduction?

If blacking out a cell disconnects surviving cells, it must remain available and be circled. This was optional extra-credit P2 logic.

80
New cards

[Chunk 5] Why do blackout() and circle() need already-marked early-return cases?

They can call each other recursively. Avoiding repeat work makes deduction chains terminate and prevents cycles; detect contradictory marks separately.

81
New cards

[Chunk 5] How many orthogonal neighbors does a board corner have, and what check prevents invalid access?

Two (for an ordinary board with at least two rows/columns). Check row/column bounds before visiting neighbors.

82
New cards

[Chunk 6] Which allocation function gives uninitialized contents, and which zero-initializes bytes?

malloc gives uninitialized storage; calloc zero-initializes count*size bytes.

83
New cards

[Chunk 6] Write the byte count needed to malloc an array of 6 ints.

6 * sizeof(int), not 6 alone.

84
New cards

[Chunk 6] What must you check before dereferencing a newly allocated pointer?

Check that allocation succeeded: the returned pointer is not NULL.

85
New cards

[Chunk 6] Why can realloc change the pointer address?

It may move the allocation to obtain enough space. Use its returned pointer after successful resizing.

86
New cards

[Chunk 6] If realloc fails, what happens to the original allocation?

It remains available. Preserve the original pointer until success; overwriting the only pointer with NULL loses access.

87
New cards

[Chunk 6] Does free(p) automatically set p and all aliases to NULL?

No. It releases the allocation; pointer variables retain their old values unless changed. Accessing the old allocation is invalid.

88
New cards

[Chunk 6] What are strlen("mouse") and the minimum bytes needed to store that string?

Length5; capacity6 bytes including the zero terminator.

89
New cards

[Chunk 6] What happens conceptually if a supposed C string lacks a terminator in accessible storage?

String functions can keep reading beyond the intended object, causing invalid access.

90
New cards

[Chunk 6] Does a == b compare C string contents when a and b are char pointers?

No. It compares addresses. Use strcmp for contents.

91
New cards

[Chunk 6] Interpret strcmp(a,b): zero, negative, and positive.

Zero: equal contents; negative: a sorts before b; positive: a sorts after b. Nonzero need not be exactly -1 or 1.

92
New cards

[Chunk 6] Joining strings of lengths 4 and 7 requires how much destination capacity?

At least12 bytes: 4+7+1 for the final terminator.

93
New cards

[Chunk 6] How do strcpy and strcat differ?

strcpy copies a string including its terminator. strcat appends to an existing terminated string. Both require sufficient writable destination space.

94
New cards

[Chunk 6] What does fopen return on failure, and what does mode "w" do?

Failure returns NULL. "w" opens for writing and creates or truncates the file.

95
New cards

[Chunk 6] What does fgets do about the terminator and a newline?

On success it zero-terminates the buffer; it may retain a read newline. One call may read only part of a long line.

96
New cards

[Chunk 6] Match stdin, stdout, stderr to uses and descriptor numbers.

stdin:input/0; stdout:normal output/1; stderr:error output/2.

97
New cards

[Chunk 6] Choose a GDB operation: stop at a specific line before investigating a bug.

Set a breakpoint at that line, then run or continue to reach it.

98
New cards

[Chunk 6] Choose a GDB operation: follow execution inside a called function.

Step into (step). Step over (next) instead executes the call without following its internal lines.

99
New cards

[Chunk 6] Choose a GDB operation: resume from a pause until the next breakpoint.

Continue.

100
New cards

[Chunk 6] Choose a GDB operation: inspect *p at the paused location.

Print the expression *p.