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. could some one give me a detail of this the result??I don not know why,more detail more better

could some one give me a detail of this the result??I don not know why,more detail more better

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 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.
  • W Offline
    W Offline
    wbgxx
    wrote on last edited by
    #1

    #include <iostream>
    using namespace std;
    #pragma pack(8)

    struct example1
    {
    short a;
    long b;
    };

    struct example2
    {
    char c;
    example1 struct1;
    short e;
    };

    #pragma pack()

    int main()
    {
    example2 struct2;
    cout << sizeof(example1) << endl; //8
    cout << sizeof(example2) << endl; //16
    cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
    << endl; //4

       getchar();
    

    }

    C _ W 3 Replies Last reply
    0
    • W wbgxx

      #include <iostream>
      using namespace std;
      #pragma pack(8)

      struct example1
      {
      short a;
      long b;
      };

      struct example2
      {
      char c;
      example1 struct1;
      short e;
      };

      #pragma pack()

      int main()
      {
      example2 struct2;
      cout << sizeof(example1) << endl; //8
      cout << sizeof(example2) << endl; //16
      cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
      << endl; //4

         getchar();
      

      }

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      I am not sure what your problem is, but i will guess.

      wbgxx wrote:

      cout << sizeof(example1) << endl; //8

      - here you wonder why you get 8 instead of 6 when short is suposed to be 2 bytes and long is suposed to be 4 bytes, so 2+4 is 6, not 8.

      wbgxx wrote:

      cout << sizeof(example2) << endl; //16

      - here you wonder why you get 16 when char is 1 byte, short is 2 byte and your struct1 is 8 bytes so you expect it to be 11 bytes altogether, not 16.

      wbgxx wrote:

      cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2) << endl; //4

      - here you wonder why you get 4 bytes instead of 1 byte, since there's only a char infront of the struct2 member which is suposed to be 1 byte long, not 4. If i am right then see this[^] and this[^].

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

      1 Reply Last reply
      0
      • W wbgxx

        #include <iostream>
        using namespace std;
        #pragma pack(8)

        struct example1
        {
        short a;
        long b;
        };

        struct example2
        {
        char c;
        example1 struct1;
        short e;
        };

        #pragma pack()

        int main()
        {
        example2 struct2;
        cout << sizeof(example1) << endl; //8
        cout << sizeof(example2) << endl; //16
        cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
        << endl; //4

           getchar();
        

        }

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Code-o-mat has pointed in the right direction. It has everything to do with the structure packing and the controlling #pragma pack directive. In you're example it says #pragma pack(8). This means pack the struct on 8 byte boundaries. For the structure example1, short is 2 bytes and long is 4 bytes. Here the compiler will reserve 4 bytes for the short so that it becomes 8 bytes total. If you change the pragma to #pragma pack(1), you should get the results as 6 and 9.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        1 Reply Last reply
        0
        • W wbgxx

          #include <iostream>
          using namespace std;
          #pragma pack(8)

          struct example1
          {
          short a;
          long b;
          };

          struct example2
          {
          char c;
          example1 struct1;
          short e;
          };

          #pragma pack()

          int main()
          {
          example2 struct2;
          cout << sizeof(example1) << endl; //8
          cout << sizeof(example2) << endl; //16
          cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
          << endl; //4

             getchar();
          

          }

          W Offline
          W Offline
          wbgxx
          wrote on last edited by
          #4

          cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
          << endl;

          and what is (unsigned int)(&struct2.struct1)means?

          I 1 Reply Last reply
          0
          • W wbgxx

            cout << (unsigned int)(&struct2.struct1) - (unsigned int)(&struct2)
            << endl;

            and what is (unsigned int)(&struct2.struct1)means?

            I Offline
            I Offline
            Iain Clarke Warrior Programmer
            wrote on last edited by
            #5

            wbgxx wrote:

            (unsigned int)(&struct2.struct1

            You first take the address of a variable... Then cast it to a number, so when cout "prints" it, it will be shown as a number, not some fancier thing. As the answers to your (slightly) later question said, this is very basic C/C++ stuff - a book is better able to help you than a message board. Pointers are one of that concepts that can explained for ages, but you'll wake up one morning and go "Oooooh!". Think of the difference between the place you live - and address written on a postcard to you. Good luck with your journey. Iain.

            I have now moved to Sweden for love (awwww).

            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