इस page में, हम C ++ में structure size का पता लगाएंगे,
जैसा कि पहले उल्लेख किया गया है, हम sizeof
operator से किसी भी data-type या variable के size का पता लगा सकते हैं।
नीचे दिए गए program में sizeof()
operator का भी उपयोग किया गया है।
किसी structure का size जानने के लिए, हम सबसे पहले structure-variable declare करते हैं। इस structure-variable का size स्वयं structure का size है।
यहाँ एक उदाहरण है,
found out structure size using sizeof() in C++
#include<iostream.h>
#include<conio.h>
struct get_size
{
int a; // 2byte
float b; //4 byte
char c; //1 byte
} x;
void main()
{
cout<<"size of structure(in byte): "<<sizeof(x);
getch();
}
OUTPUT
size of structure(in byte): 7
Explanation
program में, structure में तीन प्रकार के data -member (इंट, फ्लोट और चार) declared हैं और जैसा की हम जानते हैं कि ये data -typememory में कितना space reserve करते हैं।
तो यहां पर सभी data -member का कुल size , structure के size बराबर होगा-
int + float + char = structure variable size
अर्थात
2 + 4 + 1 = 7 bytes,
structure और class ज्यादातर मामलों में समान होते हैं, इसलिए यह class के लिए भी लागू होगा।
✍structure और class हमेशा अपने variable या object declared होने के बाद ही memory में space reserve करते हैं। variable declaration के दौरान दोनों memory में space reserve नहीं करेंगे।
एक union का memory में space लेने का तरीका अलग होता है यहाँ देखे।
Related exercise: