Assert for Debugging
C
Medium
3 views
Problem Description
Verify assemblies in critical functions using assert(). Example: pointer must not be null. Enabled in debug builds, disabled in release builds.
Official Solution
#include <stdio.h>
#include <assert.h>
void process_data(int *ptr) {
// Critical assumption: ptr must not be NULL
assert(ptr != NULL);
// Safe to use ptr now
*ptr = *ptr + 10;
}
int main() {
int value = 20;
int *p = &value;
process_data(p);
printf("Value = %dn", value);
// Uncommenting this will trigger assert failure in debug build
// process_data(NULL);
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!