sizeof(struct emp) = 0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sophia.agnes@gmail.com

    sizeof(struct emp) = 0

    Dear all,

    why the following program is giving o/p as

    sizeof(struct emp) = 0

    #include<stdio. h>
    #include<stdlib .h>

    struct emp
    { };

    int main(void)
    {
    printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

    puts ("");
    return (EXIT_SUCCESS);
    }

    i have seen this same code giving o/p as 1 in other compilers
  • =?UTF-8?q?Harald_van_D=C4=B3k?=

    #2
    Re: sizeof(struct emp) = 0

    On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
    Dear all,
    >
    why the following program is giving o/p as
    >
    sizeof(struct emp) = 0
    >
    #include<stdio. h>
    #include<stdlib .h>
    >
    struct emp
    { };
    This isn't valid C. C requires at least one member in every structure.
    That said, ...
    i have seen this same code giving o/p as 1 in other compilers
    ....why would you expect any bytes to be required to store nothing?
    Shouldn't you be asking why other compilers make sizeof(struct emp)
    anything other than zero? Or do you already know why?

    Comment

    • Joachim Schmitz

      #3
      Re: sizeof(struct emp) = 0

      sophia.agnes@gm ail.com wrote:
      Dear all,
      >
      why the following program is giving o/p as
      >
      sizeof(struct emp) = 0
      >
      #include<stdio. h>
      #include<stdlib .h>
      Some white space might improve readability
      >
      struct emp
      { };
      Gives me an error "expected a declaration"
      int main(void)
      {
      printf("\n sizeof(struct emp) = %d", sizeof(struct emp));
      sizeof() returns size_t, %d expects int.
      >
      puts ("");
      Not needed if the printf string would end with a \n
      return (EXIT_SUCCESS);
      () not needed, return is not a function
      }
      >
      i have seen this same code giving o/p as 1 in other compilers
      Which compiler and where they called in conforming mode? Conforming to which
      version of the standard?

      Bye, Jojo


      Comment

      • Serve Laurijssen

        #4
        Re: sizeof(struct emp) = 0


        <sophia.agnes@g mail.comschreef in bericht
        news:bbfcb9a6-d023-454d-9090-af47b8a7e52e@s1 3g2000prd.googl egroups.com...
        Dear all,
        >
        why the following program is giving o/p as
        >
        sizeof(struct emp) = 0
        >
        #include<stdio. h>
        #include<stdlib .h>
        >
        struct emp
        { };
        >
        int main(void)
        {
        printf("\n sizeof(struct emp) = %d", sizeof(struct emp));
        >
        puts ("");
        return (EXIT_SUCCESS);
        }
        >
        i have seen this same code giving o/p as 1 in other compilers
        empty structs is illegal C
        It is legal C++ though, there the size of an empty struct is always 1. Maybe
        you were confused with that?

        Comment

        • Keith Thompson

          #5
          Re: sizeof(struct emp) = 0

          Harald van Dijk <truedfx@gmail. comwrites:
          On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
          >Dear all,
          >>
          >why the following program is giving o/p as
          >>
          > sizeof(struct emp) = 0
          >>
          >#include<stdio .h>
          >#include<stdli b.h>
          >>
          >struct emp
          >{ };
          >
          This isn't valid C. C requires at least one member in every structure.
          That said, ...
          >
          >i have seen this same code giving o/p as 1 in other compilers
          >
          ...why would you expect any bytes to be required to store nothing?
          Shouldn't you be asking why other compilers make sizeof(struct emp)
          anything other than zero? Or do you already know why?
          Padding.

          --
          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • =?UTF-8?q?Harald_van_D=C4=B3k?=

            #6
            Re: sizeof(struct emp) = 0

            On Sun, 10 Feb 2008 12:36:05 -0800, Keith Thompson wrote:
            Harald van Dijk <truedfx@gmail. comwrites:
            >On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
            >>Dear all,
            >>>
            >>why the following program is giving o/p as
            >>>
            >> sizeof(struct emp) = 0
            >>>
            >>#include<stdi o.h>
            >>#include<stdl ib.h>
            >>>
            >>struct emp
            >>{ };
            >>
            >This isn't valid C. C requires at least one member in every structure.
            >That said, ...
            >>
            >>i have seen this same code giving o/p as 1 in other compilers
            >>
            >...why would you expect any bytes to be required to store nothing?
            >Shouldn't you be asking why other compilers make sizeof(struct emp)
            >anything other than zero? Or do you already know why?
            >
            Padding.
            In C, padding is not allowed at the immediate start of a structure.

            More seriously, padding is used to ensure the structure's members are
            properly aligned, and there's no problem with alignment of an empty
            structure's members. There's also no problem with an array of zero-byte
            objects. They all have the same address, so they are all identically
            aligned.

            Of course, the fact that they all have the same address is why a certain
            other language places extra requirements on empty structures, but
            extensions by any C compiler don't have to follow rules for other
            languages.

            Comment

            • Jack Klein

              #7
              Re: sizeof(struct emp) = 0

              On Sun, 10 Feb 2008 11:53:57 +0100, "Serve Laurijssen" <ni@hao.com>
              wrote in comp.lang.c:
              >
              <sophia.agnes@g mail.comschreef in bericht
              news:bbfcb9a6-d023-454d-9090-af47b8a7e52e@s1 3g2000prd.googl egroups.com...
              Dear all,

              why the following program is giving o/p as

              sizeof(struct emp) = 0

              #include<stdio. h>
              #include<stdlib .h>

              struct emp
              { };

              int main(void)
              {
              printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

              puts ("");
              return (EXIT_SUCCESS);
              }

              i have seen this same code giving o/p as 1 in other compilers
              >
              empty structs is illegal C
              It is legal C++ though, there the size of an empty struct is always 1. Maybe
              you were confused with that?
              <off-topic>

              The size of an empty struct in C++ is greater than 0. There is
              neither requirement nor guarantee that it be exactly equal to 1.

              </off-topic>

              --
              Jack Klein
              Home: http://JK-Technology.Com
              FAQs for
              comp.lang.c http://c-faq.com/
              comp.lang.c++ http://www.parashift.com/c++-faq-lite/
              alt.comp.lang.l earn.c-c++

              Comment

              • karthikbalaguru

                #8
                Re: sizeof(struct emp) = 0

                On Feb 10, 2:30 pm, sophia.ag...@gm ail.com wrote:
                Dear all,
                >
                why the following program is giving o/p as
                >
                 sizeof(struct emp) =  0
                >
                #include<stdio. h>
                #include<stdlib .h>
                >
                struct emp
                { };
                Not allowed in C .
                There may be unnamed padding within a structure object, but not at its
                beginning.
                There may be unnamed padding at the end of a structure or union.
                Refer -http://c0x.coding-guidelines.com/6.7.2.1.html
                >
                int main(void)
                {
                  printf("\n sizeof(struct emp) =  %d", sizeof(struct emp));
                >
                  puts ("");
                  return (EXIT_SUCCESS);
                >
                }
                >
                i have seen this same code giving o/p as 1 in other compilers

                Comment

                Working...