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. sizeof()

sizeof()

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
2 Posts 2 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.
  • Y Offline
    Y Offline
    y_seval
    wrote on last edited by
    #1

    I have problem with sizeof() function in my VC++ 6.0 compiled project struct A { short b; char c; }a; int t = sizeof(a); gives me t=4; ---------------------------------------- I think it have to be 3 struct A { char c; }a; int t = sizeof(a); gives me t=1; all right struct A { short b; }a; int t = sizeof(a); gives me t=2; all right I think that I have some problem with my project settings but I do not what. In some other projct the sizeof() works in way a want it gives 3 for the structure above. Can someoen explan the reason of the sizeof() function behavier. Thank you in advance, Seval YILMAZ

    Z 1 Reply Last reply
    0
    • Y y_seval

      I have problem with sizeof() function in my VC++ 6.0 compiled project struct A { short b; char c; }a; int t = sizeof(a); gives me t=4; ---------------------------------------- I think it have to be 3 struct A { char c; }a; int t = sizeof(a); gives me t=1; all right struct A { short b; }a; int t = sizeof(a); gives me t=2; all right I think that I have some problem with my project settings but I do not what. In some other projct the sizeof() works in way a want it gives 3 for the structure above. Can someoen explan the reason of the sizeof() function behavier. Thank you in advance, Seval YILMAZ

      Z Offline
      Z Offline
      ZoogieZork
      wrote on last edited by
      #2

      struct elements are aligned to boundaries (particularly word boundaries) to improve performance. Use #pragma pack(push,1) and #pragma pack(pop) to align a struct on byte boundaries:

      #pragma pack(push,1)
      struct A
      {
      short b;
      char c;
      }a;
      #pragma pack(pop)

      - Mike

      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