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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:37:56+00:00 2026-05-26T06:37:56+00:00

I am working on designing a small system and am wondering about a nuance

  • 0

I am working on designing a small system and am wondering about a nuance of how memory is allocated for derived classes.

If I have two classes

class foo {
public: int a;
Foo(): a(0) {};
 };
class bar : public foo {
public: int b;
Bar() : Foo() , b(0) {}
};

and then do something like this in another method

Bar* purple = new Bar();

I know that both constructors will be called (for Foo and Bar) but how will the memory be allocated. Is the memory for both ‘a’ and ‘b’ allocated at the same time, as they are both a part of Bar class, or is the memory allocated for ‘a’ when the Foo constructor is called and the memory for ‘b’ allocated when the Bar constructor is called.

Thanks in advance for the help.

  • 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-26T06:37:57+00:00Added an answer on May 26, 2026 at 6:37 am

    new Bar is just one contiguous allocation, as is new Bar[n].

    It may help to think of like this:

    Bar* purple = new Bar();
    

    is similar to:

    Bar* purple = (Bar*)malloc(sizeof(Bar));
    

    Behind the scenes, the constructor is also called, and in some cases the allocation may be greater than sizeof(Bar); new[] is a common case where it is greater because the array’s count is secretly stored in the allocation in many implementations.

    Update

    The sizeof Bar itself is the size required to store the type; its members, its bases, a pointer to its vtable, etc. – all with native alignment (by default). The compiler may also introduce padding.

    Therefore, the single contiguous allocation will be sufficiently large enough to hold everything both foo and bar require, including foo::a and bar::b.

    In more detail: where are member-vars of a class on the heap stored?

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

Sidebar

Related Questions

I have a database I'm working on designing using Access 2007 and I'm wondering
I'm working on designing an EDI system for two companies: Company A and Company
I'm working on designing a multi-tiered app in Perl and I'm wondering about the
I'm working on designing a friend system for my site. I have a USERS
I have been working on designing a file server that could take the load
So I have been working with javascript for a website I am designing, shocker
I have been working on a small Plagiarism detection engine which uses Idea from
I am working on designing an Android application, and I have several questions I
So, I'm working on designing a class wherein if certain arguments to certain methods
I'm working on designing a game with multiple levels and was wondering if I

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.