answersLogoWhite

0

Memory is allocated to struct types exactly the same way as memory is allocated to class types. Memory is allocated to each member variable in the same order as they are declared, according to the size of each member plus any padding required for alignment purposes (which is typically the word size of the architecture).

The least-derived base class is always allocated first and, where multiple-inheritance is employed, the order of inheritance in the derived class declaration determines the order of allocation for its base classes. However, the order of the base classes has no effect upon the combined size of those base classes, only upon the order in which they are allocated.

Note that in order to minimise wasted memory in each individual class, it is generally best to declare class member variables in descending order of size. Consider the following, where base1 and base2 have the exact same member types but in different orders, such that base2 occupies less memory than base1.

derived has no members save those it inherits from base1 and base2, thus its total size is the sum of its base classes.

#include<iostream>

struct base1

{

char a;

int b;

char c;

char d;

};

struct base2

{

int e;

char f;

char g;

char h;

};

struct derived: public base1, public base2 {};

int main()

{

std::cout<<"base1 is "<<sizeof(base1)<<" bytes"<<std::endl;

std::cout<<"base2 is "<<sizeof(base2)<<" bytes"<<std::endl;

std::cout<<"derived is "<<sizeof(derived)<<" bytes"<<std::endl;

}

base1 is 12 bytes

base2 is 8 bytes

derived is 20 bytes

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.


How c plus plus objects are allocated memory?

With the new operator.myclass myclasspointer = new myclass;...use the classdelete myclasspointer;


What are new and delete operators in c plus plus?

New and Delete are the memory management operators in c++,like c language we use malloc() and calloc() functions to allocate memory and free() functiong to release the memory similarily we use new to allocate memory in C++ and Delete to release the allocated memory....


Where the heap memory is allocated in c?

Main Memory (RAM).


C plus plus uses dynamic memory management?

No, C++ does not use dynamic memory management. The programmer is entirely responsible for releasing dynamic memory when it is no longer required. When static objects fall from scope, their destructors are called automatically, but there is no automatic garbage collection for dynamic objects. Allocated memory remains allocated until the programmer manually releases it, or the thread that owns the memory is terminated.


Max memory allocated by malloc in c?

Memory is allocated by malloc from the heap.... so max mem = size of heap that is free...


What does malloc return in C and C plus plus?

Address of the allocated area, or NULL.


At what time the memory is allocated for variable in c and c?

Static memory allocation occurs at compile time where as dynamic memory allocation occurs at run time.


When you call new will it internally call delete in c plus plus?

No. Calling new returns a pointer to allocated memory. If you re-use a pointer to store the return value, then you must release the memory that it previously pointed at, either by deleting the pointer, or by maintaining a separate pointer to the original memory. Calling new will not release the current memory for you.


What are the automatic varriable in c?

An automatic variable is activated and has its memory allocated in the block in which it is declared. The memory and contents will be deleted when the block is exited.


How is memory allocated in c program for a character data type?

One byte for every character.


What is dynamic mamory?

Dynamic memory refers to memory that is allocated and deallocated during program execution, as opposed to static memory which is allocated at compile time. In C and C++, dynamic memory allocation is done using functions like malloc() and free(), allowing for flexibility in managing memory resources at runtime. However, improper use of dynamic memory can lead to memory leaks or segmentation faults.