Questions tagged [struct-member-alignment]

32 questions
61
votes
7 answers

How do I organize members in a struct to waste the least space on alignment?

[Not a duplicate of Structure padding and packing. That question is about how and when padding occurs. This one is about how to deal with it.] I have just realized how much memory is wasted as a result of alignment in C++. Consider the following…
user11313931
8
votes
2 answers

Why does Clang-Tidy suggest a larger alignment?

Given the following c language struct definition: typedef struct PackTest { long long a; int b; int c; } PackTest; Clang-Tidy gives the following message: Accessing fields in struct 'PackTest' is inefficient due to poor alignment;…
yixiang
  • 890
  • 8
  • 12
5
votes
2 answers

Struct alignment safe use

I'm new in a company where the following use of a struct is done: #include #include typedef unsigned char uint8; typedef signed char int8; typedef unsigned short int uint16; typedef signed short int int16; typedef struct…
learning_frog
  • 305
  • 1
  • 3
  • 14
5
votes
0 answers

What is the default structure packing using cl/gcc/clang?

On msdn for cl compiler option /Zp, it is mentioned that "Packs structures on 8-byte boundaries (default)." Does that mean that if there a data type of size > 8 bytes, by default (no compiler option or pragma pack) it would not be naturally aligned…
Abhishek Jain
  • 9,614
  • 5
  • 26
  • 40
4
votes
3 answers

fread(): Reading from a file (without alignment) results in skipping of bytes

I have a file and using C I want to read the contents of it using fread() (from stdio.h) and write it into the members of a struct. (In my case there is a 2 byte int at the start followed by a 4 byte int.) But after writing the contents of the file…
4
votes
1 answer

sizeof c struct with char fields only

I understand how padding works. I know what the alignment is. What is strange for me is, why the size of the struct with char fields only is not aligned to 4 bytes (padding at the end)? I suspect this is simply not guaranteed by the specification,…
hencew5
  • 43
  • 2
3
votes
4 answers

Is it better for performance to have alignment padding at the end of a small struct instead of between 2 members?

We know that there is padding in some structures in C. Please consider the following 2: struct node1 { int a; int b; char c; }; struct node2 { int a; char c; int b; }; Assuming sizeof(int) = alignof(int) = 4…
3
votes
3 answers

Struct member alignment -- different sizeof using 16-bit and 32-bit compiler

I have a structure used to contruct messages to a control board I need to maintain software compatibility between a C167 16-bit Keil compiler and a 32-bit Tricore gcc compiler. typedef struct { unsigned char new_weld_status[2]; UINT32…
3
votes
4 answers

C program - Structure variable data packing and alignment

What will be the output of the program on a 32-bit machine (using GCC)? Explain. #include int main() { struct node { int data; struct node *link; }; struct node *p, *q; p = (struct node *)…
3
votes
4 answers

How come Visual Studio does not optimize structs for best memory usage?

My question is why doesnt the Visual Studio 2012 compiler automatically reorder struct members for best memory utilization? The compiler seems to store the members in exactly the order they are declared in the struct definition, with some empty…
2
votes
1 answer

C++ Member and vtable order in diamond (multiple) virtual inheritance

I wanted to know the ordering of member variables and vtable pointers in C++ on a diamond virtual inheritance. Consider the below inheritance: class Base { int b; }; class Derived: public virtual Base { int d; }; class Derived2: public…
2
votes
1 answer

Comparing two structs of the same type from external API

So basically I'm trying to compare two VkPhysicalDeviceFeatures from Vulkan, one from the VkPhysicalDevice I'm looking at, and the other that corresponds to a set of features I actually require. a VkPhysicalDeviceFeatures struct only contains…
Krupip
  • 4,404
  • 2
  • 32
  • 54
2
votes
1 answer

How to set Visual C build options for Struct Member Alignment in CMake?

I want to know how I can set the following Visual Studio build options in my CMakeLists.txt. Struct Member Alignment = 1 Byte (/Zp1) which is set in the Project properties (Configuration Properties -> C/C++ -> Code Generation).
2
votes
0 answers

Is there a guaranteed way to avoid padding between base and derived class?

I'm working on a network program that will deal with packets having a header portion and a payload portion. The header is variable-length, with extension segments added or removed depending on network conditions. The user data portion is a plain…
Dave M.
  • 1,496
  • 1
  • 12
  • 30
2
votes
2 answers

What's the reason for this struct's alignment?

In the Vulkan header vulkan.h there is a struct defined as typedef struct VkSwapchainCreateInfoKHR { VkStructureType sType; const void* pNext; VkSwapchainCreateFlagsKHR flags; …
rhynodegreat
  • 185
  • 1
  • 2
  • 5
1
2 3