Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditions and pointers

If I have something like this

    if(pointer!=NULL&&(*pointer)==x) { ... }

Will it segfault if pointer is NULL? If it is compiler specific, I'd like to know how it is in GCC.

like image 614
wirm Avatar asked May 23 '26 13:05

wirm


1 Answers

No, because of short circuit evaluation.

If the LHS is false, then the RHS won't be evaluated. Therefore, if it's a NULL pointer, it will never hit the RHS and attempt to dereference it.

like image 137
alex Avatar answered May 25 '26 01:05

alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!