File permissions in the kernel
File permissions in the kernel
Posted Aug 5, 2016 6:41 UTC (Fri) by johill (subscriber, #25196)In reply to: File permissions in the kernel by johill
Parent article: File permissions in the kernel
Posted Aug 5, 2016 19:39 UTC (Fri)
by xtifr (guest, #143)
[Link] (3 responses)
In C, if you put "const int foo = 5;" in a header, and include it in multiple files, you'll get a link-time error about multiple definitions. In C++, this not only works, it's preferred.
This is something that can easily trip up a C++ programmer who isn't used to C. C++ books tend to emphasize that #defines are bad and you should use consts instead.
I'm guessing that epa works with C++ more often than with C. :)
Posted Aug 5, 2016 23:47 UTC (Fri)
by Jonno (subscriber, #49613)
[Link] (2 responses)
Posted Aug 6, 2016 19:16 UTC (Sat)
by xtifr (guest, #143)
[Link]
The difference may be less than you think, though. You can make a pointer to a const int (and this should prevent the compiler from optimizing away the storage), but the same is true of an inline function. The real difference between an inline and a static is that a static will have a separate copy for each translation unit, while an inline will only have one copy for the whole program. (Assuming they aren't all optimized away.)
For normal use (no pointers), the difference won't matter, since the storage will be elided if you turn on even the most basic optimizations (which is pretty normal). And, since you can't modify a const int even with a cast (unless I've just found a bug in g++), the only practical difference would be if you tried to compare two different pointers to what you thought was the same const int. If the const is static, then the comparison *might* fail; if it's inline, it would succeed. But that's not something I've ever done or seen done, so I honestly don't know what the right answer is.
Anyway, I suspect we're drifting a bit off topic, and what you say sounds plausible enough, so I'm certainly not going to contradict you.
Posted Aug 13, 2016 2:38 UTC (Sat)
by thestinger (guest, #91827)
[Link]
File permissions in the kernel
File permissions in the kernel
File permissions in the kernel
File permissions in the kernel