Re: How to know the size of my structures/ammount of heap in use?

From: Martin Aupperle (XXXMartin.Aupperle_at_PrimaProgrammXXX.de)
Date: 08/30/04


Date: Mon, 30 Aug 2004 20:29:51 GMT

On Tue, 24 Aug 2004 15:50:43 -0400, Joseph M. Newcomer
<newcomer@flounder.com> wrote:
>
>The only real distinction, which is syntactic/semantic but not behavioral, is that all the
>members of a struct are implicitly public, unless otherwise stated, and all the members
>of a class are implicitly private, unless otherwise stated.
>
AND, what most people usualy forget, is that default inheritance for
struct is public while for class it is private.

        class D : B

means

        class D : public B

if B is a struct, and

        class D : private B

if B is a class.

[snip]
>That is, there is no meaningful difference. He goes on to say that choice of struct vs.
>class is one of taste. However, I tend to prefer using a class in C++ because it is more
>natural.

Interesting.

For me, the notion

struct Point
{
  int x, y
  
  .... // eventually more members

};

is much cleaner than

class Point
{
  public:

  int x, y;

  .... // eventually more members

};

OTOH, I know that many style guides and so called programming gurus
say that classes need to be written with "class". Most of these also
say that all classes need a virtual destructor, an explicit copy
constructor, an explicit assignment operator, and such. Completely
crap.

>Note that there is no implied "more efficient", "more compact", "faster", or any
>other metric in the distinction between struct and class. You may choose to use one or the
>other for esthetic reasons, but don't believe for an instant that they have any other
>meaning in terms of implementation.

There are reasons to write a class with struct resp. class. PODs, for
example, should be written as struct. Classes which maintain state
with their usualy many private members should be written as class.
Other rules exist.

Now, to determine wheter the OP's class should be written with class
or struct, I'd say it is a stateful class and thereforde should be
written as follows:

class Node
{
public:

   Node( Node* aParent );
   unsigned int getSize() const;

protected: // consider also private here

  Node* mParent;
  unsigned int mChildCount;
  CString mFolder;

 };

I don't know how childCount and folder is used, so I did not declare
any functions here.

Hope that helps
Martin

------------------------------------------------
Martin Aupperle
------------------------------------------------



Relevant Pages

  • Re: Structure size directives
    ... When you target a specific architecture, the alignment requirements are know apriori, so system developers do make use of explicit alignment and knows what they are doing. ... struct foo obj; ... offset of 4 bytes, and with the #pragma pack, it would be allocated at ... There are really two different cases here, you talk about the usage of "pack" to minimize padding in a struct, while I talk about the "pack " usage, which specify alignmentof struct members. ...
    (comp.lang.c)
  • Re: procedure parameter struct order
    ... the members of this struct would always have the same order on ... >> members in the order they are declared. ... Or if there even is a stack as such, ... > compiler would be quite correct if, behind the scenes, all it pushed on ...
    (comp.lang.c)
  • Re: Uninitialized memory, malloc and unsigned char
    ... the structure or union object may be a trap representation. ... I think this ensures that the holes between sturct members do not ... affect validity of the struct. ...
    (comp.std.c)
  • Re: "common initial sequence" rule = non-obvious constraints on padding?
    ... of any member struct of a union which currently contains such a struct ... anywhere that union type is visible. ... I'm sure that on the DS9000, the linker searches the complete program for unions and adds random padding between structure members in a way that breaks this assumption. ... compatible, you can declare the same object twice, using both types, without breaking the program. ...
    (comp.std.c)
  • Re: struct sockaddr_XYZ formats portable?
    ... >> If however, for some reasons, you want to send a particular ... Byte order isn't the problem as the multi-byte members like port are ... order and overall struct sizes were equal so ... explicit alignment is specified in the headers. ...
    (comp.unix.programmer)

Quantcast