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 not working

sizeof not working

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 Posters 11 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.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    I wrote this code:

    class ChessPiece
    {

    };

    int main()
    {
    cout << "size:" << sizeof(ChessPiece) << endl;
    }

    For some reason, it prints out 1 instead of 0. Anyone know why? Thanks.

    L J 2 Replies Last reply
    0
    • M mike7411

      I wrote this code:

      class ChessPiece
      {

      };

      int main()
      {
      cout << "size:" << sizeof(ChessPiece) << endl;
      }

      For some reason, it prints out 1 instead of 0. Anyone know why? Thanks.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Probably because there has to be some minimum size of a class. You could study the C++ standard to see why.

      1 Reply Last reply
      0
      • M mike7411

        I wrote this code:

        class ChessPiece
        {

        };

        int main()
        {
        cout << "size:" << sizeof(ChessPiece) << endl;
        }

        For some reason, it prints out 1 instead of 0. Anyone know why? Thanks.

        J Offline
        J Offline
        jeron1
        wrote on last edited by
        #3

        Perhaps take a look at this FAQ. Stroustrup: C++ Style and Technique FAQ[^]

        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

        J 1 Reply Last reply
        0
        • J jeron1

          Perhaps take a look at this FAQ. Stroustrup: C++ Style and Technique FAQ[^]

          "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          That is interesting. That page references a 'better' link. Same answer is there. Standard C++[^] Myself though if Stroustrop is the name then I would consider that better.

          J 1 Reply Last reply
          0
          • J jschell

            That is interesting. That page references a 'better' link. Same answer is there. Standard C++[^] Myself though if Stroustrop is the name then I would consider that better.

            J Offline
            J Offline
            jeron1
            wrote on last edited by
            #5

            jschell wrote:

            if Stroustrop is the name then I would consider that better.

            Agreed. :)

            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

            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