Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any size limitations for C structures?

Tags:

c

structure

Are there any size limitations for C structures?

like image 901
Nemo Avatar asked Oct 11 '11 10:10

Nemo


People also ask

What is the size of C in structure?

2) What is the size of a C structure.? A) C structure is always 128 bytes.

Can you use sizeof on a struct in C?

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.

How many bytes is a struct in C?

Above is the alignment of the structure A, and that's why the size of the struct is 32 Bytes.


2 Answers

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).

like image 104
Alexey Frunze Avatar answered Sep 19 '22 20:09

Alexey Frunze


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.

like image 28
Blagovest Buyukliev Avatar answered Sep 21 '22 20:09

Blagovest Buyukliev