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

[aclug-L] Re: C/C++ question

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: discussion@xxxxxxxxx
Subject: [aclug-L] Re: C/C++ question
From: Tom Hull <thull2@xxxxxxx>
Date: Tue, 28 Mar 2006 22:31:14 -0600
Reply-to: discussion@xxxxxxxxx

Olwe Bottorff wrote:
> 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?

an enum is an integer constant. in this case the symbol name is
Block::size (or just size within the Block declaration or a Block
member function) and the value is 16. it does not take up any
space within any Block object.

in ye C days of olde, integer constants were declared using #define.
a lot of people didn't like that because symbols defined using
#define couldn't honor any of the scoping rules of C, so enum was
invented, then later things like

   const int size = 16;

that the compiler could optimize out. this is the same as in C,
except the scoping rules are more complex for C++.

> 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....

can't say based on what you quoted. don't know what a StoragePool
is, other than a class BuddyPool is derived from. the enum takes
no space. the structs are just declarations. if there is a Header
member of BuddyPool, it would be sizeof(int), since the struct is
a bitfield that deliberately doesn't overflow sizeof(int). if there
is a Block, it would start with a Header (sizeof(int)), then the
union (in C++ a union without a type name doesn't have to have an
object name in order to be allocated), which has to leave space
for the largest of its members: in this case, either two pointers
or (16 - sizeof(int)).

as a general rule, look for object member variables in the private
section of the class definition. the main point of C++ is to
encapsulate all of the operations on an object within the definition
of that object.

also note that the compiler implementer may add space to an object
for padding depending on the machine requirements. also, objects
may have space automatically allocated for implementation reasons;
the most common example of this is a pointer to a virtual function
table.

in order to find the actual offsets for each member variable in an
object debuggers consult the symbol table, which has that info.

> LB

-- 
/*
  *  Tom Hull * http://tomhull.com/ * http://notesoneverydaylife.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]