CS50 Debugging Tools
- 77Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What does debug50 allow a programmer to do?
77 questions
- 1
What does
debug50allow a programmer to do?Model answer
debug50allows a programmer to pause execution at breakpoints, step through code, and inspect variables as the program runs.Explanation
Its side panel supports step-by-step debugging and makes the program's changing state visible.
- 2
What is the purpose of
check50?Model answer
check50automatically runs tests to check whether a program is correct.Explanation
It automates many of the correctness checks that students or teaching fellows might otherwise perform manually.
- 3
What does
printfhelp a programmer investigate during debugging?Model answer
printfcan display variable values, messages, or execution markers to show what the program is doing and where a bug occurs.Explanation
It is especially useful for checking whether execution reached a particular point or observing values inside loops.
- 4
Why can
printfbe more convenient than repeatedly stepping through a loop with a debugger?Model answer
A strategically placed
printfcan show information on many loop iterations without requiring the programmer to click through each iteration manually.Explanation
This is useful for nested or lengthy loops where repeated debugger steps would be tedious.
- 5
What does
style50check?Model answer
style50checks the formatting and style of code, such as spacing, to improve readability.Explanation
It identifies places where spaces should be added or removed.
- 6
When should a programmer use
help50?Model answer
A programmer should use
help50when an unfamiliar or cryptic error message appears while compiling or running a command.Explanation
It analyzes the command's output and provides more understandable, teaching-fellow-style guidance.
- 7
What is rubber duck debugging?
Model answer
Rubber duck debugging is the practice of explaining code aloud, step by step, to an object such as a rubber duck in order to identify a logical mistake.
Explanation
Hearing oneself describe the code can reveal an incorrect assumption or produce an unexpected insight.
- 8
Why can a program contain a memory bug even if it appears to work correctly?
Model answer
C may allow an invalid memory operation to succeed by chance, so the program can produce the expected output even though its behavior is unsafe.
Explanation
Different computers or memory layouts may cause the same bug to crash or behave differently.
- 9
What is Valgrind used to detect?
Model answer
Valgrind detects memory errors such as invalid reads, invalid writes, and memory leaks.
Explanation
It can expose problems that ordinary compilation and sample testing do not reveal.
- 10
What does
mallocdo, and what must a program do after using the allocated memory?Model answer
mallocrequests a block of memory from the heap; the program should release that memory withfreewhen it is no longer needed.Explanation
Failing to call
freecan cause a memory leak. - 11
Why can failing to free allocated memory cause a computer or program to slow down, freeze, or crash?
Model answer
Unfreed allocations remain marked as in use, so the program continually consumes available memory until insufficient memory remains.
Explanation
The operating system cannot reuse leaked blocks until they are released.
- 12
What does an 'invalid read of size 4' generally indicate when an
intis involved?Model answer
It indicates that the program attempted to read four bytes from a memory location it was not allowed to access, often because it went outside an array's bounds.
Explanation
On the example system, an
intoccupies four bytes. - 13
Why is accessing element 10 invalid when memory was allocated for 10 integers?
Model answer
The valid indexes are 0 through 9; index 10 is one position beyond the allocated array.
Explanation
Array indexing begins at zero, so an array of length 10 has ten valid positions.
- 14
Why might an out-of-bounds access not immediately cause a crash in C?
Model answer
The program may have received extra adjacent memory or may access memory that is not currently protected, allowing the invalid operation to appear to work.
Explanation
This accidental success is not guaranteed across systems or executions.
- 15
What does Valgrind report when allocated memory is not released?
Model answer
It reports that a number of bytes in one or more blocks are definitely lost, indicating a memory leak.
Explanation
The report usually identifies the allocation site so the programmer can add the appropriate
free. - 16
When should dynamically allocated memory be freed?
Model answer
It should be freed when the program is finished using it, commonly near the end of the relevant computation or program.
Explanation
Freeing memory before its final use creates an invalid access.
- 17
Why is using memory after calling
freeincorrect?Model answer
freereturns the memory to the operating system's available pool, so accessing it afterward uses memory that the program no longer owns.Explanation
The released block may be reused or contain invalid data.
- 18
What do the heap and the stack generally store?
Model answer
The heap stores dynamically allocated memory requested with functions such as
malloc, while the stack stores local variables and function-call frames.Explanation
Each function call receives its own region of stack memory.
- 19
What happens conceptually when memory is freed?
Model answer
The memory is marked as available and returned to the pool of free memory for later allocations; its physical bytes do not disappear.
Explanation
The operating system tracks which regions are in use and which are available.
- 20
What is stored in a program's text segment when the program is running?
Model answer
The text segment stores the program's machine-code instructions loaded into RAM.
Explanation
The executable is copied from long-term storage into faster memory so the processor can run it.
- 21
Why are programs loaded from storage into RAM before execution?
Model answer
RAM is generally faster to access than hard drives or solid-state storage, so loading code into RAM improves execution performance.
Explanation
RAM is electronic and has no moving mechanical parts.
- 22
What is a string in C at a low level?
Model answer
A string is a sequence or array of characters and is represented by a
char *, a pointer to a character.Explanation
The pointer refers to the beginning of the character data in memory.
- 23
Why did passing ordinary variables to the original
swapfunction fail to swap the variables inmain?Model answer
The function received copies of the values, so it swapped its local copies rather than the original variables in
main.Explanation
Function parameters are stored in a separate stack frame.
- 24
How do pointers allow a function to swap variables belonging to its caller?
Model answer
The caller passes the variables' addresses, and the function dereferences those pointers to modify the original memory locations.
Explanation
The function changes the data at the addresses rather than changing copied values.
- 25
What does dereferencing a pointer mean?
Model answer
Dereferencing means accessing the value stored at the memory address contained in the pointer.
Explanation
In C, the
*operator is used to follow a pointer to its referent. - 26
Why is dereferencing an uninitialized pointer unsafe?
Model answer
An uninitialized pointer contains an indeterminate or garbage address, so dereferencing it may access invalid memory and cause unpredictable behavior.
Explanation
A pointer should be assigned a valid address or
NULLbefore it is dereferenced. - 27
What is the difference between a null pointer and an uninitialized pointer?
Model answer
A null pointer is deliberately set to
NULL, meaning it points to no valid object, whereas an uninitialized pointer contains an unknown garbage value.Explanation
Neither should be dereferenced, but a null pointer can safely be tested for explicitly.
- 28
What is the purpose of a
structin C?Model answer
A
structgroups related values, possibly of different types, into one custom data structure.Explanation
It lets programmers represent a real-world concept, such as a student, as a single code object.
- 29
How can a student be represented using a structure?
Model answer
A student structure can contain fields such as a
char *for the student's name and achar *for the student's dorm.Explanation
The structure encapsulates related student information in one data type.
- 30
What is the purpose of
typedefwhen defining a structure?Model answer
typedefcreates a convenient name for a type, allowing a structure definition to be referred to by a shorter custom name such asstudentornode.Explanation
It makes declarations easier to read and use.
- 31
How are fields accessed in an ordinary structure variable?
Model answer
The dot operator is used, such as
student.nameorstudent.dorm.Explanation
The dot operator accesses a field directly through a structure object.
- 32
Why is encapsulating related values in a structure preferable to using separate parallel arrays?
Model answer
A structure keeps related data together, reducing the risk of mismatching values such as a name from one person with a dorm from another.
Explanation
It provides a clearer abstraction for the entity being represented.
- 33
What is a major limitation of an ordinary C array regarding data types?
Model answer
All elements of an ordinary array must have the same data type.
Explanation
For example, an array must contain all integers, all characters, or all structures of the same type.
- 34
Why can an ordinary C array not simply grow when another element is needed?
Model answer
Its allocated size is fixed, so adding capacity requires allocating a larger region and copying the existing elements into it.
Explanation
The original array has no extra capacity unless it was deliberately overallocated.
- 35
What is the time complexity of resizing an array by copying its n existing elements?
Model answer
The resizing operation takes O(n) time because each of the n existing elements must be copied.
Explanation
The additional constant work is ignored in asymptotic notation.
- 36
What does
reallocdo?Model answer
reallocchanges the size of a previously allocated memory block and returns a pointer to the resized block.Explanation
It may extend the block in place or allocate a new block, copy the data, and return the new address.
- 37
Why might
reallocreturn a different address from the original allocation?Model answer
If adjacent memory is unavailable,
reallocmust create a larger block elsewhere, copy the old contents, and return the new block's address.Explanation
The original block may no longer be large enough to expand in place.
- 38
Why should the return value of
reallocusually be stored in a temporary pointer first?Model answer
If
reallocfails and returnsNULL, assigning directly to the original pointer can lose the only reference to the original block.Explanation
A temporary pointer permits failure checking before replacing the original pointer.
- 39
What is the trade-off between allocating extra unused array capacity and resizing one element at a time?
Model answer
Extra capacity uses more memory but reduces the number of costly reallocations, while exact resizing saves memory but may require more copying.
Explanation
The better choice depends on the available resources and the expected input size.
- 40
What do the variables
sizeandcapacityrepresent in a dynamically growing array?Model answer
sizeis the number of elements currently stored, whilecapacityis the number of elements the allocated memory can hold.Explanation
When
sizeequalscapacity, additional memory is needed before inserting another element. - 41
Why might a program use
Control-Dwhen reading an unknown number of integers?Model answer
Control-Dsignals end-of-file, allowing the user to indicate that input is finished without entering another integer.Explanation
A function designed to read only integers cannot use a letter such as
qas a quit command. - 42
Why can
getIntnot directly support enteringqto end numeric input?Model answer
getIntexpects an integer and immediately rejects nonnumeric input rather than returning a string command such asq.Explanation
Supporting
qwould require reading a string first and then converting valid numeric strings. - 43
What is a sentinel value?
Model answer
A sentinel value is a special reserved value used to signal a condition such as the end of input.
Explanation
A program can test for the reserved value and stop processing when it appears.
- 44
Why must a dynamically allocated array eventually be freed?
Model answer
The allocated block remains in use until
freeis called, so failing to release it causes a memory leak.Explanation
Valgrind can report the leaked bytes and allocation location.
- 45
What is a linked list?
Model answer
A linked list is a sequence of nodes in which each node stores data and a pointer to the next node.
Explanation
The nodes need not occupy adjacent memory locations because pointers connect them.
- 46
What fields are typically included in a singly linked-list node storing integers?
Model answer
The node contains an integer value and a pointer to another node, often represented as
int numberandstruct node *next.Explanation
The pointer stores the address of the next node in the list.
- 47
Why must a self-referential structure use the structure tag before its complete definition?
Model answer
The structure tag declares the type name early enough for a pointer field to refer to another structure of the same type.
Explanation
A pointer to the type has a known size even while the structure's full contents are still being defined.
- 48
What does a
NULLnext pointer signify in a singly linked list?Model answer
It signifies that the node is the last node and that no subsequent node exists.
Explanation
Traversal stops when the next pointer equals
NULL. - 49
What pointer must be retained to access every element of a singly linked list?
Model answer
The program must retain a pointer to the first node, commonly called the head or
firstpointer.Explanation
Every later node is reached by following next pointers from the first node.
- 50
How is a new node inserted at the beginning of a linked list?
Model answer
First set the new node's next pointer to the current first node, then update the first pointer to the new node.
Explanation
Updating the new node before the first pointer prevents losing access to the existing list.
- 51
Why must a new middle node point to its successor before its predecessor points to it?
Model answer
The new node must first preserve the successor address; otherwise, changing the predecessor's pointer could disconnect the remaining list.
Explanation
The safe order is to connect the new node to the old successor and then connect the predecessor to the new node.
- 52
What is the worst-case time complexity of inserting into a sorted singly linked list?
Model answer
The worst-case time complexity is O(n) because the program may need to traverse all n nodes to find the insertion position.
Explanation
A sorted insertion may place the new node at the end of the list.
- 53
What is the time complexity of inserting a node at the beginning of an unsorted linked list?
Model answer
Insertion at the beginning takes O(1) time.
Explanation
It requires only a fixed number of pointer updates, regardless of list length.
- 54
What is the main advantage of a linked list over a fixed-size array?
Model answer
A linked list can grow or shrink dynamically without requiring all existing elements to be copied into a larger contiguous block.
Explanation
Each new node can be allocated separately as needed.
- 55
What is a disadvantage of storing linked-list nodes in separate allocations?
Model answer
The nodes are not contiguous, so the list does not provide constant-time random access by index.
Explanation
The program must follow pointers sequentially from the first node.
- 56
What is a doubly linked list, and what trade-off does it introduce?
Model answer
A doubly linked list stores both next and previous pointers, allowing traversal in either direction but requiring additional memory and pointer maintenance.
Explanation
The previous pointer makes it possible to move backward when a traversal goes too far.
- 57
How can keeping a pointer to the last node improve linked-list insertion?
Model answer
A stored last pointer allows insertion at the end in constant time rather than requiring a traversal from the first node.
Explanation
The new node can be linked directly after the current last node.
- 58
What does the arrow operator,
->, mean when used with a structure pointer?Model answer
It follows the pointer to a structure and accesses the named field in that structure.
Explanation
The expression
p->fieldis shorthand for(*p).field. - 59
Why is the arrow operator used instead of the dot operator when
nis a pointer to a node?Model answer
The dot operator requires a structure object, whereas
nis an address; the arrow operator first follows the address and then accesses the field.Explanation
For a node pointer
n,n->numberaccesses the node'snumberfield. - 60
What should a program do if
mallocreturnsNULLwhile allocating a node?Model answer
It should detect the failure and terminate or handle the error gracefully rather than dereferencing the null pointer.
Explanation
A
NULLresult means that the requested memory could not be allocated. - 61
What is a hash function?
Model answer
A hash function maps an input, such as a name, to an index or bucket used to organize data.
Explanation
For example, a function might map the first letter of a name to an index from 0 through 25.
- 62
How can an array of linked lists form a hash table?
Model answer
The array provides buckets for hash values, and each bucket stores a linked list containing all items that map to that bucket.
Explanation
This combines fast bucket access from arrays with dynamic storage from linked lists.
- 63
Why can hashing improve name lookup compared with searching one long list?
Model answer
Hashing narrows the search to the linked list in one bucket rather than requiring a scan of every stored name.
Explanation
If names are distributed evenly, each bucket contains approximately n/26 names when using the first letter over the English alphabet.
- 64
What is the worst-case time complexity of lookup in a hash table using linked-list buckets?
Model answer
The worst-case time complexity is O(n) if all items map to the same bucket.
Explanation
In that case, the hash table degenerates into one long linked list.
- 65
What is the goal of a well-designed hash function?
Model answer
Its goal is to distribute inputs as evenly as possible among the available buckets.
Explanation
Even distribution keeps the bucket lists short and produces more consistent lookup times.
- 66
Why might using only the first letter of a name be a poor hash function?
Model answer
Letters are not equally common in names, so some buckets may become much longer than others and lookup performance may be uneven.
Explanation
A hash function could use more information, such as multiple letters, an identification number, or another relatively uniform attribute.
- 67
How can a hash table have a practical advantage even when its worst-case complexity remains O(n)?
Model answer
If the hash function distributes data evenly, the average search examines a much shorter bucket list, reducing actual running time despite the same worst-case classification.
Explanation
Ignoring constant factors in asymptotic notation does not mean those factors have no practical effect.
- 68
What is a binary search tree?
Model answer
A binary search tree is a tree in which each node has up to a left and right child, with smaller values on the left and larger values on the right.
Explanation
This ordering allows searches to eliminate one subtree at each decision.
- 69
How can a binary search tree be represented in C?
Model answer
Each node can contain a value and two pointers, one to a left child and one to a right child.
Explanation
A null child pointer indicates that the corresponding subtree does not exist.
- 70
Why does searching a balanced binary search tree take O(logn) time?
Model answer
Each comparison chooses either the left or right subtree, roughly halving the remaining search space at every level.
Explanation
A balanced tree with n nodes has height proportional to logn.
- 71
What is the root of a tree?
Model answer
The root is the topmost node and the starting point for traversing the tree.
Explanation
All other nodes are reached by following child pointers from the root.
- 72
What is a trie?
Model answer
A trie is a tree whose nodes contain arrays used to represent successive characters of stored strings.
Explanation
The name is associated with retrieval, and the structure supports efficient word lookup.
- 73
How is a word stored in a trie?
Model answer
Each character selects an entry in the current node's array, leading to the node for the next character in the word.
Explanation
For a word such as
BILLY, the traversal follows entries for B, I, L, L, and Y. - 74
Why can a trie reuse memory when storing words with shared prefixes?
Model answer
Words with the same initial characters follow the same nodes until their characters diverge.
Explanation
For example, words beginning with
Bcan share the root'sBbranch. - 75
What is the lookup time for a word of fixed length in a trie?
Model answer
Lookup takes time proportional to the word's length, which is effectively O(1) when the maximum word length is treated as bounded independently of the number of stored words.
Explanation
The number of stored words does not determine how many character links must be followed.
- 76
What is the main cost of achieving very fast lookup with a trie?
Model answer
A trie can consume substantial memory because each node may contain a large array of character pointers.
Explanation
The design trades additional space for faster retrieval.
- 77
How could a trie distinguish between a word such as
BOBand a longer word such asBOBBY?Model answer
Each node needs an indication of whether a complete word ends there, in addition to pointers for continuing characters.
Explanation
The structure must distinguish a valid word endpoint from a prefix that continues into another word.

Ask anything
We've read this whole study set, so questions about it get the best answers.