answersLogoWhite

0


Best Answer

Depend on the elements of you struct. Say i have struct a { int a, int b}. Total size is : 4 + 4 = 8 bytes.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the total size of memory allocated for structure variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How is a c plus plus program stored in the memory?

C++ programs are not stored in memory (RAM) they are stored on mass storage devices (usually disk drives). When compiled, they produce machine code programs which contain machine instructions and their operands. These are also stored on mass storage devices, but when loaded into memory the machine instructions are executed by the CPU.


How object take place in memory in case of oops?

Objects are stored in memory according to their members. They are very similar to struct types, insofar as the total memory assigned to an object is equal to the total size of all its members, plus any padding required for member alignment. If the object contains pointers, the memory allocated to those pointers will reside elsewhere, outside of the object, regardless of whether that memory belongs to the object or not (it is not included in the object's memory footprint). Additional memory is also set aside for the v-table if the class contains any virtual methods.


How memory is allocated for array type variable?

For any given type, T, a definition of type T[n] will allocate an array of n elements of type T in contiguous memory, for all n>=0. The total size of the allocation will be equivalent to n * sizeof(T). Elements of type T can be accessed by index, in the range 0 to n-1. These indices allow the compiler to compute the offset from the start of the array, such that index i refers to the address i * sizeof(T). Arrays can be either fixed-size or variable-length and can be allocated on the stack or the heap but only fixed-size arrays can be allocated statically. The following shows all the possibilities: int a[10]; // fixed-size, static allocation, global scope void f(int n) { int b[10]; // fixed-size, stack allocation, local scope int c[n]; // variable-length, stack allocation, local scope int* d = new int[10]; //fixed-size, heap allocation int* e = new int[n]; // variable-length, heap allocation // ... delete [] e; delete [] d; } Note that the arrays referred to by the pointers d and e are anonymous arrays (as are all heap allocations). It is important that we maintain at least one reference to a heap allocation in order to release that memory back to the system when we no longer require it. The pointers d and e will automatically fall from scope when the function ends, but the memory they refer to remains, unless we explicitly delete that memory. Ideally, the function that allocates heap memory should also release that memory, however so long as we maintain a reference to that memory (such as through a global pointer), any function can release it. This can lead to problems such as resource leaks as ownership of the memory is unspecified; any code can take ownership of the memory at any time. This is particularly problematic in multi-threaded applications where two threads may attempt to take ownership at the same time. If one thread releases the memory while another is still accessing that memory, undefined behaviour will occur. To avoid this, pointers to heap allocations are best encapsulated within a resource handle, a class that takes ownership of the memory. In C++, std::vector provides a thread-safe resource handle to variable-length arrays.


Is it better to use malloc or calloc to allocate memory?

In general using malloc is faster, since calloc initializes the allocated memory to contain all zeroes. If this is what you want, however, then calloc can be used. The results can vary among different operating systems and environments, though. Memory allocation in an OS that uses floating blocks in heaps, such as Microsoft Windows and MacOS, should use the OS-native memory allocators instead. "Use malloc() almost always and calloc() almost never." The reason is that the initialization to zero that calloc() performs is usually not very helpful: - The initialization to "all-bits-zero" is not necessarily the same as initialization to "all-data-zero." C says very little about the representation of values in memory, nothing at all for floating-point or pointer values. On many machines all-bits-zero representations will in fact correspond to f.p. zeroes or null pointers, but this is not guaranteed by the language and there have been machines where the correspondence did not hold. If you get in the habit of using calloc() to initialize f.p. and pointer items, you may be heading for trouble. - Usually, one allocates a chunk of dynamic memory in order to store something in it -- and when you store something in it, you'll overwrite whatever was there before. Thus, the initialization performed by calloc() is usually not needed anyhow. There are occasional exceptions where all- bits-zero initialization is helpful, but they are unusual.


How is memory allocated for structres in C plus plus?

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

Related questions

Why do you go for structure and union?

Structure and union are a form of encapsulating similar data elements.Structure:Whenever a structure is declared, then space equal to the total size of the elements defined inside the structure is allocated. Hence each individual element has its own memory space. Hence structures are commonly used when there is usage for more than one element declared inside the struct at a time.Union:Whenever an union is declared, then space equal to the size of the maximum element defined inside the union is allocated. Hence the individual elements share a common memory space. Hence union is commonly used when there is usage for only one element declared inside the union at a time.


How many bytes are allocated when a variable is declared in union?

The total number of bytes allocated to the union will be the same number as would have been allocated if instead of the union was declared the largest member of the union. For example, if you declared: union myUnion { char c; int i; double d; } u;, then the space allocated to u will be the size of a double.


Definition of internal fragmentation and external fragmentation?

External Fragmentation: External Fragmentation happens when a dynamic memory allocation algorithm allocates some memory and a small piece is left over that cannot be effectively used. If too much external fragmentation occurs, the amount of usable memory is drastically reduced. Total memory space exists to satisfy a request, but it is not contiguous. Internal Fragmentation: Internal fragmentation is the space wasted inside of allocated memory blocks because of restriction on the allowed sizes of allocated blocks. Allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used


How is a c plus plus program stored in the memory?

C++ programs are not stored in memory (RAM) they are stored on mass storage devices (usually disk drives). When compiled, they produce machine code programs which contain machine instructions and their operands. These are also stored on mass storage devices, but when loaded into memory the machine instructions are executed by the CPU.


Difference between internal fragmentation and external fragmentation in operating system?

Fragmentation occurs in a dynamic memory allocation system when many of the free blocks are too small to satisfy any request. External Fragmentation: External Fragmentation happens when a dynamic memory allocation algorithm allocates some memory and a small piece is left over that cannot be effectively used. If too much external fragmentation occurs, the amount of usable memory is drastically reduced. Total memory space exists to satisfy a request, but it is not contiguous. Internal Fragmentation: Internal fragmentation is the space wasted inside of allocated memory blocks because of restriction on the allowed sizes of allocated blocks. Allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used


How object take place in memory in case of oops?

Objects are stored in memory according to their members. They are very similar to struct types, insofar as the total memory assigned to an object is equal to the total size of all its members, plus any padding required for member alignment. If the object contains pointers, the memory allocated to those pointers will reside elsewhere, outside of the object, regardless of whether that memory belongs to the object or not (it is not included in the object's memory footprint). Additional memory is also set aside for the v-table if the class contains any virtual methods.


Formula for total variable cost?

Total Variable Cost = Number of Units * Variable cost per unit


What is pf usage?

It stands for page file usage. Page file usage is the variable amount of hard drive space that is dedicated to your total system memory.


How do you calculate total variable expenses in a contribution income statement?

Total variable cost is typically the sum of all variable labor, variable materials, and variable overhead expenses.


How memory is allocated for array type variable?

For any given type, T, a definition of type T[n] will allocate an array of n elements of type T in contiguous memory, for all n>=0. The total size of the allocation will be equivalent to n * sizeof(T). Elements of type T can be accessed by index, in the range 0 to n-1. These indices allow the compiler to compute the offset from the start of the array, such that index i refers to the address i * sizeof(T). Arrays can be either fixed-size or variable-length and can be allocated on the stack or the heap but only fixed-size arrays can be allocated statically. The following shows all the possibilities: int a[10]; // fixed-size, static allocation, global scope void f(int n) { int b[10]; // fixed-size, stack allocation, local scope int c[n]; // variable-length, stack allocation, local scope int* d = new int[10]; //fixed-size, heap allocation int* e = new int[n]; // variable-length, heap allocation // ... delete [] e; delete [] d; } Note that the arrays referred to by the pointers d and e are anonymous arrays (as are all heap allocations). It is important that we maintain at least one reference to a heap allocation in order to release that memory back to the system when we no longer require it. The pointers d and e will automatically fall from scope when the function ends, but the memory they refer to remains, unless we explicitly delete that memory. Ideally, the function that allocates heap memory should also release that memory, however so long as we maintain a reference to that memory (such as through a global pointer), any function can release it. This can lead to problems such as resource leaks as ownership of the memory is unspecified; any code can take ownership of the memory at any time. This is particularly problematic in multi-threaded applications where two threads may attempt to take ownership at the same time. If one thread releases the memory while another is still accessing that memory, undefined behaviour will occur. To avoid this, pointers to heap allocations are best encapsulated within a resource handle, a class that takes ownership of the memory. In C++, std::vector provides a thread-safe resource handle to variable-length arrays.


How do you calculate variable cost per unit?

Variable cost per unit = Total variable cost / total number of units manufactured


How do you calculate Total Cost without Total variable cost?

To calculate the Total Cost without Total variable cost, one should estimate for the variables or substitute for the variables with a variable such as X or Y and then solve for the approximate total cost.