1/19
This set of flashcards covers memory management errors (stack vs. heap, double free, use-after-free), debugging and profiling tools (Valgrind, Gcov, Gprof), and C/Vim fundamentals based on the lecture transcript.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Why is it dangerous to return a pointer to a local variable like val in the function foo?
Local variables exist on the stack frame; once the function returns, that frame is reclaimed, and the pointer points to memory that could be overwritten or used for something else.
How can a programmer ensure an array persists after a function returns?
Use malloc to allocate the array on the heap, which remains persistent for the life of the program until explicitly freed.
What is a 'double free' error?
It occurs when a program attempts to free the same memory address twice, usually resulting in a crash with a SIGABRT signal.
What error occurs if a program attempts to assign a value to an index of an array after free(x) has been called?
Use after free.
When freeing an array of character pointers (char∗∗p), why is calling free(p) alone insufficient?
Calling free(p) only frees the array holding the pointers; it does not free the individual memory blocks (strings) that each pointer in the array points to.
What is Valgrind and what is its default tool?
Valgrind is an instrumentation framework for building dynamic analysis tools, and its default tool is MemCheck, which looks for memory errors and leaks.
Why should the −g flag be used when compiling code for use with Valgrind?
It provides debugging information that allows Valgrind to report the exact line number in the source code where an error occurs.
What does a 'heap block overrun' indicate in Valgrind?
It indicates the program is accessing memory outside of the specific block allocated by malloc, such as accessing index 10 of a 10-element array.
According to the lecture, when should memory errors vs. memory leaks be addressed?
Memory errors should be addressed as soon as they appear during regular testing, while memory leaks are typically checked after completing the program.
Why might strlen(prefix)+strlen(filename) be too small for memory allocation using malloc?
The strlen function does not include the null terminator in its count, so one extra byte must be allocated.
What does the gcov tool provide to a developer?
It provides code coverage analysis, showing which lines of the source code have been executed (and how many times) during testing.
Does 100% code coverage guarantee the code is correct?
No; it only means every bit of code has been run, not that it functions correctly under todos scenario.
What is the purpose of the gprof tool and what flag is required to use it?
It is used for profiling to see where a program spends its time; it requires the −pg flag during compilation.
What is represented by 'user' and 'sys' in the output of the time utility?
'User' represents the time spent executing the code itself, while 'sys' represents time spent executing system calls.
What is the C syntax to define a type foo as a struct with members intx, chary, and pid_tz?
typedeftextstructtext{inttextx;textchartexty;textpid_ttextz;}textfoo;
How do you declare a typedef for a pointer to a function that takes two void∗ arguments and returns an int?
typedeftextinttext(∗foo)(void∗,textvoid∗);
In Vim, what is the command to exit without saving changes?
:q! followed by the Enter key.
What Vim normal mode command deletes the line at the current cursor position?
dd
What Makefile variable is standard for specifying the C compiler?
CC
What is the purpose of the all target in a Makefile?
It serves as the default target that builds the primary executable(s) of the project.