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

  • Home
  • SEARCH
  • 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 995887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:47:31+00:00 2026-05-16T06:47:31+00:00

Some of my code still uses malloc instead of new . The reason is

  • 0

Some of my code still uses malloc instead of new. The reason is because I am afraid to use new because it throws exception, rather than returning NULL, which I can easily check for. Wrapping every call to new in a try{}catch(){} also doesn’t look that good. Whereas when using malloc I can just do if (!new_mem) { /* handle error */ }.

Therefore I have a question. Can I use smart pointers together with malloc?

Something like:

SmartPointer<Type> smarty = malloc(sizeof(Type));

Something like this.

Is this possible?

Thanks, Boda Cydo.

  • 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-16T06:47:32+00:00Added an answer on May 16, 2026 at 6:47 am

    If you are using shared_ptr or unique_ptr, you can specify a custom deleter. For example,

    struct free_delete
    {
        void operator()(void* x) { free(x); }
    };
    

    This can be used with shared_ptr like so:

    std::shared_ptr<int> sp((int*)malloc(sizeof(int)), free_delete());
    

    If you are using unique_ptr, the deleter is a part of the unique_ptr‘s type, so the deleter needs to be specified as a template argument:

    std::unique_ptr<int, free_delete> up((int*)malloc(sizeof(int)));
    

    However, it is better to use exceptions correctly, rather than avoiding them, when writing C++, especially with respect to allocation failures. In most cases, you cannot successfully recover from an allocation failure in the function trying to do the allocation, so exceptions can help you to handle the error where you are actually capable of handling it.

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

Sidebar

Related Questions

I have some existing code that uses an enum instead of a function pointer
Hi I have some code that uses block RandomAccessFile file = new RandomAccessFile(some file,
I'm still a little unclear and when to wrap a lock around some code.
Edit: The code here still has some bugs in it, and it could do
I've gone through most of the example code and I still need some help.
We've got an app with some legacy printer setup code that we are still
In some code I've inherited, I see frequent use of size_t with the std
I'm maintaining some code that uses a *= operator in a query to a
We have a J2EE app built on Struts2+spring+iBatis; not all DAO's use iBatis...some code
In Eclipse (Ganymede) I'm debugging some code that uses Apache Commons HttpClient and would

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.