answersLogoWhite

0

All structures of the same type must be of the same size because they all have exactly the same member types. If they were all different sizes it would be impossible to know how much memory to allocate to them.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

What are the two ways of initializing a structure variable in c plus plus?

Two ways? There are at least four: 1) Zero the memory allocated to the structure. 2) Shallow-copy the memory from another structure of the same type. 3) Deep-copy the memory from another structure of the same type. 4) Set the individual members.


What is the largest amount of computer memory expressed in 1 byte?

i ask my self this same question... why is there no answer?


What is the role of pointer variable in data structures?

Structures are a type of variable, just as int and char are types of variable. All variables of the same type have one thing in common: their size is always the same. Typically, an int is 4 bytes, and a short is 2 bytes while a char is 1 byte. So a structure with 2 ints is therefore 8 bytes in length. A structure with an int and a char is 5 bytes, but structure member alignment will typically round this up to the nearest 4 bytes, padding any unused bytes (dependant upon your compiler's member alignment options). Regardless, a structure's size is known in advance by the number and type of its member variables. It's size is fixed. This is important when creating arrays of structures because every element in the array must be same size, and the size must be known in advance. This then makes it impossible to store variable length strings and other dynamic arrays within a structure. That is where pointer variables come in. Since all they store is a memory address, pointer variables are a fixed size (4 bytes on a 32-bit system). It really doesn't matter how much memory is allocated to that memory address, because that memory is not physically part of the structure -- only the pointer variable itself is a member of the structure. The pointer variable simply "points" to the memory address stored in it, and the type of the pointer determines how that memory is accessed, whether it is a pointer to a primitive variable type, an array of char or an array of another structure altogether. Ultimately, the structure is only concerned with the pointer variable, not what it points at. This then allows your structures to make better use of memory. There is no need to allocate an array far larger than you actually need, simply to cater for the "worst-case". Other uses for pointers within structures include the ability to point at other structures of the same type, such as is required in linked lists. You cannot do this with references because the reference must be completely defined in advance; a structure that references its own type would require infinite memory because that reference contains another reference, which contains another reference... But it can point at another instance of the structure, including itself, because pointers only need to be declared, not completely defined. The same applies to classes with member pointer variables. The only difference is that structures have public access by default while classes have private access by default. But classes can point to structures and vice versa. Apart from that, classes are just like any other variable type; they are a fixed size.


How many songs same as 1 byte?

A byte is a unit of computer memory equaling 8 bits. A bit is a one or a zero. A byte is essentially equal to one character, as it can store a value from 1 to 255. A byte is too small to store any songs. Perhaps the questioner means a megabyte(1,000,000 bytes) or a gigabyte(1,000,000,000 bytes)?


Difference between int a and extern int a. why the first one is definition while the second is declaration please explain?

The declaration 'int a' both declares the variable of 'a' and allocates memory for it. When you use 'extern' you are referring to a variable called 'a' that has its memory allocated in another module. The actual variable 'a' is not in the same compilation unit as the current one being compiled. Where the variable 'a' is located is resolved by the linker. When using 'extern' you state your intent to use a variable called 'a', but it doesn't reserve any memory for it in the current module.


How are we going to use pointer in a program?

A pointer is a variable used specifically to store a memory address. We say the variable "points to" the memory address because we can dereference the pointer to access the value stored at that address. The pointer's type determines how that dereferenced value will be interpreted. Being a variable, we can change the stored address and thus change which value we point at. This makes it possible for the same variable to refer to different objects in memory, which includes other pointer variables.


What is constant explain their storage representation with suitable example?

A constant is a variable that is immutable. The storage representation is exactly the same as for any other variable of the same type, the only difference is that all constants are allocated in the program's data segment (static memory).


In a criterion variable the same as a dependent variable?

Yes, criterion variable is the same as a dependent variable.


What are the advantages of union over structures?

union occupies a lesser memory space compared to structure as the same memory block is used for all the members in union


Do reference variables occupy memory in c?

All references must be non-null, therefore they will always have memory allocated to them. A reference is simply a non-null memory location that stores a specific type of variable, as determined by the reference's type. The reference name is an alias (a token) for the memory location, in much the same way that an array name is an alias for the starting address of the array. A pointer variable is different in that memory must be set aside for the pointer variable itself, in addition to the memory it actually points to. On a 32-bit system, 4 bytes must be allocated to every pointer variable, to store the memory address that they point to, which could be null. But references only occupy the memory they actually refer to, which can never be null (a null reference will in fact render the program invalid).


What happens if you attempt to use a variable before it has been initialized?

In C, uninitialized variables may contain any value, usually whatever happened to be in the same memory location before the memory was allocated to that function. This is a likely source of bugs, since it means that whatever the programmer meant for the variable to contain was not in it.


Is a continuous variable the same as a dependent variable?

definitely not. Dependant variable is a variable that you measure . A controlled variable is something that you keep the same in the experiment.