Questions tagged [struct-member-alignment]

32 questions
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
  • 170
  • 1
  • 11
1
vote
1 answer

Why Offset varies for character array in C structure if array size changes and how the defined macro is calculating offset here?

I have defined a macro that calculates the offset of a structure to any of the structure field. The Code is as follow: #define offset(struct_name, fld_name) \ (unsigned int)&(((struct_name *)0)->fld_name) typedef struct emp_{ char name[20]; …
1
vote
1 answer

Alignment in nested struct/union

I have the structure below compiled by VC 2005: typedef struct { unsigned int a :8; unsigned int b :8; unsigned int c :8; union { unsigned int val1 :8; unsigned int val2 …
tblum
  • 153
  • 6
1
vote
4 answers

Is there a way to access members of a struct

I want to be able to find the size of the individual members in a struct. For example struct A { int a0; char a1; } Now sizeof(A) is 8, but let's assume I am writing a function that will print the alignment of A as shown below where "aa"…
taho
  • 67
  • 1
  • 1
  • 6
1
vote
0 answers

Alignment of a simple class to allow array access without UB

Suppose I have the following simple class: struct employee{ std::string name; short salary; std::size_t age; employee(std::string name, short salary, std::size_t age) : name{name}, salary{salary}, age{age}{} …
alfC
  • 14,261
  • 4
  • 67
  • 118
1
vote
1 answer

vcpkg: Specify struct member alignment

I have some old code which is compiled with packing of 1-byte (argument /Zp1) Struct Member Alignment and I'm adding unit testing with gTest to them. I'm using vcpkg to install gTest, but it is compiled using the default setting of 8-byte packing.…
ByteNudger
  • 1,545
  • 5
  • 29
  • 37
1
vote
2 answers

Extra bits added in bitfield struct in C

I am trying to create a client C code for CAPWAP protocol. I tried implementing CAPWAP header using a bit field structure. But after sending this structure over socket using sendto(), when I sniff the packet using wireshark, I find that some extra…
1
vote
2 answers

Why does this alignment attribute have to be specified in a typedef?

I originally wrote this question from my tablet and took a lot of shortcuts in doing so that I think ultimately lead to confusion for people who read and/or attempted to answer the question. I'm not asking for a solution to the problem I originally…
Brian Vandenberg
  • 4,011
  • 2
  • 37
  • 53
1
vote
1 answer

What is MSVC /Zp alternative on GCC and clang?

In case, i want to define the structure alignment at a module level using the compiler flags such as /Zp for cl on windows. What are the alternatives on GCC and clang?
Abhishek Jain
  • 9,614
  • 5
  • 26
  • 40
1
vote
3 answers

Aligning a class to a class it inherits from? Force all stack alignment? Change sizeof?

I want to have a base class which dictates the alignment of the objects which inherit from it. This works fine for the heap because I can control how that gets allocated, and how arrays of it get allocated in a custom array template. However, the…
0
votes
1 answer

LLVM IR: struct alignment is wrong

I'm new to llvm framework and using llvm to generate jit code. But the code generated about struct is a bit problematic: ` source_filename = "test1" target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128" target triple =…
0
votes
1 answer

fread() not reading struct data correctly after upgrading C++ tools version

I have an older project that reads a binary file into a struct using fread(). It uses Visual Studio 2017 (v141) I upgraded the project to the newest C++ tools version (v142) for VS 2019, and also changed the solution from AnyCPU to x86. I also…
Rye bread
  • 1,305
  • 2
  • 13
  • 36
0
votes
0 answers

Finding the difference in size of addresses(padding)

I have a struct in c such that, typedef struct { char ch; int num; } structB; Now by how many bytes should the addresses of ch and num differ based on the addresses that they are stored on? When I run sizeof, the size of the structB is 8,…
0
votes
0 answers

Unable to read from a file in Turbo C

The following code works fine in Dev C++ but when I am running in turbo C I am not getting any output. I already have data stored in users.bin file. #include #include #include #include #define MAX_LIMIT…
havegudday
  • 65
  • 6
0
votes
0 answers

Array of aligned structure that induces a regular array of individual members

If I have an structure (e.g. employee below), an array of such structure induces strided arrays of all the members of the structure only if the size of the structure is a (least) common multiple (LCM) of the size all members. Otherwise there will…
alfC
  • 14,261
  • 4
  • 67
  • 118