Questions tagged [struct-member-alignment]

30 questions
58
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
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
  • 265
  • 1
  • 3
  • 11
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
  • 8,146
  • 4
  • 18
  • 34
4
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
  • 792
  • 6
  • 10
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

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…
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 *)…
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
  • 3,570
  • 1
  • 27
  • 40
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,178
  • 8
  • 27
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
  • 155
  • 1
  • 2
  • 5
2
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…
1
vote
0 answers

Strange byte alignment between windows and Linux gcc compiler

I see a strange behavior of reading a bin file and mapping in to the structure in the windows compared to linux gcc compiler. Below is my c code: #include #include #include #define IV_MAX_LEN 32 #define…
Aravind Nadumane
  • 150
  • 1
  • 11
1
2