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
2
votes
2 answers

sizeof(struct) weird output in C++

Could somebody please explain the output of this code? #include using namespace std; struct Yo{ char sex; int a; }; int main() { Yo c; cout<
Rishikesh Shukla
  • 317
  • 1
  • 12
1
vote
3 answers

_int64 bit field

I need to use a 6-byte (48-bit) bitfield in a structure that I can use as unsigned integer for comparison etc. Something along the following: pack (1) struct my_struct { _int64 var1:48; } s; if (s.var >= 0xaabbccddee) { // do something } But…
cool.2k7
  • 21
  • 1
  • 2
1
vote
0 answers

GCC end-aligned structure

I am working on a memory allocator. Each allocated buffer starts on an 8-byte boundary and each is preceded by a header for managing the allocation (the header is immediately before the 8-byte-aligned buffer). The header looks like: struct header { …
Philippe Aubertin
  • 1,031
  • 1
  • 9
  • 22
0
votes
0 answers

Typedef struct problems (extracting box files)

I need a little help. I'm working an an extractor tool, but I have problems with my code. Can you please help me? I tried to edit my code multiple times. But I can not make it to work properly. I can extract files from the archive file, but it won't…
0
votes
0 answers

C++ question , how can I see the default value of packing

I'm a little confused on the current packing scheme in a struct. Take this example #include using namespace std; struct Node { char c; double d; int s; } Node; int main(int argc, const char * argv[])…
Maverick447
  • 175
  • 1
  • 11
0
votes
1 answer

In the directive, #pragma pack(n), which values are valid values of n?

I did google and found that n= 1,2,4,8 are only valid arguments to the preprocessor directive #pragma pack(n). Can someone tell me please what's wrong with the values other than the above-mentioned values? (ex- n=3,5, etc why invalid)? Are the…
Abhishek Jaiswal
  • 288
  • 2
  • 14
0
votes
3 answers

Bit fields maked more optimized

I'm making some code for a 16 bits microprocessor. My memory is quite limited at 128 KB. IAR C/C++ Compiler for MSP430 I need to implement some code to save some memory. I tried to implemented this with this C characteristic implementation. struct…
MrSpartan
  • 37
  • 5
0
votes
1 answer

Does pragmapack() in C programming have any usage apart from structure packing?

While I'm on an interview the interviewer asked me are there any other usage of pragmapack() in C apart from structure packing? So I answered that I don't know apart from structure packing. So are there any other usage of it?
Meganathan
  • 21
  • 5
0
votes
2 answers

std::memcpy struct with tightly packed members of TriviallyCopyable type T to an array of T and vise versa

Here is has been established that it is illegal to treat tightly packed consecutive struct members of type T as an array of T. But what about copying the underlying representation? Given: struct vec { float x, y, z; }; with the the same…
1
2