1. Home
  2. Explore
  3. Computer Science
  4. CS50 Debugging Tools

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

CS50 Debugging Tools

  • 78Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Debugging Tools
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What does debug50 allow a programmer to do?

Tap to flipClick to flipspace

Step through code line by line, inspect variables, and use breakpoints to investigate program behavior.

Or swipe the card

78 cards

  1. What does debug50 allow a programmer to do?

    Step through code line by line, inspect variables, and use breakpoints to investigate program behavior.

  2. What is the purpose of check50?

    To automatically run tests that check whether a program is correct.

  3. When is printf useful for debugging?

    When you want to display variable values, trace execution, or determine whether code reached a particular point.

  4. Why might printf be preferable to repeatedly stepping through a debugger?

    It can efficiently trace repeated execution, such as iterations of a nested loop, without manually advancing through every step.

  5. What does style50 check?

    The formatting and style of C code, such as spacing and indentation.

  6. When should a programmer use help50?

    When a command or compilation produces an error message that the programmer does not understand.

  7. What is Valgrind designed to detect?

    Memory errors, including invalid memory accesses and memory leaks.

  8. What does an 'invalid read of size 4' reported by Valgrind usually indicate in a program using integers?

    The program attempted to read 444 bytes, typically one integer, from memory it should not access.

  9. How can Valgrind be combined with help50?

    Prefix the command with both tools, such as help50 valgrind ./program, to make Valgrind's output easier to interpret.

  10. What is rubber duck debugging?

    Explaining code aloud, step by step, to an inanimate object or another listener to expose logical mistakes.

  11. What happens if memory allocated with malloc is never released with free?

    The program leaks memory, potentially causing the computer or program to slow down, freeze, or crash.

  12. What is a memory leak?

    Allocated memory that a program no longer uses but has failed to release.

  13. What does malloc return?

    The address of an allocated block of memory, represented by a pointer.

  14. How can a program allocate space for 101010 integers without hard-coding the integer size?

    Use malloc(10 * sizeof(int)).

  15. Why should dynamically allocated memory generally be freed near the end of its use?

    It should be released only after the program is finished using it; freeing it earlier and then accessing it causes invalid memory use.

  16. What does realloc do?

    It resizes an allocated memory block and returns the address of the resized block.

  17. What may happen if realloc cannot enlarge a block in place?

    It may allocate a larger block elsewhere, copy the existing data, and return the new block's address.

  18. What should a program do if malloc or realloc returns NULL?

    Treat allocation as failed and terminate or handle the error safely.

  19. Why should the return value of realloc often be stored in a temporary pointer first?

    If realloc fails and returns NULL, assigning directly to the original pointer could lose the original block's address.

  20. What does free conceptually do to allocated memory?

    It marks the memory as available for reuse by the operating system or other allocations.

  21. What is the difference between a memory leak and an invalid memory access?

    A leak fails to release allocated memory; an invalid access reads or writes memory outside the program's valid ownership.

  22. What is the text segment of a running program's memory?

    The region containing the program's machine-code instructions.

  23. What is stored in the heap?

    Dynamically allocated memory requested with functions such as malloc and realloc.

  24. What is stored in the stack?

    Function-call frames and local variables.

  25. Why can a stack overflow or heap overflow occur?

    The stack and heap share a finite memory space and can grow into each other when too much memory is used.

  26. What is a string in C underneath the abstraction provided by the CS50 library?

    A sequence of characters, represented by a char *.

  27. Why can accessing an array beyond its declared boundary appear to work in C?

    C often does not automatically perform bounds checking, and the program may accidentally access usable-looking memory.

  28. Why are out-of-bounds bugs especially difficult to diagnose?

    They may work on one computer or input but fail on another because memory layouts differ.

  29. What happens when a function receives ordinary values as arguments in C?

    The function receives copies of those values.

  30. Why did a basic swap function fail to swap the caller's variables?

    It swapped local copies of the values rather than the original variables.

  31. How can a function modify variables belonging to its caller?

    Pass pointers to those variables and dereference the pointers inside the function.

  32. What does the dereference operator * do to a pointer?

    It accesses the value stored at the memory address held by the pointer.

  33. What is a garbage value?

    An unpredictable value left in memory from previous activity and not intentionally initialized by the current program.

  34. Why is dereferencing an uninitialized pointer unsafe?

    It may contain an invalid or unpredictable address, so dereferencing it can access arbitrary memory or crash the program.

  35. What is a structure (struct) in C?

    A custom data type that groups related fields, potentially of different types, into one object.

  36. How can a student structure represent a student?

    It can group fields such as a char * name and a char * dorm into one custom type.

  37. What does typedef accomplish when defining a structure?

    It creates a convenient name for the structure type, such as student or node.

  38. How are fields of a structure accessed when you have a structure object?

    Use the dot operator, such as student.name.

  39. How does encapsulation apply to a structure such as student?

    It combines related data into one abstraction that can be treated as a single meaningful entity.

  40. What is a major limitation of a C array?

    Its size is fixed when allocated and its elements normally must have the same data type.

  41. What is the time complexity of resizing an array by allocating a new array and copying nnn existing elements?

    O(n)O(n)O(n).

  42. What is the purpose of maintaining both size and capacity for a dynamic array?

    size records how many elements are currently stored; capacity records how many elements fit in allocated space.

  43. How can an array be represented without using fixed-size bracket notation?

    Represent it with a pointer to the first element of a dynamically allocated memory block.

  44. Why is repeatedly increasing a dynamic array by only one element inefficient?

    It may call realloc and copy the entire array each time a new element is added, producing repeated linear work.

  45. What is the role of EOF in an input loop?

    It signals that the user has finished providing input, allowing the program to stop reading.

  46. Why cannot getInt directly support typing q to quit?

    getInt expects an integer and immediately handles input as an integer rather than a quit string.

  47. What is a linked list?

    A sequence of nodes in which each node stores data and a pointer to the next node.

  48. What fields does a basic singly linked-list node contain?

    A data value, such as an integer, and a pointer to the next node.

  49. Why does a linked-list node definition use struct node *next?

    It stores a pointer to another node of the same structure type.

  50. Why is the structure name declared before the fields in a self-referential node definition?

    C reads declarations from top to bottom, so the name must be known before it is used for the next pointer.

  51. What does the next pointer of the final node in a singly linked list contain?

    NULL.

  52. What pointer must a program retain to access every node in a linked list?

    A pointer to the first node, often called the head or first pointer.

  53. What is the time complexity of searching for or inserting into a sorted singly linked list in the worst case?

    O(n)O(n)O(n).

  54. How can inserting an element at the beginning of an unsorted linked list take constant time?

    Point the new node to the current first node, then update the first pointer to the new node.

  55. Why must a new node's pointer be updated before changing the previous node's pointer during insertion?

    Otherwise, changing the previous pointer first could lose the address of the rest of the list.

  56. What is the difference between a singly linked list and a doubly linked list?

    A singly linked list stores a pointer only to the next node; a doubly linked list stores pointers to both the next and previous nodes.

  57. How can a linked list avoid the fixed-size limitation of an array?

    It allocates nodes individually as needed rather than reserving one contiguous block in advance.

  58. What does the arrow operator -> mean in C?

    It dereferences a pointer to a structure and accesses one of the structure's fields.

  59. What does a loop that repeatedly assigns ptr = ptr->next do?

    It traverses a linked list from one node to the next.

  60. What is hashing?

    Using a hash function to map input data to a bucket or index, often to speed up lookup.

  61. What is a hash function?

    A function that takes an input and produces a value, commonly an integer used as an index into a table.

  62. How can a hash table combine arrays and linked lists?

    Use an array of buckets, where each bucket contains a linked list of items mapped to that bucket.

  63. What is the worst-case lookup time for a hash table?

    O(n)O(n)O(n).

  64. Why might a hash table be faster in practice than a simple linear list?

    A good hash function distributes elements among buckets, making each bucket's linked list much shorter.

  65. What is a collision in a hash table?

    A situation in which multiple inputs map to the same bucket.

  66. What makes a hash function effective?

    It distributes inputs as uniformly as possible across the available buckets.

  67. What factors can be used to design a hash function for people?

    Names, last names, identification numbers, birth dates, or combinations of characters.

  68. What is a binary search tree?

    A tree in which each node's left descendants contain smaller values and its right descendants contain larger values.

  69. What is the root of a tree?

    The top node from which traversal begins.

  70. How can a binary search tree node be represented in C?

    With a data field and two pointers, one to a left child and one to a right child.

  71. What is the search time for a balanced binary search tree containing nnn elements?

    O(logn)O(\\log n)O(logn).

  72. Why can a binary search tree be preferable to an array?

    It can preserve efficient searching while allowing nodes to be added without copying every existing element.

  73. What is a trie?

    A tree whose nodes contain arrays used to represent sequences of characters.

  74. How is a word such as BILLY stored in a trie?

    The program follows or creates one character-indexed node for each successive letter: B, I, L, L, and Y.

  75. What is the lookup time for a word of length kkk in a trie?

    O(k)O(k)O(k), which is effectively constant relative to the number of stored words.

  76. What is the main trade-off of a trie?

    It provides very fast lookup at the cost of using substantial memory.

  77. How can a trie distinguish between a word and a prefix of a longer word?

    Each node needs additional information indicating whether a complete word ends there or whether the path continues.

  78. Why are tries useful for spell checkers?

    They can quickly determine whether a typed word exists in a large in-memory dictionary.

Card 1 of 78
Card 1 of 78

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent