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 8504915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:10:14+00:00 2026-06-11T02:10:14+00:00

I am using the mpz_class (using MPIR 2.5.1 with Visual Studio C++ 2010, and

  • 0

I am using the mpz_class (using MPIR 2.5.1 with Visual Studio C++ 2010, and the C++ version of MPIR), and for me it’s not feasible to store huge numbers in memory, so I want to do it with binary files.

I already finished this with text files, but when I use 100,000+ bit numbers, binary files should will (hopefully) save a lot of space.

I have written a short example to help you understand what I’m trying to do:

ofstream binFile;
binFile.open ("binary.bin", ios::out | ios::binary);

mpz_class test;
test.set_str("999999999999999",10);

binFile.write((char *)(&test), sizeof(test));

cout << "NUMBER: " << test << "\tSIZE: " << sizeof(test) << endl;
binFile.close();

I am trying to write the character-data representing the mpz_class instance. Then, to test it, I tried to read the file:

ifstream binFile2;
binFile2.open("binary.bin", ios::in | ios::binary);

mpz_class num1 = 0; 
binFile2.read ((char *)(&num1), sizeof(num1));

cout << "NUMBER: " << num1 << "\tSIZE: " << sizeof(num1) << endl;
binFile2.close();

Many examples I see online use this method for storing class data into binary files, but my output is this:

NUMBER: 999999999999999 SIZE: 12

NUMBER: 8589934595      SIZE: 12

Why can’t I store class data directly, and then read it again? There is no way the instance of mpz_class can be size 12, is this the size of the pointer??

I have also tried this, but I think it’s basically the same thing:

char* membuffer = new char[12]; //sizeof(test) returned 12
binFile2.read (membuffer , sizeof(test));
memcpy(&test, &membuffer, sizeof(test))

Any advice on how to fix this would be appreciated. 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-06-11T02:10:15+00:00Added an answer on June 11, 2026 at 2:10 am

    I think you need to spend more time with the GMP manual (section 12.1):

    Conversions back from the classes to standard C++ types aren’t done
    automatically, instead member functions like get_si are provided (see
    the following sections for details).

    So, what you probably need to do is call mpz_class::get_str and mpz_class::set_str. Anyway, the C++ interface is just a light wrapper around the C API, so you’re probably better off using the low-level stuff, since it’s much better documented. In this case, you would have to use mpz_get_str and mpz_set_str (for integers).

    Just keep in mind that there’s no API function that can provide a direct binary serialization of the GMP data types, so you need to work with strings. I’m not sure if there are certain limitations to the size of these beasts, so you should test your code thoroughly if you plan to make use of such large numbers. Maybe the best choice is to extract a string representation in base 62 (maximum allowed) so that it doesn’t blow up your memory (in base 2 it will eat up one byte for every bit) and then write that to file.

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

Sidebar

Related Questions

Using Yii, I want to delete all the rows that are not from today.
Using a populated Table Type as the source for a TSQL-Merge. I want to
Using WPF/PRISM I want to log my messages through ILoggerFacade to my GUI (A
using a binary search tree I need to add to a vector all int
Using Delphi 2010. I am looking for (possibly) a function or procedure which can
Using php/html, I want to retrieve email addresses (plus other information) from MySQL and
Using emacs on Ubuntu 11.10. I want to connect to a SQL Server database
Using restlet, I want to make a post to android's c2dm service. I have
Using interface builder, I want an a button that when clicked shows you a
Using ASP.Net and VB.Net Code Image1.ImageUrl = c:\Blue hills.jpg The above code is 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.