Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: memory alignment?

Reply
Thread Tools

Re: memory alignment?

 
 
Andrew Koenig
Guest
Posts: n/a
 
      08-01-2003
cylin> If I have a class like below.
cylin> class test {
cylin> int a;
cylin> double b;
cylin> };

cylin> Using sizeof(test), it'll get 16 bytes.
cylin> Because I maybe handle larger than 1 million such kind objects.
cylin> Can't we reduce this class to only cost 12 bytes?

In other words, you want to be able to write

test xyzzy[1000000];

and have it take up 12 million bytes instead of 16 million.

Let's assume that an int takes up 4 bytes and a double takes
up 8 bytes. Then whether you can do what you want depends on
whether your implementation requires a double to be aligned on
a 4-byte or an 8-byte multiple.

However, if it required 4-byte alignment only, then I would have
expted sizeof(test) to be 12, not 16. It is conceivable, though
unlikely, that if you rewrote it this way:

class test {
double b;
int a;
};

that the compiler would use only 12 bytes for each object. However, I
would expect any compiler that inserts padding in the first example
above to insert padding between array elements also.

So what can you do? If you really need to save the space, you can
split up the elements of your data structure:

double b_array[1000000];
int a_array[1000000];

and then access the elements individually. Yes, I know that's
perverting your data structure. As Robert Dewar once told me,
some programs are poorly designed on purpose.

--
Andrew Koenig, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-01-2003


Andrew Koenig wrote:
>
> As Robert Dewar once told me,
> some programs are poorly designed on purpose.



A wise guy.

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Patriot Memory PDC1G5600ELK Memory Review Silverstrand Front Page News 0 09-07-2005 02:24 AM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
RAM Memory or virual memory Julián Sanz García ASP .Net 4 11-12-2004 06:25 PM
GC does not release memory...memory keeps growing!!! Mahesh Prasad ASP .Net 1 02-22-2004 08:40 AM
AspNet Process Memory Issue on Win2k Server - Peformance is fine - Memory usuage doesn't stop growing Cy Huckaba ASP .Net 1 06-26-2003 04:00 AM



Advertisments