A pointer that points to a freed block of memory is called a stale pointer. Stal
ID: 3729939 • Letter: A
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 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.
Explanation / Answer
IT is not crashing because it is pointing to NULL (a 0 value).
Correct code.
void freeint(int **p) {
free(p);
*p = NULL;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.