6 ms·
> Bitfields may appear only as struct/union members, so there are no pointers to bitfields, arrays of bitfields You can certainly have a pointer to a struct me
by zabzonk 7y ago
> Bitfields may appear only as struct/union members, so
there are no pointers to bitfields, arrays of bitfields
You can certainly have a pointer to a struct member, so this isn't the reason why you can't have pointers to bitfields. The reason is that bits are not addressable, fullstop.
- gnode 7y agoBits are addressable, just not with a normal pointer. It would have been possible to have a special fat pointer for bits, similar to how C++ sometimes has fat pointers for member functions (depending on compiler implementation). The restriction in C can only be explained as a limitation of the language itself -- although probably motivated by the implementation complexity it would require, for a niche use case.
- deleted 7y ago[deleted]
- zabzonk 7y agoI didn't mention pointers regarding bits, I mentioned addressability - a bit cannot have an address (in any language I'm aware of), though of course you can have any number of ways of accessing it.
- gnode 7y agoPointers, as a language concept, don't have to correspond to the addressing schemes of the hardware or ISA. On some architectures instructions may only be able to address aligned whole words. Some microcontrollers (e.g. Intel MCS-51) feature bit-addressable memory. Apparently, there's a special __bit type supported by the Small Device C Compiler for using bit addressable memory on such devices, although I don't know if it has support for taking pointers to these.
- tjoff 7y agoIsn't a byte supposed to correspond to the smallest addressable unit of memory?
- gnode 7y agoThe original usage of the term "byte" was to refer to fields of variable length consecutive bits on a bit-addressable machine: https://en.wikipedia.org/wiki/Byte#History https://en.wikipedia.org/wiki/Byte#History Nowadays a byte is conventionally eight bits, especially for measures like "megabyte", but the term octet is often used to avoid ambiguity. Commonly they're used for pointers, yet often only words are addressable by machine instructions (e.g. many ARM instructions take a byte address yet raise a hardware exception on use of unaligned addresses).
- tjoff 7y agoInteresting, but I think the notion of a byte in C is different. But I'm not able to look it up at the moment.
- gnode 7y agoIt may well vary depending on which C standard you're talking about. ISO C defines both a byte and a char as at least long enough to contain characters "of the basic character set of the execution environment". They must be uniquely addressable. Although it seems their definitions don't preclude them from being different, or from sub-bytes being uniquely addressable by pointers.
- int_19h 7y agoIt is, but it's defined rather weirdly: "byte: addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" Hence why the type that corresponds to it is "char"! Beyond that, the only thing that kinda sorta implies that it's the smallest addressable unit is the definition of CHAR_BIT: "number of bits for smallest object that is not a bit-field (byte)"
- deleted 7y ago[deleted]
- mytailorisrich 7y agoYes, that's straightforward at least when coming to C from assembly (as it used to be the case once upon a time...) In practice, though, bitfields are seldom used, so everything related to them is largely academic.
- wahern 7y agoBitfields aren't commonly used, especially in public interfaces. But usage is far more prevalent than for typical "largely academic" features. For example, $ grep -rE ':[[:digit:]][[:digit:]]*;' /usr/include/ | wc -l 702 $ uname -a OpenBSD orville.25thandClement.com 6.6 GENERIC.MP#3 amd64 $ grep -rE ':[[:digit:]][[:digit:]]*;' /usr/include/ | wc -l 276 $ uname -a Linux alpine-3-10 4.9.65-1-hardened #2-Alpine SMP Mon Nov 27 15:36:10 GMT 2017 x86_64 Linux $ grep -rE ':[[:digit:]][[:digit:]]*;' /usr/include/ | wc -l 532 $ uname -a Linux splunk0 5.0.0-36-generic #39-Ubuntu SMP Tue Nov 12 09:46:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
- dtornabene 7y agothey're also more commonly used in security tooling
- deleted 7y ago[deleted]
- grawprog 7y agoNot C but I used bitfields pretty extensively for the toy 24-bit virtual machine I wrote in D. Internally, it's how I represented registers, bytes and words. There's only 16MB available for addressing so I wanted individual bits to be accessible. It was the easiest way I found to do that.
- mytailorisrich 7y agoActual bitfields are used extensively. I am obviously talking about the 'bitfield' C feature, which isn't.
- a1369209993 7y ago> Bitfields may appear only as struct/union members[, so bitfields may NOT appear as referents of pointers, elements of arrays, etc], so there are no pointers to bitfields, arrays of bitfields, etc