4 ms·
No, you'll get UB.
by lomnakkus 8y ago
No, you'll get UB.
- bluecalm 8y agoObviously you check for NULL first. If something may or may not exists you first ask if it exists (check for NULL) and then of it exists you do something with it. When you free something you destroy it, that's why it's useful to set the pointer which before pointed to it to NULL.
- lomnakkus 8y agoI just (well, a few days ago) explained to you that your suggested "solution" would result in UB. Then you come up with the "solution" that everybody must always check for NULL pointers before dereferencing them. If everybody always did that we might as well just define the behavior of reference-after-free, because what you said either a) requires you to keep a reference count (or null-check before every deref which may be even more expensive) at runtime(!), or b) requires a program-level proof. So... no. What you suggested is not a solution in any meaningful sense.