#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!

Answer from Simon Buchan on Stack Overflow
🌐
Quora
quora.com › How-many-bytes-is-a-struct-in-C
How many bytes is a struct in C? - Quora
Answer (1 of 5): It depends on how you declare the members inside structure. If you have one integer than size of structure will be 4. If there is an int and four characters then size would be 8. Use sizeof operator in C to know the size of Structure
Discussions

size of struct in C - Stack Overflow
Even though the c field doesn't need padding, the struct will generally have a sizeof(struct foo_t) == 8 (on a 32-bit system - rather a system with a 32-bit int type) because there will need to be 3 bytes of padding after the c field. More on stackoverflow.com
🌐 stackoverflow.com
Total bytes for fields in Structure - C++ Forum
R. Here is the structure ... You can also use sizeof to find the number of bytes an object has. More on cplusplus.com
🌐 cplusplus.com
Struct with specific byte size and layout
Hi, I'm attempting to follow along with this series discussing how to build a simple sqlite clone and I'm attempting to write it in Rust. I'm having trouble understanding how to achieve the serialization that is done in C, in Rust So (in C) there is a struct to represent a Row #define ... More on users.rust-lang.org
🌐 users.rust-lang.org
1
1
May 29, 2022
Struct; What is 16 Bytes?
Enshittification More on reddit.com
🌐 r/csharp
31
9
May 3, 2018
🌐
IncludeHelp
includehelp.com › c › size-of-struct-in-c-padding-alignment-in-struct.aspx
Size of struct in C | padding, alignment in struct
July 19, 2020 - Size of the struct should be sum ... doesn't depend on what kind of data type they are pointing too) So the size of the struct should be: (4+8+1+8)=21 Bytes...
🌐
Cplusplus
cplusplus.com › forum › beginner › 130197
Total bytes for fields in Structure - C++ Forum
Also, the problem with these kinds of questions is that some data types you don't know how long they are, for example long - its at least 4 bytes, though it could be 8 bytes (and is on my compiler). ... So you are saying the total with out overhead is 43. By the way what is Overhead and padding?
🌐
OpenGenus
iq.opengenus.org › size-of-struct-in-c
Size of struct in C/ C++
September 16, 2021 - Size of the struct should be sum of all the data member, which is: Size of int n1+ size of int* n2 +size of char c1+ size of char* c2 · Now considering the 64-bit system, Size of int is 4 Bytes Size of character is 1 Byte Size of any pointer type is 8 Bytes (Pointer size doesn't depend on ...
🌐
PVS-Studio
pvs-studio.com › en › blog › lessons › 0023
Lesson 23. Pattern 15. Growth of structures′ sizes
In the 64-bit build mode the structure MyStruct will take 24 bytes. It is clear. First there is one byte for m_bool and 7 vacant bytes for the purpose of alignment because a pointer takes 8 bytes and must be aligned on an 8-byte boundary.
Find elsewhere
🌐
Python
docs.python.org › 3 › library › struct.html
struct — Interpret bytes as packed binary data
February 23, 2026 - The buffer’s size in bytes must be a multiple of the size required by the format, as reflected by calcsize(). Each iteration yields a tuple as specified by the format string. Added in version 3.4. ... Return the size of the struct (and hence of the bytes object produced by pack(format, ...)) corresponding to the format string format.
🌐
Medium
medium.com › mycsdegree › struct-padding-in-c-overview-examples-visuals-96888cae82fe
Struct Padding in C: Overview, Examples, Visuals | by Kyra Krishna | mycsdegree | Medium
February 16, 2023 - It will be the standard “block” size for the entire struct. If a data type doesn’t fit within a given block, you must add it to the next block. (eg. in struct a, 8 bytes will be the standard size — it’s the size of char*, a pointer).
Top answer
1 of 3
7

You should prefer small structs because structs will likely be copied, and smaller structs are cheaper to copy.

The docs do provide a bit more context for that 16-byte size limit:

Next, reference type assignments copy the reference, whereas value type assignments copy the entire value. Therefore, assignments of large reference types are cheaper than assignments of large value types.

Assuming a word size of 8 bytes, a 16-byte struct is only twice as large as a pointer to a reference type, thus still comparably efficient to copy.

This is completely unrelated to limited stack size. The stack typically has far more space than you might think, unless you're writing deeply recursive algorithms.

In C and C++, it is common to store significant amounts of data on the stack, and these languages only have value types. But unlike C# they can specify whether a struct is passed by value (and thus copied), or by reference or pointer (without a copy). The C++ Core Guidelines recommend that parameters are passed by reference, unless the type is known to be be efficiently copyable (trivially copyable without invoking copy constructors, and only up to two or three words large). Under those circumstances, a copy is more efficient than passing by reference because the copy avoids pointer indirection.

2 of 3
2

I know that one reason for keeping it under 16 bytes is that structs are intended to be used on the stack.

The manner in which structs are stored in C# is an implementation detail. They might be stored on the stack, but they might not.

Does this 16 byte rule still make sense in a situation where I use a struct as a private field in a class?

What matters is whether you want value or reference semantics, not the absolute size of the struct. If you're willing to put up with the copying, your structs can be as large as you want. In most cases, however, you're going to be better served by using classes for larger data types.

Further Reading
The Truth About Value Types

🌐
Cprogramming
cboard.cprogramming.com › c-programming › 179748-size-struct.html
Size of a struct
November 10, 2020 - #include <stdio.h> struct clientData ... clientData)); } structs are padded for alignment purposes. acctNum will be 4 bytes, lastName 16 (1 pad), firstname 12(2 pad) and balance 8 bytes....
Top answer
1 of 7
24

You're misquoting the book (at least the 2nd edition). Jeffrey Richter states value types can be more than 16 bytes if:

You don't intend to pass them to other methods or copy them to and from a collection class.

Additionally Eric Gunnerson adds (regarding the 16 byte limit)

Use this guideline as a trigger to do more investigation.

It is simply not true that a struct "has to be less than 16 bytes in size". It all depends on usage.

If you are creating the struct and also consuming it and are worried about performance then use a profiler comparing a struct vs class to see which works best for you.

2 of 7
11

Only you know how your structs are being used in your program. But if nothing else, you can always test it for yourself. For instance, if it's frequently passed to other functions, the following may illuminate you:

class MainClass
{
    static void Main()
    {
        Struct64 s1 = new Struct64();
        Class64 c1 = new Class64();
        DoStuff(s1);
        DoStuff(c1);
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < 10000; i++)
        {
            s1 = DoStuff(s1);
        }
        sw.Stop();
        Console.WriteLine("Struct {0}", sw.ElapsedTicks);
        sw.Reset();

        sw.Start();
        for (int i = 0; i < 10000; i++)
        {
            c1 = DoStuff(c1);
        }
        sw.Stop();
        Console.WriteLine("Class {0}", sw.ElapsedTicks);
        sw.Reset();

        Console.ReadLine();
    }
}

with:

public class Class64
{
    public long l1;
    public long l2;
    public long l3;
    public long l4;
    public long l5;
    public long l6;
    public long l7;
    public long l8;
}
public struct Struct64
{
    public long l1;
    public long l2;
    public long l3;
    public long l4;
    public long l5;
    public long l6;
    public long l7;
    public long l8;
}

Try this sort of thing with representative structs/classes, and see what results you get. (On my machine, above test, the class seems ~3 times faster)

🌐
Robopenguins
robopenguins.com › cpp-data-memory
Understanding C Struct Binary Representation | Robopenguins
January 22, 2022 - With packing we can get around the implicit padding being added by the compiler. By using the packed attribute we can be sure that the size of the struct is 15 bytes regardless of the processor being targeted.
🌐
Sololearn
sololearn.com › en › Discuss › 1606282 › why-the-size-of-structure-is-24-why-not-20-
Why the size of structure is 24 ? why not 20 ? | Sololearn: Learn to code for FREE!
The size of the structure on my system is only 16. Again node, a, b sum up to 12, + plus 4 bytes for the pointer = 16 bytes. (no padding needed). ... Kirk Schafer as the pointer size is 8byte according to my understanding it has to be a 64bit system.