Are there any size limitations for C structures?
2) What is the size of a C structure.? A) C structure is always 128 bytes.
The sizeof for a struct is not always equal to the sum of sizeof of each individual member. This is because of the padding added by the compiler to avoid alignment issues. Padding is only added when a structure member is followed by a member with a larger size or at the end of the structure.
Above is the alignment of the structure A, and that's why the size of the struct is 32 Bytes.
From the C standard:
5.2.4.1 Translation limits
1 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:
... — 65535 bytes in an object (in a hosted environment only)
... — 1023 members in a single structure or union
... — 63 levels of nested structure or union definitions in a single struct-declaration-list ... 13) Implementations should avoid imposing fixed translation limits whenever possible.
Other than that, the upper bound is SIZE_MAX
(maximum value for size_t
).
Since the sizeof
operator yields a result of type size_t
, the limit should be SIZE_MAX
.
You can determine the value of SIZE_MAX
like this:
#include <stdint.h>
#include <stdio.h>
int main (void) {
printf("%zu", SIZE_MAX);
return 0;
}
This is what the compiler should allow. What the runtime environment allows is another story.
Declaring a similarly sized object on the stack (locally) in practice will not work since the stack is probably much, much smaller than SIZE_MAX
.
Having such an object globally might make the executable loader complain at program startup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With