Code Segment Question
-
struct Inst { char Iname[20]; char Office[10]; float salary; int InstId; char phone[10]; } void main() { Inst ISInst1; Inst* InstPtr; . . } How much space will the ISInst1 variable take up in main memory. How much space will InstPtr take? Can you explain this to me? Loli10
-
struct Inst { char Iname[20]; char Office[10]; float salary; int InstId; char phone[10]; } void main() { Inst ISInst1; Inst* InstPtr; . . } How much space will the ISInst1 variable take up in main memory. How much space will InstPtr take? Can you explain this to me? Loli10
This question does not have a single answere since it depends on the target machine, your compiler, compiler settings... If you run on Win32 and use VC compiler with standard settings I'd say your struct will take up 52 bytes of memory. There are 4 'hidden' bytes in there. two comes after Office, and 2 after phone. This is due to byte alignment. For efficiency, datatypes like float and int are put an a 4 byte boundary. This allows the CPU to fetch your data in a single call. If you want to check the size of your datatypes you should use the operator sizeof(), e.g. int instsize = sizeof(struct Inst). This will also show that your pointer is 4 bytes (again depending on the target machine) If you are familiar with pragmas you could try putting #pragma pack(1) before you declare your struct and se what happens. Hope that helps.
-
This question does not have a single answere since it depends on the target machine, your compiler, compiler settings... If you run on Win32 and use VC compiler with standard settings I'd say your struct will take up 52 bytes of memory. There are 4 'hidden' bytes in there. two comes after Office, and 2 after phone. This is due to byte alignment. For efficiency, datatypes like float and int are put an a 4 byte boundary. This allows the CPU to fetch your data in a single call. If you want to check the size of your datatypes you should use the operator sizeof(), e.g. int instsize = sizeof(struct Inst). This will also show that your pointer is 4 bytes (again depending on the target machine) If you are familiar with pragmas you could try putting #pragma pack(1) before you declare your struct and se what happens. Hope that helps.
Good single answer :-) Regards, Alvaro Always do right. This will gratify some people, and astonish the rest. - Mark Twain
-
struct Inst { char Iname[20]; char Office[10]; float salary; int InstId; char phone[10]; } void main() { Inst ISInst1; Inst* InstPtr; . . } How much space will the ISInst1 variable take up in main memory. How much space will InstPtr take? Can you explain this to me? Loli10
The easiest way to find out would be to use the sizeof operator:
int viSize1 = sizeof(ISInst1);
int viSize2 = sizeof(InstPtr);------------------------ Derek Waters derek@lj-oz.com