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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:17:18+00:00 2026-06-15T13:17:18+00:00

Possible Duplicate: Are data members allocated in the same memory space as their objects

  • 0

Possible Duplicate:
Are data members allocated in the same memory space as their objects in C++?

If I declare an object like this:

void main() {
  MyClass class;
}

it will be created automatically on the stack.

What happens if I do this:

class MySecondClass {
  private:
    MyClass class;
}

Will the member be created on the stack? If so, what happens if MySecondClass is created via new? Will this member still be on the stack?

  • 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-15T13:17:19+00:00Added an answer on June 15, 2026 at 1:17 pm

    Will the member be created on the stack?

    Yes.

    If so, what happens if MySecondClass is created via new? Will this member still be on the stack?

    No. It will be stored along with the rest of the object, “on the heap” or wherever your free store is implemented, or wherever the object gets dynamically allocated (which could be some memory pool or something else).

    It’s worth noting here that the terms “stack” and “heap” are generally mis-used. What you’re really asking is the following:

    Does the member have automatic storage duration? Yes.

    Will it do so even when the encapsulating object has dynamic storage duration? No — the dynamicness of the encapsulating object is, in a sense, “inherited”.

    [C++11: 3.7.5]: The storage duration of member subobjects, base class subobjects and array elements is that of their complete object (1.8).

    The practical location in memory in either case will be the stack and the free store (“heap”) respectively, and that doesn’t really matter.

    And, by the way, main must have int return type.

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

Sidebar

Related Questions

Possible Duplicate: Does this type of memory get allocated on the heap or the
Possible Duplicate: Does this type of memory get allocated on the heap or the
Possible Duplicate: Does Objective-C guarantee the initialization of interface member data? When I declare
Possible Duplicate: When to use virtual destructors? If all the data members of a
Possible Duplicate: Getting JavaScript object key list How to iterate json data in jquery
Possible Duplicate: Why isn't the const qualifier working on pointer members on const objects?
Possible Duplicate: Structure of a C++ Object in Memory Vs a Struct memory layout
Possible Duplicate: Size of a byte in memory - Java I read this in
Possible Duplicate: Member initialization of a data structure’s members EDIT: I typed the title
Possible Duplicate: Public Data members vs Getters, Setters In what cases should public fields

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.