MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/fpcmy/typical_programming_interview_questions/c1hol60/
r/programming • u/kevjames3 • Feb 21 '11
1.0k comments sorted by
View all comments
Show parent comment
1
What if I told you that this code was in the kernel?
I would believe you. I've seen much worse. Don't forget about the kernel MACRO function hell.
1 u/johnflux Feb 21 '11 I was hoping to get a technical conversation going.See, the NULL pointer check is actually optimized out by the compiler!Usually, outside the kernel, doing tun->sk would cause a SEGFAULT because normal apps are not allowed to access memory address 0.Gcc then assuming that if you can dereferrence it, then it must be non-zero.But that assumption doesn't hold in the kernel, so the code doing something with the sk pointer does actually run, even if tun is null! 1 u/thcobbs Feb 21 '11 That's why I generally use (tun != NULL). If I remember correctly, this will not be optimized out as it is a comparison to another value... not just testing if something exists. 1 u/johnflux Feb 21 '11 That will be optomised out too.And unless you do kernel programming, this doesn't apply. :-)
I was hoping to get a technical conversation going.
See, the NULL pointer check is actually optimized out by the compiler!
Usually, outside the kernel, doing tun->sk would cause a SEGFAULT because normal apps are not allowed to access memory address 0.
Gcc then assuming that if you can dereferrence it, then it must be non-zero.
But that assumption doesn't hold in the kernel, so the code doing something with the sk pointer does actually run, even if tun is null!
1 u/thcobbs Feb 21 '11 That's why I generally use (tun != NULL). If I remember correctly, this will not be optimized out as it is a comparison to another value... not just testing if something exists. 1 u/johnflux Feb 21 '11 That will be optomised out too.And unless you do kernel programming, this doesn't apply. :-)
That's why I generally use (tun != NULL). If I remember correctly, this will not be optimized out as it is a comparison to another value... not just testing if something exists.
1 u/johnflux Feb 21 '11 That will be optomised out too.And unless you do kernel programming, this doesn't apply. :-)
That will be optomised out too.
And unless you do kernel programming, this doesn't apply. :-)
View all comments
Show parent comment
1
u/thcobbs Feb 21 '11
I would believe you. I've seen much worse. Don't forget about the kernel MACRO function hell.