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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:15:42+00:00 2026-05-14T18:15:42+00:00

I am transitioning from C to C++. In C++, is there any use for

  • 0

I am transitioning from C to C++. In C++, is there any use for the malloc function, or can I just use the new keyword? For example:

class Node {
    /* ... */
};

/* ... */

Node *node = malloc(sizeof(Node));
// vs
Node *node = new Node;

Which one should I use?

  • 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-14T18:15:42+00:00Added an answer on May 14, 2026 at 6:15 pm

    Use new. You shouldn’t need to use malloc in a C++ program, unless it is interacting with some C code or you have some reason to manage memory in a special way.

    Your example of node = malloc(sizeof(Node)) is a bad idea, because the constructor of Node (if any exists) would not be called, and a subsequent delete node; would have undefined results.

    If you need a buffer of bytes, rather than an object, you’ll generally want to do something like this:

    char *buffer = new char[1024];
    // or preferably:
    std::unique_ptr<char[]> buffer = std::make_unique<char[]>(1024);
    // or alternatively:
    std::vector<char> buffer(1024);
    

    Note that for the latter examples (using std::vector or std::unique_ptr), there is no need to delete the object; its memory will automatically be freed when it goes out of scope. You should strive to avoid both new and malloc in C++ programs, instead using objects that automatically manage their own memory.

    Here are your options and their benefits:

    Method Dynamically Sized Automatically Managed Resizable
    std::array<char, N> No Yes No
    new char[N] Yes No No
    std::unique_ptr<char[]> Yes Yes No
    std::vector<char> Yes Yes Yes
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are transitioning to git from SVN, and there's some concepts I can't wrap
From the Transitioning to ARC Release Notes Use Lifetime Qualifiers to Avoid Strong Reference
Possible Duplicate: “using” keyword in java I'm transitioning from C# to java, so please
I am just transitioning from Eclipse to Wing IDE for my Python code. In
i'm still transitioning from as2 to as3, i'm having trouble with parsing XML data
I am currently transitioning from VB to C# and am having some issues with
I have a question about TortoiseHg. My organization is transitioning from SVN to Mercurial.
I'm having trouble transitioning to Java from C/C++ for my Telnet interface to some
How does Windows protect against a user-mode thread from arbitrarily transitioning the CPU to
I am transitioning code that used implementations of Spring MVC's Controller to use the

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.