0

I read that the size of a struct is at least the sum of individual fields inside it, and it can be greater than that when using struct array for alignment. What is alignment, why does it add that so called padding, how does it make it faster?

This is the code

#include <stdio.h>

int main(void) {
    typedef struct {
        int a;
        int b;
        char *c; 
    } node;

    node x;
    x.a = 1;
    x.b = 2;
    x.c = "sahil";
    printf("size is %d\n", sizeof(x));
}

when I use %f instead of %d, it shows 0 as output. Why so? When used with %d as written above, it prints 16.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 2
    `%f` is for floats. sizeof return `size_t` - an integral value, not float. – wohlstad Mar 17 '23 at 14:30
  • 1
    `printf` conversion specifiers direct not only the characteristics of the output format, but also the required data type of the corresponding argument. If the argument type is mismatched with the conversion specifier then the behavior is undefined. – John Bollinger Mar 17 '23 at 14:33
  • What compiler and which options are you using? You should get a warning about providing wrong parameter type if you use `%f` together with `size_t`. If you don't get a warning, increase warning level. For GCC use `-Wall -Wextra -pedantic`. If you get warnings, don't ignore them. – Gerhardh Mar 17 '23 at 14:34
  • Note that `%d` is wrong too, but "undefined behavior" can include behavior that appears to be what you expected. – John Bollinger Mar 17 '23 at 14:35
  • 2
    None is correct. You need to use use `%zu` to output the `size_t` type value – 0___________ Mar 17 '23 at 14:37
  • @Gerhardh got it . one more thing. i read some posts about alignment of entries of struct and padding. there the largest type was int, therefore the padding was given according to 4 bytes. if i have pointers in the struct, then will it add padding taking 8 bytes as reference????? – Sahil Gautam Mar 17 '23 at 14:40
  • @SahilGautam no. If you have struct as a member and this struct is 1000000 bytes long will it pad to 1000000? – 0___________ Mar 17 '23 at 14:44
  • @0___________ i was saying, if i have a pointer and a char, then will it add 7 bytes of padding at the end of that char – Sahil Gautam Mar 17 '23 at 14:46
  • also if someone knows how to set the warning levels of gcc to max without using the flags each time – Sahil Gautam Mar 17 '23 at 14:48
  • @SahilGautam recompile gcc to have all those flags set as default – 0___________ Mar 17 '23 at 14:49
  • @SahilGautam no - https://godbolt.org/z/acr3s896c – 0___________ Mar 17 '23 at 14:50
  • @0___________ how to recompile gcc. me noob you know – Sahil Gautam Mar 17 '23 at 14:52
  • @SahilGautam: *there the largest type was int* Not really. On today's 64-bit architectures, the largest type in your structure is the `char *` pointer, with a size of 8 bytes. This `node` type has a size of 4 + 4 + 8 = 16 bytes, no padding and its alignment requirement is 8-bytes. If you changed the order of the members to `int a; char *c; int b;` the size would become 24 bytes with 4 bytes of padding after `a` and 4 bytes of padding after `b` in order to ensure proper 8-byte alignment of the `char *c` member. – chqrlie Mar 17 '23 at 16:21

0 Answers0