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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:52:14+00:00 2026-05-27T22:52:14+00:00

I would like some clarification on what happens when a class is instantiated on

  • 0

I would like some clarification on what happens when a class is instantiated on the stack.

When a C++ class is instantiated on the heap:

MyClass *myclass = new MyClass();

a pointer is created of type MyClass and the class is instantiated on the same line by “new MyClass();”. Stretching it out like this:

MyClass *myclass;
myclass = new MyClass();

If I’m not mistaken, a pointer is created on the 1st line and then memory is allocated for the instance on the 2nd line and a pointer to the address of the instance is assigned to myclass.

Does this mean that when a class is instantiated on the stack this way:

MyClass myclass = MyClass();

that it’s created twice?

  • 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-27T22:52:15+00:00Added an answer on May 27, 2026 at 10:52 pm

    “Stack” and “heap” have no definition in C++, memory is not required to be allocated anywhere.

    With you example here:

    MyClass myclass = MyClass();
    

    You are initializing myclass via copy constructor to the temporary object. myclass (and the temporary) has automatic storage which you can consider being allocated on “the stack” if that makes you happy.

    Copy elision is allowed in this case, essentially optimizing it to MyClass myClass, however note that it cannot always do this, such as when the copy constructor is private.

    Here is an example that you can test:

    struct obj {
      static int c;
      int myc;
      obj() : myc(c++) {
        std::cout << "ctor of " << myc << '\n';
      }
      obj(const obj&) : myc(c++){
        std::cout << "copy ctor of " << myc << '\n';
      }
      ~obj() {
        std::cout << "dtor of " << myc << '\n';
      }
    };
    int obj::c = 1;
    
    int main(int argc, char** argv)
    {
      obj x = obj();
    }
    

    If the copy is elided you will see:

    ctor of 1
    dtor of 1
    

    Otherwise (gcc option -fno-elide-constructors to prevent the elision happening):

    ctor of 1
    copy ctor of 2
    dtor of 1
    dtor of 2
    

    Additionally, making the copy constructor private will give a compiler error.

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

Sidebar

Related Questions

I am learning C++ exceptions and I would like some clarification of the scenario:
I got nabbed by the following bug again and would like some clarification to
OK n00b here with SOAP, Would like some clarification on how to use SOAP.
We are designing a new database and I would like some input in where
I would like some clarification on what the code in front of the HTML
I just have a small question that I would like some clarification about, if
I would like to get some clarification to this answer -> Nested stored procedures
I would like some advice on the best approach to use in the following
I would like some of my preferences to have icons, like the Settings app.
I am developing a site and i would like some simple markup. I 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.