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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:34:40+00:00 2026-06-06T20:34:40+00:00

I have to implement a wrapper for malloc called mymalloc with the following signature:

  • 0

I have to implement a wrapper for malloc called mymalloc with the following signature:

void mymalloc(int size, void ** ptr)

Is the void** needed so that no type casting will be needed in the main program and the ownership of the correct pointer (without type cast) remains in main().

void mymalloc(int size, void ** ptr)
{
    *ptr = malloc(size) ;
}
main()
{
    int *x;
    mymalloc(4,&x); // do we need to type-cast it again?
                    // How does the pointer mechanism work here?
}

Now, will the pointer being passed need to be type-cast again, or will it get type-cast implicitly?

I do not understand how this works.

  • 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-06T20:34:41+00:00Added an answer on June 6, 2026 at 8:34 pm

    malloc returns a void*. For your function, the user is expected to create their own, local void* variable first, and give you a pointer to it; your function is then expected to populate that variable. Hence you have an extra pointer in the signature, a dereference in your function, and an address-of operator in the client code.

    The archetypal pattern is this:

    void do_work_and_populate(T * result)
    {
         *result = the_fruits_of_my_labour;
    }
    
    int main()
    {
        T data;                      // uninitialized!
        do_work_and_populate(&data); // pass address of destination
        // now "data" is ready
    }
    

    For your usage example, substitute T = void *, and the fruits of your labour are the results of malloc (plus checking).

    However, note that an int* isn’t the same as a void*, so you cannot just pass the address of x off as the address of a void pointer. Instead, you need:

    void * p;
    my_malloc(&p);
    int * x = p;  // conversion is OK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to implement a sparse matrix (a matrix that has predominantly zeroes, so
I have the following situation: Three concrete service classes implement a service interface: one
So I have a wrapper class that when I send it a message, it
I have a UserControl, which is basically a wrapper for a GridView that needs
I have an EntityId class that servers as a simple wrapper class to database
I have got an assignment in which I have to implement a AS3 wrapper
I have written a small wrapper for the dict built-in class that loads entries
If I have a class that needs to implement an interface but one or
Here's my situation: I have .NET wrapper-objects in a C++/CLI layer that hold pointers
I am trying to implement the youtube_it youtube api wrapper for ruby and have

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.