7

In my code, I have the following struct:

struct foo {
 int a;
 int b;
};

In b, only values between 0 and 100 are stored. So in general, I could replace it by a char. But than the size of the struct is the same due to padding effects. As my code contains vectors and sets of these structure with several million entries, it would bring the memory usage down by more than one third if I could circumvent padding in some way. Is there any possibility to do this, e.g. some other (boost) data container which behaves in the same way?

Edit: I use both, the GNU and the Intel compiler on Linux systems:

Thomas W.
  • 2,134
  • 2
  • 24
  • 46
  • 3
    You need a `struct` `pack`-ing. Depends on your compiler, you may use (probably `pragma pack`) to tell your compiler, not to add padding. – Kiril Kirov Nov 01 '12 at 14:22
  • maybe this will help http://www.viva64.com/en/d/0150/ – Digital Da Nov 01 '12 at 14:26
  • 2
    @Kiril Why did you not add this as an answer? – Ant Nov 01 '12 at 14:26
  • @Ant - because I can't test it for sure right now and it depends on the compiler, which is not written in the question (I mean, I don't know the compiler to give an exact answer). Also, I'm pretty sure there's a duplicate of this question, I'll try to find it. – Kiril Kirov Nov 01 '12 at 14:30
  • possible duplicate of [C++ struct alignment question](http://stackoverflow.com/questions/1455458/c-struct-alignment-question) – Kiril Kirov Nov 01 '12 at 14:32
  • @Kiril: I just tested it with a GNU compiler, and it works. Now I have to see, how the performance is influenced when accessing the data. – Thomas W. Nov 01 '12 at 14:33
  • @Kiril thank you. It's so compiler dependent that I thought that was a sufficient and correct answer. – Ant Nov 01 '12 at 14:33
  • 1
    Perhaps you could split your struct in two. – didierc Nov 01 '12 at 14:33
  • I added my comment as an answer :) – Kiril Kirov Nov 01 '12 at 14:42
  • 2
    Do you really **need** to pack it? Most processors are more efficient with the native size (integer) than a smaller size. Also, there may be extra processing time required to handle the smaller sizes; check your assembly code. *Is the packing going to save you huge amounts of memory space?* – Thomas Matthews Nov 01 '12 at 15:04
  • 3
    @ThomasMatthews: I made some benchmarks: The part of my code, which works on this data is around 10% slower with the packed data. But this part takes just 1% of the overall runtime, so the extra slowdown does not really matter. But, the data is responsible for more than 30% of the code's memory usage. – Thomas W. Nov 02 '12 at 06:58

2 Answers2

7

Moving my comment as an answer, as the community advised :)

This is compiler dependent. What you need is to use struct packing.

For Visual Studio, you need #pragma pack and for gcc, you need to use an attribute packed.

For more information, see C++ struct alignment question

Hope that helps, sorry I can't really test it right now, but that's what you need

Community
  • 1
  • 1
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
3

Short of using special #pragmas to control the structure padding/alignment, you could split a and b and store them in separate vectors, and "match" them by index.

As for sets, even if you could easily split the structure to its constituent parts (which you probably can't from the purely logical perspective - sets don't have indexes), you'd still pay for dynamic memory management alignment which would likely erase any advantage you might have gained with that.

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167