FlashKeepers

C and C++ · College · College and University

C V: Undefined Behavior and Common Pitfalls

Buffer overflows, undefined and implementation-defined behavior, integer overflow, sequence points, aliasing, and debugging tools such as gdb and valgrind.

About this set

40 basic cards for College C and C++. It opens with "What is undefined behavior in C?" and "How can undefined behavior manifest at runtime?". Study it alongside the other College sets below.

40 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What is undefined behavior in C?
Show answerProgram behavior that is not defined by the C standard; the program may crash, produce incorrect output, or behave in any way whatsoever.
How can undefined behavior manifest at runtime?
Show answerThe program may crash, produce wrong results, or appear to work correctly. The standard does not constrain the outcome.
What is implementation-defined behavior in C?
Show answerBehavior not specified by the standard, but a conforming compiler must document how it handles the situation.
Is a C compiler required to warn about undefined behavior?
Show answerNo. The compiler may silently permit undefined behavior. Avoiding it is the programmer's responsibility.
Name three common sources of undefined behavior in C.
Show answerBuffer overflow, integer overflow, use-after-free, null pointer dereference, uninitialized variables, accessing out-of-bounds memory, or violating the strict aliasing rule.
What is a buffer overflow?
Show answerWriting data beyond the boundary of an allocated buffer, corrupting adjacent memory.
Why is the gets() function dangerous?
Show answerIt reads input into a buffer without size checking, making buffer overflow inevitable when input exceeds the buffer size.
What is the vulnerability of strcpy()?
Show answerIt copies a string without checking whether the destination buffer is large enough, allowing buffer overflow.
What is an off-by-one error?
Show answerAn error in loop bounds or array indexing that causes access to one element beyond the intended range, often from confusion about inclusive vs exclusive bounds.
Can a stack buffer overflow corrupt heap memory?
Show answerNo. Stack and heap are separate regions. A stack overflow corrupts adjacent stack memory such as return addresses or saved registers.
What happens when a signed integer overflows in C?
Show answerUndefined behavior. The result may wrap around, saturate, trap, or behave unpredictably depending on the compiler and platform.
How does unsigned integer overflow differ from signed?
Show answerUnsigned overflow wraps modulo 2^n and is well-defined. Signed overflow is undefined behavior.
How can you detect integer overflow before it occurs?
Show answerCheck operand bounds before the operation. For addition, verify that a <= INT_MAX - b before computing a + b.
What is integer underflow?
Show answerThe result of an operation is smaller than the minimum representable value. For unsigned types it wraps; for signed types it is undefined.
Name one defense against integer overflow.
Show answerCheck operands before the operation, use wider types for intermediate results, or use safe arithmetic libraries.

25 more cards in the app

Study this set free