C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - True / False Questions (Q.No. 11)
11.
On declaring a structure 0 bytes are reserved in memory.
Discussion:
20 comments Page 1 of 2.
Jayesh Rane said:
5 years ago
When a structure is defined, memory is not allocated at that time. Only when a variable of that particular structure is created then only memory is allocated to that structure.
Tanikachalamaji266@gmail.com said:
5 years ago
When structure variable is created then, memory will be allocated.
Noel said:
7 years ago
I think the answer is incorrect:
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
The reason for above error is simple when a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
The reason for above error is simple when a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created.
Chandramani said:
9 years ago
Friends,
The size of structure variable is the sum of the size of all its members.
Also, the size of the structure is not fixed it can be defined by the programmer as its requirement.
And by default, the size of the structure is "zero".
The size of structure variable is the sum of the size of all its members.
Also, the size of the structure is not fixed it can be defined by the programmer as its requirement.
And by default, the size of the structure is "zero".
Abc said:
1 decade ago
Thank you very much @Sampath. Now I clearly get the def 's of structure declaration and definition.
Debendra nath tiwary said:
1 decade ago
Guys the you all are right i.e. if no objects declared then no space is allocated. But the problem here is the compiler --- as structure is user defined type the compiler may reserve some space (which also depends on the compiler) for some registration purpose.
Sagar said:
1 decade ago
Yes sizeof(struct) is giving memory of data types declared in it. It doesn't mean that memory is allocated in stack section.
Sumit said:
1 decade ago
sizeof() gives the size of variable of that type.
eg:- sizeof(int) gives 4 byte.
Does not mean that the memory is reserved.
So how can we check that the memory is reserved for structure.
eg:- sizeof(int) gives 4 byte.
Does not mean that the memory is reserved.
So how can we check that the memory is reserved for structure.
Ritesh_IIITA said:
1 decade ago
Friends try to run this code:
#include<stdio.h>
int main()
{
struct emp{
int age;
char name[20];
};
printf("\n\tsize of structure: %d",sizeof(struct emp));
return 0;
}
Output:
size of structure: 24
Process exited normally.
Press any key to continue . . .
Therefore it is proved that memory is allocated at the time of structure declaration.
#include<stdio.h>
int main()
{
struct emp{
int age;
char name[20];
};
printf("\n\tsize of structure: %d",sizeof(struct emp));
return 0;
}
Output:
size of structure: 24
Process exited normally.
Press any key to continue . . .
Therefore it is proved that memory is allocated at the time of structure declaration.
Sampath said:
1 decade ago
Guys this is the meaning of declaration and definition of structure.
struct info
{
int i;
int j; /* this definition of structure */
};
struct info b; /* this is declaration */
struct into
{
int i;
int j; /* here definition and declarations are
}a; simultaneously happening */
So memory will be allocated when structure is declared.
Still didn't get...:)?
int g;
Here int is data type so g is declared as an integer.
In the same way in the above example struct info is my derived data type, before declaration of any variable to my derived data type, i should define it, later i should declare any variable to it.
I hope you understand now clearly about the meaning of definition and declaration in the context of structure.
struct info
{
int i;
int j; /* this definition of structure */
};
struct info b; /* this is declaration */
struct into
{
int i;
int j; /* here definition and declarations are
}a; simultaneously happening */
So memory will be allocated when structure is declared.
Still didn't get...:)?
int g;
Here int is data type so g is declared as an integer.
In the same way in the above example struct info is my derived data type, before declaration of any variable to my derived data type, i should define it, later i should declare any variable to it.
I hope you understand now clearly about the meaning of definition and declaration in the context of structure.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers