Question
A pointer that points to a freed block of memory is called a stale pointer. Stale pointers are insidious in that dereferecing them does not
A pointer that points to a freed block of memory is called a stale pointer. Stale pointers are insidious in that dereferecing them does not always fail, even though it should. If the pointer was just magically set to NULL upon being freed, then you'd get a seg fault. Your friend writes this wrapper around free to do just this for ints:
void freeint(int *p) {
free(p);
p = null;
}
Try this code out with our own main and see if it crashes on dereferencing a stale pointer. Spoiler, it doesn't. Describe whats wrong and rewrite the code to work as intended. After writing freeint, write a more general stalefree that works for all types but still complies without warnings
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started