3 ms·
We have our differences, but you’re totally correct here, and I’m not sure why you’re downvoted. The “byte addressable” thing is exactly why C was created over
by steveklabnik 8y ago
We have our differences, but you’re totally correct here, and I’m not sure why you’re downvoted.
The “byte addressable” thing is exactly why C was created over B, even, right? That was one of the crucial features not supported.
- icedchai 8y agoI don't think he is correct. The underlying representation of a pointer is not defined by the C standard. You could have a C implementation that works on segmented, non-flat architectures. Look at the C compilers from the DOS days, for example...
- steveklabnik 8y agoI think we're talking past each other, and in some ways, this is what the post is about. The C abstract machine presents a flat, uniform, byte-addressed address space.[1] The reason that it does not define what a pointer's representation is is because it needs to map that to what the hardware actually does, which is your point. 1: Actually, my impression is that it does, but this is actually an area of the spec in which I'm less sure. I'm going to do some digging. Regardless, the point is that the memory model != the machine model, which is your point. EDIT: For example, https://en.cppreference.com/w/c/language/memory_model https://en.cppreference.com/w/c/language/memory_model > The data storage (memory) available to a C program is one or more contiguous sequences of bytes. Each byte in memory has a unique address. Though cppreference is not the spec itself, of course. It is the conceptual model that's often presented. LAST EDIT: I happened to run into someone who seemed knowledgeable on this topic on Reddit: https://www.reddit.com/r/programming/comments/9kruju/should_you_learn_c_to_learn_how_the_computer_works/e71l1zg/ https://www.reddit.com/r/programming/comments/9kruju/should_... TL;DR no! This is another misconception. Tricky!
- icedchai 8y agoYep, these common assumptions are not always true if you read deep enough into the spec. Even though they may be true in practice, only as a side effect in most implementations... I taught myself C when I was a teenager (on an Amiga, back in the 80's.) It is amazing that almost 30 years later, I'm still learning new things about it.
- Animats 8y agoI'm not sure why C was created over B. ANSI C had more abstraction than K&R C; early versions of C didn't have function prototypes or type checking. There seems to have been a slow progression towards more abstractions, as the machines used for compiling got bigger. Many of the early bad decisions in C probably come from having to run the compiler in a very small, slow machine. Today we expect to get the whole program into an in memory data structure within the compiler, but that was not possible in the PDP-11 era. C abstracts control flow, but not memory access. C data access is pretty much what the hardware gives you. Memory is bytes addressed by integers. There's even pointer arithmetic. C puts a layer of data structures on top of that, but the underlying memory model is one "(char *)" cast away. Every call to "write" implies that cast. By the time you get to, say, Python or Javascript, that flat memory model is completely hidden. There could be a compacting garbage collector underneath, changing the addresses of everything while the program is running, and you'd never know.
- icedchai 8y agoThe memory model you describe is overly simplistic, and is not actually mandated by the C standard. Too many assumptions in that area will lead to undefined behavior. Example: conversions between function pointers and regular pointers... https://stackoverflow.com/questions/32437069/c-void-function-pointer-argument https://stackoverflow.com/questions/32437069/c-void-function...
- vram22 8y agoI just looked up B (again, had seen it earlier): https://en.wikipedia.org/wiki/B_(programming_language) https://en.wikipedia.org/wiki/B_(programming_language) From the above article: >B is typeless, or more precisely has one data type: the computer word. I had read the Martin Richards (inventor of the language) book about BCPL (predecessor of B) early in my career, although I could not work on it, since there was no machine available to me that could run it. (It was already outdated then.) Interesting language though, and even the systems software examples shown in the book (written using BCPL) were cool. You're right about why C was created, byte addressability. From the article about C: https://en.wikipedia.org/wiki/C_(programming_language) https://en.wikipedia.org/wiki/C_(programming_language) >The original PDP-11 version of Unix was developed in assembly language. The developers were considering rewriting the system using the B language, Thompson's simplified version of BCPL.[11] However B's inability to take advantage of some of the PDP-11's features, notably byte addressability, led to C.