does system use/lose the space?

Started by
2 comments, last by Dave Hunt 17 years, 11 months ago
if i declare a class like this class X{ uint8_t i; } sizeof(X) = 4byte / 8byte(x64) (because of wordalignment afaik) does the system really "lose" this 3/7 byte ? in my case its pretty important because such a class will be used pretty often i mean ... 7 byte can be pretty much space, when you use this class a couple of million times [Edited by - ronnybrendel on April 28, 2006 10:29:59 AM]
Advertisement
Yup, the pad bytes are "lost" in that they are allocated and consume storage. Some particularly nasty hacks involve storing "secret" data in these pad bytes, but that's really a bad thing to be doing.

1,000,000 instances of an 8-byte class is 8,000,000 bytes. 8,000,000 bytes is on the order of 7.5ish MB of peak memory usage (assuming all one million instances are allocated at a time). If you are that strapped for memory, may I direct you to your local Best Buy?

Yes, you can remove the padding using compiler-specific #pragmas or other extensions. In Visual Studio you'll want to look up #pragma pack. However you should be aware that while you may be reducing size, you're sacrificing other things, such as speed, potentially (you may end up paying for misaligned data access).

Unless you've profiled and really discovered that those pad bytes are really, really killing you, this is not something to concern yourself with.
thanx for the help :)

edit: we use a treestructure for displaying tracedata (such as function-calls, ipc-messages, counter, ...) and we are currently trying to reduce RAM usage in order to load bigger traces .... some traces have more than 50mio functioncalls ... so its important to save every single byte :X ... my task at the moment is to create an efficient structure ... ( important is the x64-stuff, because of "newer" HPC use x64)
Quote:Original post by jpetrie
Yes, you can remove the padding using compiler-specific #pragmas or other extensions. In Visual Studio you'll want to look up #pragma pack. However you should be aware that while you may be reducing size, you're sacrificing other things, such as speed, potentially (you may end up paying for misaligned data access).


Even with #pragma pack, each instance of class X will still be word-aligned, so you'll still lose the pad bytes.

This topic is closed to new replies.

Advertisement