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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:42:12+00:00 2026-05-17T20:42:12+00:00

I’m working with gnump and have a function that must return mpz_t . So

  • 0

I’m working with gnump and have a function that must return mpz_t. So I have to use raw pointers to return a value. I allocate space with new for pointer and send it as a parameter in my function.

I think it is better to use smart pointers. But I didn’t work with them before. I read the manual but still can’t understand how to use shared_ptr properly to return a variable from a function.

shared_ptr<mpz_t> func()
{
    mpz_t z;
    mpz_init_set_str(z, "23423423423", 10);

    shared_ptr<mpz_t> p /* Shall I allocate space with "new" or smth else?.. */

    return p;
}

I would be grateful for any example.

  • 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-17T20:42:12+00:00Added an answer on May 17, 2026 at 8:42 pm

    The use of shared pointers in this context does not help you. The type mpz_t itself is pointer-like. Such a pointer is initialised by calling any of the mpz_init_… functions. However, you need to call a mpz_clear to free the space allocated by the init function you’ve used.

    Storing the pointer-like in a shared_ptr does not have the effect you want. It does keep track of the number of references to your mpz_t variable, and it does also delete mpz_t variable once there are no more references to it. However, that only frees the mpz_t variable itself, which is pointer-like. It does not call the mpz_clear function.

    Smart pointers are extremely helpful, but they are meant to refer to class objects, not pointer-like variables. They take care of the destruction of the object they reference. Which makes sense if they reference a complex object, but not if they reference a pointer.

    GNU MP offers a C++ class interface. (Look for mpz_class)

    shared_ptr<mpz_class> func()
    {
      shared_ptr<mpz_class> z(new mpz_class("23423423423", 10));
      return z;
    }
    

    If you need to pass an mpz_t to other functions, you can obtain it from the shared pointer:

    p->get_mpz_t()
    

    where p is of type shared_ptr<mpz_class>.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

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.