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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:06:16+00:00 2026-05-11T11:06:16+00:00

I’ve been using C++ for a short while, and I’ve been wondering about the

  • 0

I’ve been using C++ for a short while, and I’ve been wondering about the new keyword. Simply, should I be using it, or not?

  1. With the new keyword…
    MyClass* myClass = new MyClass();     myClass->MyField = "Hello world!"; 
  1. Without the new keyword…
    MyClass myClass;     myClass.MyField = "Hello world!"; 

From an implementation perspective, they don’t seem that different (but I’m sure they are)… However, my primary language is C#, and of course the 1st method is what I’m used to.

The difficulty seems to be that method 1 is harder to use with the std C++ classes.

Which method should I use?

Update 1:

I recently used the new keyword for heap memory (or free store) for a large array which was going out of scope (i.e. being returned from a function). Where before I was using the stack, which caused half of the elements to be corrupt outside of scope, switching to heap usage ensured that the elements were intact. Yay!

Update 2:

A friend of mine recently told me there’s a simple rule for using the new keyword; every time you type new, type delete.

    Foobar *foobar = new Foobar();     delete foobar; // TODO: Move this to the right place. 

This helps to prevent memory leaks, as you always have to put the delete somewhere (i.e. when you cut and paste it to either a destructor or otherwise).

  • 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. 2026-05-11T11:06:17+00:00Added an answer on May 11, 2026 at 11:06 am

    Method 1 (using new)

    • Allocates memory for the object on the free store (This is frequently the same thing as the heap)
    • Requires you to explicitly delete your object later. (If you don’t delete it, you could create a memory leak)
    • Memory stays allocated until you delete it. (i.e. you could return an object that you created using new)
    • The example in the question will leak memory unless the pointer is deleted; and it should always be deleted, regardless of which control path is taken, or if exceptions are thrown.

    Method 2 (not using new)

    • Allocates memory for the object on the stack (where all local variables go) There is generally less memory available for the stack; if you allocate too many objects, you risk stack overflow.
    • You won’t need to delete it later.
    • Memory is no longer allocated when it goes out of scope. (i.e. you shouldn’t return a pointer to an object on the stack)

    As far as which one to use; you choose the method that works best for you, given the above constraints.

    Some easy cases:

    • If you don’t want to worry about calling delete, (and the potential to cause memory leaks) you shouldn’t use new.
    • If you’d like to return a pointer to your object from a function, you must use new
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 265k
  • Answers 265k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here; http://www.breakitdownblog.com/supported-values-for-suppresswarnings/. Note these are for main Java libraries; other… May 13, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer cause I have assigned TargetControlID of HoverMenuExtender in code behind… May 13, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer Depends on the algorithm used for the pseudo-random generation. If… May 13, 2026 at 12:33 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.