0

Most of the sites I've been reading state that a zero-sized array, single element array or Flexible Array Members is the way to go when a dynamic array is needed, but the definition of an integer in GMP's source code is a pointer at the end of the struct (__mpz_struct), why is that?

I was expecting it to use zero-sized arrays, single element arrays or FAM

14
  • 1
    GMP uses the struct to control the size of the number, similar to a dynamic array that one would write with count, capacity, pointer. This is __mpz_struct. But, this is semi-hidden. One uses mpz_t: typedef __mpz_struct mpz_t[1]; so one can pass around what looks like numbers "passed by value" when, in fact, we're passing around pointers. Commented Dec 8, 2023 at 19:03
  • 3
    Most of the sites I've been reading state that a zero-sized array, single element array or Flexible Array Members is the way to go when a dynamic array is needed - this is quite arguable. Zero-size array is non-standard and along with the single-element array are kind of "hack" to emulate FAM. FAM itself is quite confusing for many developers and has limitations (like having only one per struct and it must be the last element). Using a pointer to dynamic substructure is a natural choice if one doesn't want to deal with the above. Commented Dec 8, 2023 at 19:14
  • Predates the addition of FAMs to the language and didn't want to use old compiler extensions like a 0 length array? Commented Dec 8, 2023 at 19:25
  • Further, __mpz_struct has mp_limb_t * which is where the variable length data is stored. mp_limb_t can be uint32_t or uint64_t (or even uint16_t). This depends upon what the "natural" register/integer size is for the processor we're building for (e.g.): uint16_t for 8086, uint32_t for i386, uint64_t for x86_64. This is something that the lib worries about so we don't have to. Commented Dec 8, 2023 at 19:29
  • 1
    Just redid my websearch. Okay, int data[]; as the last element of the struct. No, it can't be used in GMP because the library must be able to grow/shrink the array at will. For example, if we use GMP to iterate factorials from 1 to N, we start with just one limb but grow to thousands (or millions) as needed. FAM can't do this for GMP. Commented Dec 8, 2023 at 19:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.