Questions tagged [structure-packing]

zen and art of reducing memory size of data structures, primarily in C and C++ code

Structure packing is a set of techniques used to reduce memory size of data structures, primarily in C and C++ code.

You need to know this technique if you intend to write code for memory-constrained embedded systems, or operating-system kernels. It is useful if you are working with application data sets so large that your programs routinely hit memory limits. It is good to know in any application where you really, really care about minimizing cache-line misses.

http://www.catb.org/esr/structure-packing/

24 questions
64
votes
4 answers

size of struct in C

Possible Duplicate: Why isn’t sizeof for a struct equal to the sum of sizeof of each member? Consider the following C code: #include struct employee { int id; char name[30]; }; int main() { struct employee e1; …
user191776
57
votes
10 answers

Memory alignment in C-structs

I'm working on a 32-bit machine, so I suppose that the memory alignment should be 4 bytes. Say I have this struct: typedef struct { unsigned short v1; unsigned short v2; unsigned short v3; } myStruct; The plain added size is 6 bytes,…
Ivan
15
votes
9 answers

Size of structure with a char, a double, an int and a t

When I run only the code fragment int *t; std::cout << sizeof(char) << std::endl; std::cout << sizeof(double) << std::endl; std::cout << sizeof(int) << std::endl; std::cout << sizeof(t) << std::endl; it gives me a result like…
user466534
9
votes
5 answers

Reinterpret struct with members of the same type as an array in a standard compliant way

In various 3d math codebases I sometimes encounter something like this: struct vec { float x, y, z; float& operator[](std::size_t i) { assert(i < 3); return (&x)[i]; } }; Which, AFAIK is illegal because…
6
votes
2 answers

size and alignment of int bitfields

A struct with bitfields, even when "packed", seems to treat a bitfield's size (and alignment, too?) based on the specified int type. Could someone point to a C++ rule that defines that behavior? I tried with a dozen of compilers and architectures…
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
5
votes
6 answers

size of a structure containing bit fields

Possible Duplicate: Why isn't sizeof for a struct equal to the sum of sizeof of each member? I was trying to understand the concept of bit fields. But I am not able to find why the size of the following structure in CASE III is coming out as 8…
user1367292
  • 1,029
  • 2
  • 11
  • 13
4
votes
1 answer

Portable way to create heterogenous vertex data array

It is very common in graphics programming to work with vertex formats. This is described, for example, here. However, I am looking for a way to accomplish that which does not invoke undefined behavior (I'm mainly looking for C++ info, but C would be…
jwd
  • 10,837
  • 3
  • 43
  • 67
4
votes
2 answers

1 Byte Alignment vs 4 Byte Alignment (Pixel Packing)

I am reading the OpenGL SuperBible for OpenGL 3.x. I am having a hard time understanding the whole "pixel packing concept." I get that typically a 199px wide image would require 597 bytes [(199 * 3)3 for each color channel RGB]. My first question is…
foobar5512
  • 2,470
  • 5
  • 36
  • 52
3
votes
1 answer

Reordering bit-fields mysteriously changes size of struct

For some reason I have a struct that needs to keep track of 56 bits of information ordered as 4 packs of 12 bits and 2 packs of 4 bits. This comes out to 7 bytes of information total. I tried a bit field like so struct foo { uint16_t R : 12; …
geo
  • 435
  • 3
  • 13
3
votes
2 answers

Unexpected size when a struct has a union

I am trying to verify the size of a struct. For some reasons, it's giving me a size of 18 rather than the expected 14 bytes (the union should have a max of 8 + 2 + 2 = 12 bytes). Can someone help me? typedef struct __attribute__((__packed__))…
fatdragon
  • 2,211
  • 4
  • 26
  • 43
3
votes
4 answers

how size of a structure varies with different data types

I am using Linux 32 bit os, and GCC compiler. I tried with three different type of structure. in the first structure i have defined only one char variable. size of this structure is 1 that is correct. in the second structure i have defined only one…
rajenpandit
  • 1,265
  • 1
  • 15
  • 21
3
votes
9 answers

C++ struct size: 2+4+2+2+4 = 16

Possible Duplicate: Why isn’t sizeof for a struct equal to the sum of sizeof of each member? Why is the sizeof(); of this structure 16 bytes? I'm compiling in g++. struct bitmapfileheader { unsigned short bfType; unsigned int…
Lukas
  • 41
  • 3
2
votes
5 answers

Sizeof operator returns incorrect size?

Possible Duplicate: Why isn't sizeof for a struct equal to the sum of sizeof of each member? Extra bytes when declaring a member of a struct as uint32_t For some reason, the sizeof operator returns a bogus size for this structure (48 instead of…
Kristina
  • 15,859
  • 29
  • 111
  • 181
2
votes
4 answers

Extra bytes when declaring a member of a struct as uint32_t

I have a problem when using the uint32_t type from the stdint.h library. If I run the following code (on Ubuntu linux 11.10 x86_64, g++ version 4.6.1): #include "stdint.h" #include using std::cout; typedef struct{ // api identifier …
2
votes
1 answer

How to deal with alignment skew when storing a structure in a byte buffer?

I want to refer to a memory location as either an array of chars or a specialized struct with two upfront char members. The reason for this is a C-API that only takes a char array, but I want to put some more information into it with my struct that…
glades
  • 3,778
  • 1
  • 12
  • 34
1
2