Complete.Org: Mailing Lists: Archives: discussion: March 2006:
[aclug-L] C/C++ question
Home

[aclug-L] C/C++ question

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: discussion@xxxxxxxxx
Subject: [aclug-L] C/C++ question
From: Olwe Bottorff <galanolwe@xxxxxxxxx>
Date: Tue, 28 Mar 2006 15:48:47 -0800 (PST)
Reply-to: discussion@xxxxxxxxx

This is some code I found on Bruno Preiss's Algorithms
site:
----
class BuddyPool : public StoragePool
{
public:
    enum Status { free, reserved };
    struct Header
    {
        Status status: 1;
        unsigned int k : bitsizeof (unsigned int) - 1U;
    };
    struct Block : public Header
    {
        enum { size = 16 };
        struct Links
        {
            Block* next;
            Block* prev;
        };
        union
        {
            Links link;
            char userPart [size - sizeof (Header)];
        };
    };
 ...
---

It's for memory allocation. I'm confused about what
the 

enum { size = 16 };

is doing. Is this just declaring a constant size to be
16 inside the struct so you don't have to declare it
16 outside later? If so, size is a real member of the
struct Block and will take up memory, it's just that
it already has a value--or am I seeing this wrong?

I'm also trying to figure out what this Block struct
will look like in memory if I allocate dynamic memory
for it, e.g., what is happening with the struct Links
declaration? Does it always get included since it's
embedded, or is it left out when the Links link is not
used (i.e., if a constructor doesn't utilize it)? The
book seems to be saying that because of the union, it
should just have the Header stuff and the char
userPart if the *next/*prev pointers aren't asked for
by a constructor--all for the lightest memory
footprint possible. Any enlightenment appreciated....


LB

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

-- This is the discussion@xxxxxxxxx list.  To unsubscribe,
visit http://www.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]