answersLogoWhite

0


Best Answer

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

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Static memory allocation is used by almost all variables (not using word new). When you have a static variable, it keeps reserved for its purposes memory until the program where it was declared does not finish its execution.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

In C++, we work with three types of memory (it's the same memory, it's only the way in which it is used that changes). First we have static memory. This is where all our static variables, global variables and constants are allocated. Static memory is fixed-length and is divided into two subsections: initialised memory and uninitialised memory. The initialised segment typically contains all constants and all variables that have a non-zero initial value, the values of which are obtained from a "carbon-copy" image stored in the executable. The uninitialised segment makes up the remainder of static memory and is allocated at load time from a size value stored within the executable. Although the uninitialised segment contains a mixture of both uninitialised and zero-initialised variables and constants, the entire segment is zero-initialised at load time. Being fixed length, the memory offsets of every variable and constant are known at compile time.

Next we have function call stack memory (or simply stack memory). Every thread has its own independent stack and, like static memory, is of fixed length. Each stack is allocated as and when a thread is instantiated and released when the thread is destroyed. Every application has at least one thread, the main thread, which implements the entry point of the application (the global main function), thus every application has at least one stack. The stack is used to implement the function call and return mechanism, to store local variables and to implement exception handling. It is called a stack because the memory is pre-allocated, we simply need to keep track of where the top of the stack is (the next available storage address). When we invoke a function, the stack pointer is adjusted to accommodate the return address of the calling code along with the function's arguments (if any) and local variables (if any). Thus the first "stack frame" in the main thread's stack will hold the return address of the execution environment, the main function's arguments (such as the command line switches passed from the execution environment) and the local variables of the main function.

Finally, we have the heap or free store. This is memory that can be programmatically allocated as and when required at runtime. In C++ we use the new and delete operators to allocate and release memory on the heap. We can also use the C-style malloc() and free() functions to allocate and release heap memory, however these functions know nothing about constructors, destructors or exception handling and are best avoided. Despite what C programmers may say, there's nothing to be gained from using these functions; they are no more or less efficient than operators new and delete.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

It's function malloc (or calloc, realloc).

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

With the new operator, or implicitly, in a block. In the former case, allocation is generally from the heap. In the latter case, allocation is generally from the stack.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How is memory allocated for structres in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.