Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Code Segment Question

Code Segment Question

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancecareer
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Loli10
    wrote on last edited by
    #1

    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

    N D 2 Replies Last reply
    0
    • L 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

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • N Niklas L

        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.

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        Good single answer :-) Regards, Alvaro Always do right. This will gratify some people, and astonish the rest. - Mark Twain

        1 Reply Last reply
        0
        • L 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

          D Offline
          D Offline
          Derek Waters
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups