Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote