31
struct a
{
    char *c;
    char b;
};

What is sizeof(a)?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98

8 Answers8

35
#include <stdio.h>

typedef struct { char* c; char b; } a;

int main()
{
    printf("sizeof(a) == %d", sizeof(a));
}

I get "sizeof(a) == 8", on a 32-bit machine. The total size of the structure will depend on the packing: In my case, the default packing is 4, so 'c' takes 4 bytes, 'b' takes one byte, leaving 3 padding bytes to bring it to the next multiple of 4: 8. If you want to alter this packing, most compilers have a way to alter it, for example, on MSVC:

#pragma pack(1)
typedef struct { char* c; char b; } a;

gives sizeof(a) == 5. If you do this, be careful to reset the packing before any library headers!

Simon Buchan
  • 12,707
  • 2
  • 48
  • 55
9

Contrary to what some of the other answers have said, on most systems, in the absence of a pragma or compiler option, the size of the structure will be at least 6 bytes and, on most 32-bit systems, 8 bytes. For 64-bit systems, the size could easily be 16 bytes. Alignment does come into play; always. The sizeof a single struct has to be such that an array of those sizes can be allocated and the individual members of the array are sufficiently aligned for the processor in question. Consequently, if the size of the struct was 5 as others have hypothesized, then an array of two such structures would be 10 bytes long, and the char pointer in the second array member would be aligned on an odd byte, which would (on most processors) cause a major bottleneck in the performance.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • And on the old 68k macs, crash! – Simon Buchan Sep 27 '08 at 05:14
  • The reason I hypothesized that alignment would not have an effect here is that both members would be properly aligned without padding between them. I did not expect sizeof to account for padding at the end. My hypothesis has been disproved experimentally, as I noted in an edit to my answer. – Fred Larson Sep 27 '08 at 05:16
5

If you want to manually count it, the size of a struct is just the size of each of its data members after accounting for alignment. There's no magic overhead bytes for a struct.

Jeff Hubbard
  • 9,822
  • 3
  • 30
  • 28
2

The exact value is sizeof(a).
You might also take a risk and assume that it is in this case no less than 2 and no greater than 16.

eugensk
  • 1,882
  • 1
  • 14
  • 20
  • Although in theory there is no upper limit as the implementation and size of pointers is compiler specific provided they behave according to the standard. – Skizz Feb 18 '09 at 15:13
1

This will vary depending on your architecture and how it treats basic data types. It will also depend on whether the system requires natural alignment.

mdec
  • 5,122
  • 4
  • 25
  • 26
0

I assume you mean struct and not strict, but on a 32-bit system it'll be either 5 or 8 bytes, depending on if the compiler is padding the struct.

Serafina Brocious
  • 30,433
  • 12
  • 89
  • 114
0

I suspect you mean 'struct', not 'strict', and 'char' instead of 'Char'.

The size will be implementation dependent. On most 32-bit systems, it will probably be 5 -- 4 bytes for the pointer, one for the char. I don't believe alignment will come into play here. If you swapped 'c' and 'b', however, the size may grow to 8 bytes.

Ok, I tried it out (g++ 4.2.3, with -g option) and I get 8.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
0

The sizeof the structure should be 8 bytes on a 32 bit system, so that the size of the structure becomes multiple of 2. This makes individual structures available at the correct byte boundaries when an array of structures is declared. This is achieved by padding the structure with 3 bytes at the end.

If the structure had the pointer declared after the char, it would still be 8 bytes in size but the 3 byte padding would have been added to keep the pointer (which is a 4 byte element) aligned at a 4 byte address boundary.

The rule of thumb is that elements should be at an offset which is the multiple of their byte size and the structure itself should be of a size which is a multiple of 2.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
computinglife
  • 4,321
  • 1
  • 21
  • 18
  • It's relatively uncommon, but a structure containing only char arrays could have an odd size: `struct unusual { char a[3]; char b[4]; };` could have a size of 7 without causing any ill-effects. A compiler might still pad it to 8 bytes, but there's no obvious need to do so. – Jonathan Leffler Jun 07 '15 at 16:16