0

I was going through Julia backend and found this

struct _jl_taggedvalue_bits {
    uintptr_t gc:2;
};

When I initialize it and look in debugger gc is zero initialized. So, what is the exact meaning of gc:2?

enter image description here

Found here: https://github.com/JuliaLang/julia/blob/8c9799530383d571d5beab4e530d86e40a289929/src/julia.h#L95

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
aneax
  • 1
  • 3
  • 1
    It's a bitfield: https://en.cppreference.com/w/c/language/bit_field – UnholySheep Nov 23 '21 at 11:08
  • It wasn't initialised, it is a local variable. – Weather Vane Nov 23 '21 at 11:10
  • Unrelated? See [C11 6.7.2.1#5](https://port70.net/~nsz/c/c11/n1570.html#6.7.2.1): *"A bit-field shall have a type that is a qualified or unqualified version of `_Bool`, `signed int`, `unsigned int`, or some other implementation-defined type."* Note the absence of `uintptr_t` – pmg Nov 23 '21 at 11:16
  • @pmg, isn't `uintptr_t` implementation defined? – tstanisl Nov 23 '21 at 11:19
  • [C++ info on bit fields.](https://en.cppreference.com/w/cpp/language/bit_field) – IS4 Nov 23 '21 at 11:19
  • 1
    @tstanisl, well, yes, but it feels wrong to have 2 bits associated with a type that includes "`ptr`" in its identifier. :-) – pmg Nov 23 '21 at 11:35

1 Answers1

0

When I initialize it and look in debugger gc is zero initialized.

It wasn't zero initialised. There is a difference between being zero initialised, and being uninitialised, but incidentally having the value zero in memory.

Meaning of :2 in uintptr_t gc:2

It's syntax for bitfield. 2 is the width of the field.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    Many bad tool chains zero-init all of the stack in debug build, I guess for the purpose of hiding bugs as long as possible, so that they don't get fixed early on, when it's cheap and easy. Then when you switch to release build, the whole program breaks. – Lundin Nov 23 '21 at 12:15