sizeof a relative pointer in a struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ManicQin

    sizeof a relative pointer in a struct

    Hi all.
    I'm trying to get the size of a variable in a struct by his relative
    postion i.e.

    ///
    #define offsetof(s,m) (size_t)&(((s *)0)->m)

    struct ThePimp{
    char rings[10];
    char blings[20];

    };

    int sizeOfBling = sizeof( (*char)&ThePimp +
    (char)offsetof( ThePimp,blings) );

    ///
    (If I have any syntax mistakes please ignore)

    You can easily predict that sizeOfBling will be 4... (sizeof(int*))
    Is there any way to acomplish it?

    Thanks
  • Noob

    #2
    Re: sizeof a relative pointer in a struct

    ManicQin wrote:
    #define offsetof(s,m) (size_t)&(((s *)0)->m)
    AFAIU, however offsetof is defined, it is defined in stddef.h

    $ cat foo.c
    #include <stdio.h /* printf */
    #include <stddef.h /* offsetof */
    struct ThePimp { char rings[10]; char blings[20]; };
    int main(void)
    {
    printf("%d\n", (int)offsetof(s truct ThePimp, blings));
    return 0;
    }

    $ gcc -std=c89 -pedantic -Wall -Wextra foo.c

    $ ./a.out
    10

    Comment

    • REH

      #3
      Re: sizeof a relative pointer in a struct

      On Mar 20, 11:39 am, ManicQin <Manic...@gmail .comwrote:
      Hi all.
      I'm trying to get the size of a variable in a struct by his relative
      postion i.e.
      >
      ///
      #define offsetof(s,m) (size_t)&(((s *)0)->m)
      >
      struct ThePimp{
      char rings[10];
      char blings[20];
      >
      };
      >
      int sizeOfBling = sizeof( (*char)&ThePimp +
      (char)offsetof( ThePimp,blings) );
      >
      ///
      (If I have any syntax mistakes please ignore)
      >
      You can easily predict that sizeOfBling will be 4... (sizeof(int*))
      Is there any way to acomplish it?
      >
      Thanks
      1. offsetof is already defined for you.
      2. why not just use sizeof?
      3. Where does the "easily predicted" 4 come from?
      4. How does your method adjust for alignment?

      REH

      Comment

      • rpgfan3233

        #4
        Re: sizeof a relative pointer in a struct

        On Mar 20, 11:41 am, REH <spamj...@stny. rr.comwrote:
        On Mar 20, 11:39 am, ManicQin <Manic...@gmail .comwrote:
        >
        >
        >
        Hi all.
        I'm trying to get the size of a variable in a struct by his relative
        postion i.e.
        >
        ///
        #define offsetof(s,m)   (size_t)&(((s *)0)->m)
        >
        struct ThePimp{
        char rings[10];
        char blings[20];
        >
        };
        >
        int sizeOfBling = sizeof( (*char)&ThePimp +
        (char)offsetof( ThePimp,blings) );
        >
        ///
        (If I have any syntax mistakes please ignore)
        >
        You can easily predict that sizeOfBling will be 4... (sizeof(int*))
        Is there any way to acomplish it?
        >
        Thanks
        >
        1. offsetof is already defined for you.
        2. why not just use sizeof?
        3. Where does the "easily predicted" 4 come from?
        4. How does your method adjust for alignment?
        >
        REH
        The "easily predicted" 4 comes from a machine where sizeof(TYPE*) = 4
        because it is the native size for that machine (most likely a 32-bit
        machine).

        Comment

        • REH

          #5
          Re: sizeof a relative pointer in a struct

          On Mar 20, 12:46 pm, rpgfan3233 <rpgfan3...@gma il.comwrote:
          The "easily predicted" 4 comes from a machine where sizeof(TYPE*) = 4
          because it is the native size for that machine (most likely a 32-bit
          machine).
          So? Where is the pointer he is trying to determine the size of. I
          see two arrays, neither of which would be 4 bytes in size, regardless
          of the machine.

          REH

          Comment

          • REH

            #6
            Re: sizeof a relative pointer in a struct

            On Mar 21, 9:23 am, ManicQin <Manic...@gmail .comwrote:
            I would ignore most of the comments
            but I do want to reply to somethings
            1) sizeof a pointer is 4 (platform specific)
            But you are not getting the size of a pointer, both of your fields are
            arrays. The size of "rings" is 10. The size of "blings" is 20. This
            will always be true, regardless of the target system, unless you
            change the type of your array elements, or the number of elements in
            your array.

            REH

            Comment

            • REH

              #7
              Re: sizeof a relative pointer in a struct

              On Mar 21, 9:23 am, ManicQin <Manic...@gmail .comwrote:
              I cannot rely on people not messing with my struct so i cannot know
              what is after or before the requested var thanks NOOB
              and by the way the alignment is dealed thanks REH
              >

              How is the alignment problem dealt with? If there is padding before a
              field, it will get included in your total when your calculate
              offsetof. How do you remove it?

              REH

              Comment

              • Ben Bacarisse

                #8
                Re: sizeof a relative pointer in a struct

                ManicQin <ManicQin@gmail .comwrites:
                On Mar 22, 6:08 pm, REH <spamj...@stny. rr.comwrote:
                >On Mar 21, 9:23 am, ManicQin <Manic...@gmail .comwrote:
                >>
                I would ignore most of the comments
                but I do want to reply to somethings
                1) sizeof a pointer is 4 (platform specific)
                >>
                >But you are not getting the size of a pointer, both of your fields are
                >arrays. The size of "rings" is 10. The size of "blings" is 20. This
                >will always be true, regardless of the target system, unless you
                >change the type of your array elements, or the number of elements in
                >your array.
                >>
                >REH
                REH:
                Sorry I rechecked it... The way I use it, it returns 4 (or
                size_of(pointer ) for the pedants)
                (See the code I posted and try him - read this post all through to see
                why I coded it that way)
                Check what REH is saying. He (or she) is saying that you seem to be
                trying to get the size of something that is not a pointer. Of course
                you get 4 (on your system) if you apply sizeof to a pointer, but your
                code:

                struct ThePimp{
                char rings[10];
                char blings[20];

                };

                int sizeOfBling = sizeof( (*char)&ThePimp +
                (char)offsetof( ThePimp,blings) );

                suggested you wanted the size of the part of struct (just from the
                name of the variable). I think all REH is saying is that you should
                not take the size of a pointer if you want the size of something else.
                Ha Ha Ha Keith:
                In normal systems you were right... But in this hideous system I only
                know the offset of the variable...
                (That's why I added the offsetof in my code above...)
                Are you starting to catch my drift...
                Nope. I, for one, am now lost about what your question is. Keith
                Thompson told you how to get the size of struct member (which is what
                I though was the question) but it seems it is not. You know how to
                find the offset of a member (though you should not need to define your
                own offsetof macro) so that can't be it.

                Are you maybe confused as to why sizeOfBling is 4? It is 4 simply
                because the type of the expression sizeof is applied to is a pointer.
                Are you confused as to why adding a char to a char * gives a pointer?
                There's a huge switch case goes like:
                switch (param)
                {
                case offsetof(rings) ... return 10
                case offsetof(blings )... return 20
                };
                That does not help us follow your problem. Are you trying to write
                something like this? Are you trying to modify it in some way?

                --
                Ben.

                Comment

                • ManicQin

                  #9
                  Re: sizeof a relative pointer in a struct

                  Ok Please dont ask me why am I writing this code the way it is, It's
                  an abbreviation of a much bigger complexed system
                  and I cant explain why it was chosen to work this way. I'm trying to
                  fix a particular point in the system.

                  I'm trying to achieve the line with the comment (and the
                  returnSizeofPar am function) in a smaller, nicer and more DYNAMIC way.
                  As you can see whenever I add a new Parameter to Parameters struct I
                  need to update returnSizeofPar amByOffest ... for me it looks a bit
                  crooked...
                  Is there a way to know the sizeof a variable in a struct only by his
                  offset. is it possible?

                  struct Parameters
                  {
                  char strIDNum[10];
                  char strFirstName[20];
                  char strFamilyName[30];
                  };

                  int returnSizeofPar amByOffest(int lParamOffset)
                  {
                  Parameters cTemp;

                  switch (lParamOffset) {
                  case offsetof(Parame ters,strIDNum): return sizeof(cTemp.st rIDNum);
                  break;
                  case offsetof(Parame ters,strFirstNa me): return
                  sizeof(cTemp.st rFirstName); break;
                  case offsetof(Parame ters,strFamilyN ame): return
                  sizeof(cTemp.st rFamilyName); break;
                  };

                  return 0;
                  }

                  int main() {
                  Parameters cTemp= {"AAA" , "BBB" , "CCC"};
                  char strTempForPrint ing[100] = {0};
                  int lParamToPrint = offsetof(Parame ters , strFirstName);
                  int sizeOfParam =
                  returnSizeofPar amByOffest(lPar amToPrint ); //
                  *************** *****

                  memcpy(strTempF orPrinting,(cha r*)&cTemp + (char)lParamToP rint ,
                  sizeOfParam );
                  printf("The parameter is: %s \n",strTempForP rinting);
                  return 0;
                  }

                  p.s. Please tell me why I wasn't understood in the above posts. (It's
                  important for me to understand why there was a mis-connection)

                  Comment

                  • Ben Bacarisse

                    #10
                    Re: sizeof a relative pointer in a struct

                    ManicQin <ManicQin@gmail .comwrites:

                    Ok Please dont ask me why am I writing this code the way it is,
                    OK.

                    <snip>
                    Is there a way to know the sizeof a variable in a struct only by his
                    offset. is it possible?
                    No.
                    struct Parameters
                    {
                    char strIDNum[10];
                    char strFirstName[20];
                    char strFamilyName[30];
                    };
                    >
                    int returnSizeofPar amByOffest(int lParamOffset)
                    {
                    Parameters cTemp;
                    >
                    switch (lParamOffset) {
                    case offsetof(Parame ters,strIDNum): return sizeof(cTemp.st rIDNum);
                    break;
                    case offsetof(Parame ters,strFirstNa me): return
                    sizeof(cTemp.st rFirstName); break;
                    case offsetof(Parame ters,strFamilyN ame): return
                    sizeof(cTemp.st rFamilyName); break;
                    };
                    >
                    return 0;
                    }
                    >
                    int main() {
                    Parameters cTemp= {"AAA" , "BBB" , "CCC"};
                    char strTempForPrint ing[100] = {0};
                    int lParamToPrint = offsetof(Parame ters , strFirstName);
                    int sizeOfParam =
                    returnSizeofPar amByOffest(lPar amToPrint ); //
                    *************** *****
                    >
                    memcpy(strTempF orPrinting,(cha r*)&cTemp + (char)lParamToP rint ,
                    sizeOfParam );
                    printf("The parameter is: %s \n",strTempForP rinting);
                    return 0;
                    }
                    This is not C (I am sure you know that -- the compiler will have told
                    you so) but since your objective is impossible, there is no point in
                    trying to make it C.
                    p.s. Please tell me why I wasn't understood in the above posts. (It's
                    important for me to understand why there was a mis-connection)
                    I think this because you question was not clear. You may have hinted
                    at the above question before, but since the answer is such a clear and
                    obvious "no" (can you tell how many pages are in a book just from its
                    position on the shelf?) people may have tried to re-interpret the
                    hints as some other question.

                    --
                    Ben.

                    Comment

                    • ManicQin

                      #11
                      Re: sizeof a relative pointer in a struct

                      On Mar 24, 2:25 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
                      ManicQin <Manic...@gmail .comwrites:
                      Ok Please dont ask me why am I writing this code the way it is,
                      >
                      OK.
                      >
                      <snip>
                      >
                      Is there a way to know the sizeof a variable in a struct only by his
                      offset. is it possible?
                      >
                      No.
                      >
                      >
                      >
                      struct Parameters
                      {
                      char strIDNum[10];
                      char strFirstName[20];
                      char strFamilyName[30];
                      };
                      >
                      int returnSizeofPar amByOffest(int lParamOffset)
                      {
                      Parameters cTemp;
                      >
                      switch (lParamOffset) {
                      case offsetof(Parame ters,strIDNum): return sizeof(cTemp.st rIDNum);
                      break;
                      case offsetof(Parame ters,strFirstNa me): return
                      sizeof(cTemp.st rFirstName); break;
                      case offsetof(Parame ters,strFamilyN ame): return
                      sizeof(cTemp.st rFamilyName); break;
                      };
                      >
                      return 0;
                      }
                      >
                      int main() {
                      Parameters cTemp= {"AAA" , "BBB" , "CCC"};
                      char strTempForPrint ing[100] = {0};
                      int lParamToPrint = offsetof(Parame ters , strFirstName);
                      int sizeOfParam =
                      returnSizeofPar amByOffest(lPar amToPrint ); //
                      *************** *****
                      >
                      memcpy(strTempF orPrinting,(cha r*)&cTemp + (char)lParamToP rint ,
                      sizeOfParam );
                      printf("The parameter is: %s \n",strTempForP rinting);
                      return 0;
                      }
                      >
                      This is not C (I am sure you know that -- the compiler will have told
                      you so) but since your objective is impossible, there is no point in
                      trying to make it C.
                      >
                      p.s. Please tell me why I wasn't understood in the above posts. (It's
                      important for me to understand why there was a mis-connection)
                      >
                      I think this because you question was not clear. You may have hinted
                      at the above question before, but since the answer is such a clear and
                      obvious "no" (can you tell how many pages are in a book just from its
                      position on the shelf?) people may have tried to re-interpret the
                      hints as some other question.
                      >
                      --
                      Ben.
                      "I'm trying to get the size of a variable in a struct by his relative
                      postion"
                      .... I thought that would be self explanatory - Never mind that now.
                      Thanks you just shattered my dreams of creating a better world... ;)
                      BTW why isnt it C?

                      Comment

                      • Richard Heathfield

                        #12
                        Re: sizeof a relative pointer in a struct

                        ManicQin said:

                        <snip>
                        "I'm trying to get the size of a variable in a struct by his relative
                        postion"
                        ... I thought that would be self explanatory
                        It is. So is "this can't be done".

                        Think about it. If it were possible to do that, then we could control the
                        size of an object simply by placing it at a certain position in the
                        struct. If, say, the relative position "first" yields "2 bytes", then we
                        could fit a double into two bytes just by making it the first member of a
                        structure! That simply isn't how the world works. Sorry.

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • Ben Bacarisse

                          #13
                          Re: sizeof a relative pointer in a struct

                          ManicQin <ManicQin@gmail .comwrites:
                          On Mar 24, 2:25 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
                          >ManicQin <Manic...@gmail .comwrites:
                          Ok Please dont ask me why am I writing this code the way it is,
                          >>
                          >OK.
                          >>
                          ><snip>
                          >>
                          Is there a way to know the sizeof a variable in a struct only by his
                          offset. is it possible?
                          >>
                          >No.
                          >>
                          >>
                          >>
                          struct Parameters
                          {
                          char strIDNum[10];
                          char strFirstName[20];
                          char strFamilyName[30];
                          };
                          >>
                          int returnSizeofPar amByOffest(int lParamOffset)
                          {
                          Parameters cTemp;
                          >>
                          switch (lParamOffset) {
                          case offsetof(Parame ters,strIDNum): return sizeof(cTemp.st rIDNum);
                          break;
                          case offsetof(Parame ters,strFirstNa me): return
                          sizeof(cTemp.st rFirstName); break;
                          case offsetof(Parame ters,strFamilyN ame): return
                          sizeof(cTemp.st rFamilyName); break;
                          };
                          >>
                          return 0;
                          }
                          >>
                          int main() {
                          Parameters cTemp= {"AAA" , "BBB" , "CCC"};
                          char strTempForPrint ing[100] = {0};
                          int lParamToPrint = offsetof(Parame ters , strFirstName);
                          int sizeOfParam =
                          returnSizeofPar amByOffest(lPar amToPrint ); //
                          *************** *****
                          >>
                          memcpy(strTempF orPrinting,(cha r*)&cTemp + (char)lParamToP rint ,
                          sizeOfParam );
                          printf("The parameter is: %s \n",strTempForP rinting);
                          return 0;
                          }
                          >>
                          >This is not C (I am sure you know that -- the compiler will have told
                          >you so) but since your objective is impossible, there is no point in
                          >trying to make it C.
                          >>
                          p.s. Please tell me why I wasn't understood in the above posts. (It's
                          important for me to understand why there was a mis-connection)
                          >>
                          >I think this because you question was not clear. You may have hinted
                          >at the above question before, but since the answer is such a clear and
                          >obvious "no" (can you tell how many pages are in a book just from its
                          >position on the shelf?) people may have tried to re-interpret the
                          >hints as some other question.
                          >>
                          >--
                          >Ben.
                          Best not to quote sigs.
                          "I'm trying to get the size of a variable in a struct by his relative
                          postion"
                          ... I thought that would be self explanatory - Never mind that now.
                          Well it was and I answered it. However, this:
                          BTW why isnt it C?
                          made me look in detail more. First the answer: it is not C because it
                          has syntax errors and does not comply with C's other rules. In
                          particular, offsetof does produce a constant suitable for use in a
                          switch statement. However, any switch can be replaced by if-then-else
                          so that would work.

                          Programmers look for general solutions, so your general question got
                          answered (at least by me) in the most general way: can't be done. The
                          solution for one structure, written out by hand, is so obvious that I
                          did not think you were asking for it:

                          struct S temp;
                          if (arg == offsetof(struct S, member1))
                          return sizeof temp.member1;
                          if (arg == offsetof(struct S, member2))
                          return sizeof temp.member2;

                          and so on. I am sorry if this sounds rude -- I really did not think
                          you could be asking for this. If you want something more automatic
                          and general, then the original answer still stands: you can't.

                          You can use C99 features to make it less of a maintenance problem:

                          -------------------
                          #include <stddef.h>

                          struct S {
                          int member1;
                          int member2;
                          };

                          size_t get_size(size_t arg)
                          {
                          struct S temp;
                          size_t sizes[] = {
                          [offsetof(struct S, member1)] = sizeof temp.member1,
                          [offsetof(struct S, member2)] = sizeof temp.member2,
                          };
                          return sizes[arg];
                          }
                          -------------------

                          but that is about it.

                          --
                          Ben.

                          Comment

                          • ManicQin

                            #14
                            Re: sizeof a relative pointer in a struct

                            Lets say that the answer did not shock me, I thought maybe there's
                            something I missed so it was worth asking.
                            Thank You all.

                            Comment

                            • pete

                              #15
                              Re: sizeof a relative pointer in a struct

                              ManicQin wrote:
                              "I'm trying to get the size of a variable in a struct by his relative
                              postion"
                              ... I thought that would be self explanatory
                              It's bizarre.
                              It's like "I'm trying to shoot a target by stabbing at it with a knife."

                              You use the sizeof operator to get the size of a variable.

                              --
                              pete

                              Comment

                              Working...