in the C standard, there is a concept of an "object" that 's defined as:
region of data storage in the execution environment, the contents of which can represent values
(note: the concept of object here has nothing to do with OOP, you better think about objects in C standard sense as semantic concepts rather than actual objects with methods and OOP principles)
let's explain this concept piece by piece and start with data storage, which means any addressable memory for your executable (typically its virtual address space) which we can say is just RAM, practically speaking though we can think about memory-mapped files as part of the data storage since they're added to addressable memory by the execution environment.. what is the execution environment?
the execution environment is the underlying operating system or environment which your executable runs on top, in case of freestanding builds (like firmware, embedded systems, kernels, etc..) there is no execution environment, but if you're writing an application for a specific platform (e.g. windows, linux or mac) then this platform is the execution environment
then the C standard says that the contents of this region of data storage (a sequence of byes) represent values, and here is the specific concept I want to explain. (there are other properties of object that C standard defines but these are out of context now)
to represent value you should have an interpretation, in case of the C standard the "effective type" is the type of the object which dictates how we'll interpret (read/write) to the region of data storage for this object, and to specify the effective type of an object we either explicitly set it as in declaring variables:
```c
int x = 5;
```
here we're declaring an object whose effective type is int, which means that you guarantee that this object will only be accessed through int or compatible-type object according to the strict aliasing rules, for example, unsigned int is a compatible type, while as float isn't!
this means that if you accessed the variable x using a float pointer you'll have undefined behavior (whatever happens happens), this is the core of the interpretation concept, and if you tried to forcefully access an object through an incompatible type you'll get meaningless/garbage data because you interpreted the sequence of bytes (the contents of the region of data storage which are supposed to represent values of a specific effective type or a compatible one according to strict aliasing rule)
there is an exception though the C standard puts which actually gives C one of its most powerful features which is char pointers, according to the C standard, char. signed char and unsigned char are permitted to alias any object type without triggering undefined behavior and this is why C programmers always use char pointer to read raw memory for example when the type of that memory is ambiguous or unknown at all
this is a very philosophical concept but it's the only way I understand a lot of stuff
this means that if you accessed the variable x using a float pointer you'll have undefined behavior (whatever happens happens), this is the core of the interpretation concept, and if you tried to forcefully access an object through an incompatible type you'll get meaningless/garbage data because you interpreted the sequence of bytes (the contents of the region of data storage which are supposed to represent values of a specific effective type or a compatible one according to strict aliasing rule)
there is an exception though the C standard puts which actually gives C one of its most powerful features which is char pointers, according to the C standard, char. signed char and unsigned char are permitted to alias any object type without triggering undefined behavior and this is why C programmers always use char pointer to read raw memory for example when the type of that memory is ambiguous or unknown at all
this is a very philosophical concept but it's the only way I understand a lot of stuff
this is why a bit stream like: 01000001 can mean the letter 'A' according the ASCII table, a color value in RBG system, a combination of permissions and actually an infinite number of meanings, it's just context that dictates the interpretation of this bitstream and human perception is which gives meaning to this interpretation!
No comments:
Post a Comment
any inappropriate comments will be deleted and the commenter will get blocked permanently unless I'm free to fuck him.