How to initialize a structure variable to the size of the structure at compile time?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Strachan
    New Member
    • Apr 2010
    • 4

    How to initialize a structure variable to the size of the structure at compile time?

    I have seen this before and know it works (at least on an old Borland compiler). Unfortunately, though my memory is good, it's short. Using gcc how do I (can I?) initialize TESTSTRUCT.ssiz e to sizeof(TESTSTRU CT) at compile time?

    first data location counter = ??

    Code:
    typedef struct {
         int       ssize = x;        // The size of this structure
         long    lvar;
         short   svar;
    } TESTSTRUCT;
    x = current data location counter - first data location counter;
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You use the sizeof operator like this

    Code:
        struct mys {
            size_t size;
            int a;
            float b;
            double c;
        } mys = {sizeof(mys), 0, 0.0F, 0.0};
    The reason this doesn't work for you is that you are not defining a variable so there is no storage to store any value. See that typedef you are defining a type.

    Comment

    • Roy Strachan
      New Member
      • Apr 2010
      • 4

      #3
      I think you are missing the point of the question. There is no problem getting the size of the structure at run time, what I am trying to do is get the size at compile time and store that value in the structure itself; ie mys.size == sizeof(mys). This would probably require that the preprocessor make 2 passes so that the struct size is known. There should be some symbol that equates to the location at the start of the structure (data pointer?) and to the end of the structure so that the difference between them is the size of the structure.
      Thanks for the reply
      Roy

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No you are missing the point. You code does NOT allocate a structure so there are no memory pointers to perform the calculation you require.

        Also the sizeof operator is, on the whole, evaluated by the compiler at compile time not at runtime because the compiler has to know the size of any structure in order to be able to place instances of it in memory.

        Also C/C++ compilers are generally single pass compilers I believe.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Once you declare the structure you can define as many instances of that structure as you like. Each instance has its own initializer. You would need to specify the appropriate sizeof(type) over and over again in the initializer of each instance.

          Are you instead looking for some way for the structure declaration to automatically set a read-only field equal to the size of the structure? You can't do that in C, but you might be able to do it in C++ by having the constructor initialize that field for you when each instance is instantiated. I don't really know what all this C++ jargon means, but perhaps one of the C++ experts can figure it out.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You are pretty much spot on with that Don, in C++ you could have a member that was initialised to the classes size in the class constructor, optionally it could be constant or even a static constant

            Code:
            struct TESTSTRUCT{
                 int       ssize;
                 long    lvar;
                 short   svar;
            
                TESTSTRUCT()
                : ssize(sizeof(TESTSTRUCT))
                {
                }
            };

            Comment

            • Roy Strachan
              New Member
              • Apr 2010
              • 4

              #7
              OK, OK. Now I feel stupid. My old memory remembered it wrong. I think all that I was doing was getting the size of the structure not actually allocating to it. Gotta admit this getting old thing is not for wimps. I haven't done any programming except for minor little things for about 10 years and I am finding that I have forgotten most of what I ever knew, if I ever really knew it. Also I'm finding that gcc is a lot more fussy that the old Borland C and C++ builder compilers that I used to use on windoze. I think I'm finding out why Linux is more solid than Microsloth; the tools won't let you be that sloppy.
              Thanks for your patience,
              Roy

              Comment

              • Roy Strachan
                New Member
                • Apr 2010
                • 4

                #8
                Originally posted by donbock
                Once you declare the structure you can define as many instances of that structure as you like. Each instance has its own initializer. You would need to specify the appropriate sizeof(type) over and over again in the initializer of each instance.

                Are you instead looking for some way for the structure declaration to automatically set a read-only field equal to the size of the structure? You can't do that in C, but you might be able to do it in C++ by having the constructor initialize that field for you when each instance is instantiated. I don't really know what all this C++ jargon means, but perhaps one of the C++ experts can figure it out.
                You could have the answer there. I *was* using Borland's C++ Builder. I'm getting a bit frustrated with my lack of memory, but I have got what i was trying to do working.
                Thanks for the help
                Roy

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  If you are using gcc then remember to use the -Wall and -pedantic switches for maximum compiler whining about code/standard errors.

                  Comment

                  Working...