Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3801350
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:02:12+00:00 2026-05-19T14:02:12+00:00

I was perusing some code using arbitrary-length integers using the GNU Multi-Precision (GMP) library

  • 0

I was perusing some code using arbitrary-length integers using the GNU Multi-Precision (GMP) library code. The type for a MP integer is mpz_t as defined in gmp.h header file.

But, I’ve some questions about the lower-level definition of this library-defined mpz_t type. In the header code:

/* THIS IS FROM THE GNU MP LIBRARY gmp.h HEADER FILE */
typedef struct
{
    /* SOME OTHER STUFF HERE */
} __mpz_struct;

typedef __mpz_struct mpz_t[1];

First question: Does the [1] associate with the __mpz_struct? In other words, is the typedef defining a mpz_t type as a __mpz_struct array with one occurrence?

Second question: Why the array? (And why only one occurrence?) Is this one of those struct hacks I’ve heard about?

Third question (perhaps indirectly related to second question): The GMP documentation for the mpz_init_set(mpz_t, unsigned long int) function says to use it as pass-by-value only, although one would assume that this function would be modifying its contents within the called function (and thus would need pass-by-reference) syntax. Refer to my code:

/* FROM MY CODE */
mpz_t fact_val;                /* declaration */
mpz_init_set_ui(fact_val, 1);  /* Initialize fact_val */

Does the single-occurrence array enable pass-by-reference automatically (due to the breakdown of array/pointer semantics in C)? I freely admit I’m kinda over-analyzing this, but I’d certainly love any discussion on this. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:02:13+00:00Added an answer on May 19, 2026 at 2:02 pm

    *First question: Does the [1] associate with the __mpz_struct? In other words, is the typedef defining a mpz_t type as a __mpz_struct array with one occurrence?*

    Yes.

    Second question: Why the array? (And why only one occurrence?) Is this one of those struct hacks I’ve heard about?

    Beats me. Don’t know, but one possibility is that the author wanted to make an object that was passed by reference automatically, or, “yes”, possibly the struct hack. If you ever see an mpz_t object as the last member of a struct, then “almost certainly” it’s the struct hack. An allocation looking like

    malloc(sizeof(struct whatever) + sizeof(mpz_t) * some_number)`
    

    would be a dead giveaway.

    Does the single-occurrence array enable pass-by-reference automatically…?

    Aha, you figured it out too. “Yes”, one possible reason is to simplify pass-by-reference at the expense of more complex references.

    I suppose another possibility is that something changed in the data model or the algorithm, and the author wanted to find every reference and change it in some way. A change in type like this would leave the program with the same base type but error-out every unconverted reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While perusing some Apple Header files, I notice that they declare multiple interfaces using
Perusing the code I am maintaining, I see that in some places the With
I was perusing some topics and started thinking about a way to get the
I'm using Spring ( 3.0.5.RELEASE ) to create & manage some beans and am
I was perusing the underscore.js library and I found something I haven't come across
Several times, while perusing the Boost library's documentation, I've run across return values that
I was perusing an Lua code file, and the very top of the file
Perusing through the documentation , I see an endpoint that can be used to
While perusing the source for AbstractMap in Java I ran across this: 440 /**
I was perusing section 13.5 after refuting the notion that built-in operators do not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.