3

Hi i want to make a function which calculate the size of structure without padding bytes.

Example :

struct test{
    int   x;
    char  y;
    int   z;
};

If i calculate the size of above structure i must get 9 byte (i.e. without padding bytes)

And consider the case where i might not know the variable present in struct.

Example :

struct test{
    int   x;
    ....
    ....
    ....
    int   z;
};

so if i calculate the size it should give correct size.

Is it possible do write such function?I tried reading on structure's but i dint find any solution.I saw there is some compiler option are present from that i can get but i dont want any in build compiler option.

4
  • You can always add your own size function or use sizeof with some special compiler specific magic. Commented Feb 4, 2013 at 16:48
  • 1
    #pragma pack(1) to force it down to unpadded size? Commented Feb 4, 2013 at 16:48
  • @Dukeling i know through standard options i will get as i mentioned in question, but i dont need such options. Commented Feb 4, 2013 at 16:50
  • 1
    Why? There's nothing useful that you can do with such information. Commented Feb 4, 2013 at 17:53

2 Answers 2

4

No, C doesn't have enough introspection to make this possible. At run-time, there is no information left that the program can use to "know" which fields are in a structure, or what their types are.

Sign up to request clarification or add additional context in comments.

4 Comments

ok that valid for case 2.But suppose if we know the the varibles of struct?
Pass each variable to sizeof() and sum the sizes
@Badshah: it's not possible either. What you would need is to generically iterate over all the member variables of a class and apply sizeof to each, then do the sum. But there's no way to perform such an iteration
@AndyProwl yes i know even if i will do also somehow it will be a bulky process.
3

The sizeof compiler construct should give you the size of the object "as the compiler sees it". Most compilers will align structures on 4 byte boundaries for efficient memory access at the expense of space. Try

#include <stdio.h>
int main() {
  struct test{
    int x;
    char a;
    int y;
  };
  struct test myStruct;
  printf("size of myStruct is %ld\n", sizeof(myStruct));
}

When I run it, I get

size of myStruct is 12

Which shows that my compiler, in this situation, is allocating four bytes to 'a'. I think that's your answer... unless I really misunderstood your question, and in particular your "without padding bytes" comment.

Rewriting the definition of the struct as:

  struct test{
    int x;
    char a;
    int y;
  }__attribute__((packed));

The output becomes

size of myStruct is 9

This only works at compile time - so it is not really a "function". And it doesn't apply if you don't know your struct at compile time. As such, I think my answer and the one given by @unwind above are not inconsistent.

4 Comments

above will give me size of overall struct i.e. 12 bytes not 9 bytes.
size of operator will give the overall structure size i.e.(int 4 , char 1 (3 bytes paaded ) 1 + 3 = 4, int 4, so total - 4 + 4 + 4 = 12.But i need actuall i.e. 9 bytes ...
yes now it will give 9 because we are using pragma "__attribute__((packed));"
Actually, in most compilers, the alignment of a struct will vary according to its contents. Something like struct X { char a; char b; char c; } will probably have an alignment of 1, and a size of 3; if the struct contains a double, it will usually have an alignment of 8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.