4 ms·
Instead of the second, I'd rather put: "Thou shalt understand pointers, before using them"
by illuminated 6y ago
Instead of the second, I'd rather put: "Thou shalt understand pointers, before using them"
- gigatexal 6y agoCan you program in C effectively without pointers?
- klyrs 6y agoDepends on what you mean by "effectively." If you wanna, say, do an exhaustive search over all 4-dimensional integer vectors in a rectangular region, and print off the ones that satisfy some relationship, you don't need pointers (nevermind what printf and string literals are doing under the hood). It's a wonderfully beefy desktop calculator, in that way. But for most applications, pointers are pretty much necessary.
- WalterBright 6y agoArrays in C (once passed to functions) are pointers, even when they use array syntax: https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
- klyrs 6y agoYou don't need arrays for the code I was describing: it's a nested for-loop on 4 ints. But, yeah, that's a really common mistake.
- illuminated 6y agoNope, therefore you cannot do it effectively if you do not understand them :)
- Gibbon1 6y agoYou can program C effectively without pointer arithmetic. Storing copies of pointers for later is usually a bad idea.
- pm215 6y agoThe second item is warning against a particular misuse of pointers that was apparently widespread at the time. On VAX Unix, it happened that the memory at address zero of a process was readable and contained zeroes. So a lot of code would misuse a NULL pointer as being equivalent to a pointer-to-an-empty-string, for instance: worked fine on the VAX!. So it's kind of a specific flavour of commandment 10. These days the common machines don't have a readable address zero, and all the old non-portable code has faded into history.
- asveikau 6y ago16-bit x86 also had the interrupt handler table at linear address 0, I seem to recall some compilers for DOS that would make NULL point at that.
- oriolid 6y agoYes. At least writing to interrupt vector table usually crashed your computer right at the point. I remember a certain DSP architecture that system flags near zero address. It was an interesting experience to debug a program that failed because a write to struct member through null pointer switched the machine to a different instruction set.
- kevin_thibedeau 6y agoEvery MMU-less micro lets you read address 0.
- monocasa 6y agoA lot of micros have MPUs that'll catch address 0 accesses without a full MMU. AFAIK all ARMs newer than ARM7 (ie. ARMv5) have one for instance.
- RealityVoid 6y agoYeah, they will, if you activated it. And if you made access to 0 address be caught explicitly ( you might have the MMU active but not have it configured in such a way)